Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 9f20ce6

Browse files
committed
Addressing Nick's comments
1 parent b4177f9 commit 9f20ce6

File tree

3 files changed

+92
-21
lines changed

3 files changed

+92
-21
lines changed

utils/analyze_output_utils.go

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package utils
22

3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/golang/glog"
8+
)
9+
310
type Result interface {
411
OutputStruct() interface{}
512
OutputText(resultType string) error
@@ -18,14 +25,24 @@ func (r ListAnalyzeResult) OutputStruct() interface{} {
1825
}
1926

2027
func (r ListAnalyzeResult) OutputText(resultType string) error {
21-
r.Analysis = r.Analysis.([]string)
28+
analysis, valid := r.Analysis.([]string)
29+
if !valid {
30+
glog.Error("Unexpected structure of Analysis. Should be of type []string")
31+
return errors.New(fmt.Sprintf("Could not output %s analysis result", r.AnalyzeType))
32+
}
33+
r.Analysis = analysis
34+
2235
return TemplateOutput(r, "ListAnalyze")
2336
}
2437

2538
type MultiVersionPackageAnalyzeResult AnalyzeResult
2639

2740
func (r MultiVersionPackageAnalyzeResult) OutputStruct() interface{} {
28-
analysis := r.Analysis.(map[string]map[string]PackageInfo)
41+
analysis, valid := r.Analysis.(map[string]map[string]PackageInfo)
42+
if !valid {
43+
glog.Error("Unexpected structure of Analysis. Should be of type map[string]map[string]PackageInfo")
44+
return errors.New(fmt.Sprintf("Could not output %s analysis result", r.AnalyzeType))
45+
}
2946
analysisOutput := getMultiVersionPackageOutput(analysis)
3047
output := struct {
3148
Image string
@@ -40,7 +57,11 @@ func (r MultiVersionPackageAnalyzeResult) OutputStruct() interface{} {
4057
}
4158

4259
func (r MultiVersionPackageAnalyzeResult) OutputText(resultType string) error {
43-
analysis := r.Analysis.(map[string]map[string]PackageInfo)
60+
analysis, valid := r.Analysis.(map[string]map[string]PackageInfo)
61+
if !valid {
62+
glog.Error("Unexpected structure of Analysis. Should be of type map[string]map[string]PackageInfo")
63+
return errors.New(fmt.Sprintf("Could not output %s analysis result", r.AnalyzeType))
64+
}
4465
analysisOutput := getMultiVersionPackageOutput(analysis)
4566

4667
strAnalysis := stringifyPackages(analysisOutput)
@@ -59,7 +80,11 @@ func (r MultiVersionPackageAnalyzeResult) OutputText(resultType string) error {
5980
type SingleVersionPackageAnalyzeResult AnalyzeResult
6081

6182
func (r SingleVersionPackageAnalyzeResult) OutputStruct() interface{} {
62-
analysis := r.Analysis.(map[string]PackageInfo)
83+
analysis, valid := r.Analysis.(map[string]PackageInfo)
84+
if !valid {
85+
glog.Error("Unexpected structure of Analysis. Should be of type map[string]PackageInfo")
86+
return errors.New(fmt.Sprintf("Could not output %s analysis result", r.AnalyzeType))
87+
}
6388
analysisOutput := getSingleVersionPackageOutput(analysis)
6489
output := struct {
6590
Image string
@@ -74,7 +99,11 @@ func (r SingleVersionPackageAnalyzeResult) OutputStruct() interface{} {
7499
}
75100

76101
func (r SingleVersionPackageAnalyzeResult) OutputText(diffType string) error {
77-
analysis := r.Analysis.(map[string]PackageInfo)
102+
analysis, valid := r.Analysis.(map[string]PackageInfo)
103+
if !valid {
104+
glog.Error("Unexpected structure of Analysis. Should be of type map[string]PackageInfo")
105+
return errors.New(fmt.Sprintf("Could not output %s analysis result", r.AnalyzeType))
106+
}
78107
analysisOutput := getSingleVersionPackageOutput(analysis)
79108

80109
strAnalysis := stringifyPackages(analysisOutput)
@@ -130,7 +159,12 @@ func getMultiVersionPackageOutput(packageMap map[string]map[string]PackageInfo)
130159
type FileAnalyzeResult AnalyzeResult
131160

132161
func (r FileAnalyzeResult) OutputStruct() interface{} {
133-
analysis := r.Analysis.([]DirectoryEntry)
162+
analysis, valid := r.Analysis.([]DirectoryEntry)
163+
if !valid {
164+
glog.Error("Unexpected structure of Analysis. Should be of type []DirectoryEntry")
165+
return errors.New("Could not output FileAnalyzer analysis result")
166+
}
167+
134168
if SortSize {
135169
directoryBy(directorySizeSort).Sort(analysis)
136170
} else {
@@ -141,7 +175,12 @@ func (r FileAnalyzeResult) OutputStruct() interface{} {
141175
}
142176

143177
func (r FileAnalyzeResult) OutputText(analyzeType string) error {
144-
analysis := r.Analysis.([]DirectoryEntry)
178+
analysis, valid := r.Analysis.([]DirectoryEntry)
179+
if !valid {
180+
glog.Error("Unexpected structure of Analysis. Should be of type []DirectoryEntry")
181+
return errors.New("Could not output FileAnalyzer analysis result")
182+
}
183+
145184
if SortSize {
146185
directoryBy(directorySizeSort).Sort(analysis)
147186
} else {

utils/diff_output_utils.go

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package utils
22

3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/golang/glog"
8+
)
9+
310
type DiffResult struct {
411
Image1 string
512
Image2 string
@@ -10,7 +17,12 @@ type DiffResult struct {
1017
type MultiVersionPackageDiffResult DiffResult
1118

1219
func (r MultiVersionPackageDiffResult) OutputStruct() interface{} {
13-
diff := r.Diff.(MultiVersionPackageDiff)
20+
diff, valid := r.Diff.(MultiVersionPackageDiff)
21+
if !valid {
22+
glog.Error("Unexpected structure of Diff. Should follow the MultiVersionPackageDiff struct")
23+
return errors.New(fmt.Sprintf("Could not output %s diff result", r.DiffType))
24+
}
25+
1426
diffOutput := struct {
1527
Packages1 []PackageOutput
1628
Packages2 []PackageOutput
@@ -25,7 +37,11 @@ func (r MultiVersionPackageDiffResult) OutputStruct() interface{} {
2537
}
2638

2739
func (r MultiVersionPackageDiffResult) OutputText(diffType string) error {
28-
diff := r.Diff.(MultiVersionPackageDiff)
40+
diff, valid := r.Diff.(MultiVersionPackageDiff)
41+
if !valid {
42+
glog.Error("Unexpected structure of Diff. Should follow the MultiVersionPackageDiff struct")
43+
return errors.New(fmt.Sprintf("Could not output %s diff result", r.DiffType))
44+
}
2945

3046
strPackages1 := stringifyPackages(getMultiVersionPackageOutput(diff.Packages1))
3147
strPackages2 := stringifyPackages(getMultiVersionPackageOutput(diff.Packages2))
@@ -67,7 +83,12 @@ func getMultiVersionInfoDiffOutput(infoDiff []MultiVersionInfo) []MultiVersionIn
6783
type SingleVersionPackageDiffResult DiffResult
6884

6985
func (r SingleVersionPackageDiffResult) OutputStruct() interface{} {
70-
diff := r.Diff.(PackageDiff)
86+
diff, valid := r.Diff.(PackageDiff)
87+
if !valid {
88+
glog.Error("Unexpected structure of Diff. Should follow the PackageDiff struct")
89+
return errors.New(fmt.Sprintf("Could not output %s diff result", r.DiffType))
90+
}
91+
7192
diffOutput := struct {
7293
Packages1 []PackageOutput
7394
Packages2 []PackageOutput
@@ -82,7 +103,11 @@ func (r SingleVersionPackageDiffResult) OutputStruct() interface{} {
82103
}
83104

84105
func (r SingleVersionPackageDiffResult) OutputText(diffType string) error {
85-
diff := r.Diff.(PackageDiff)
106+
diff, valid := r.Diff.(PackageDiff)
107+
if !valid {
108+
glog.Error("Unexpected structure of Diff. Should follow the PackageDiff struct")
109+
return errors.New(fmt.Sprintf("Could not output %s diff result", r.DiffType))
110+
}
86111

87112
strPackages1 := stringifyPackages(getSingleVersionPackageOutput(diff.Packages1))
88113
strPackages2 := stringifyPackages(getSingleVersionPackageOutput(diff.Packages2))
@@ -134,12 +159,23 @@ func (r HistDiffResult) OutputText(diffType string) error {
134159
type DirDiffResult DiffResult
135160

136161
func (r DirDiffResult) OutputStruct() interface{} {
137-
r.Diff = sortDirDiff(r.Diff.(DirDiff))
162+
diff, valid := r.Diff.(DirDiff)
163+
if !valid {
164+
glog.Error("Unexpected structure of Diff. Should follow the DirDiff struct")
165+
return errors.New("Could not output FileAnalyzer diff result")
166+
}
167+
168+
r.Diff = sortDirDiff(diff)
138169
return r
139170
}
140171

141172
func (r DirDiffResult) OutputText(diffType string) error {
142-
diff := sortDirDiff(r.Diff.(DirDiff))
173+
diff, valid := r.Diff.(DirDiff)
174+
if !valid {
175+
glog.Error("Unexpected structure of Diff. Should follow the DirDiff struct")
176+
return errors.New("Could not output FileAnalyzer diff result")
177+
}
178+
diff = sortDirDiff(diff)
143179

144180
strAdds := stringifyDirectoryEntries(diff.Adds)
145181
strDels := stringifyDirectoryEntries(diff.Dels)

utils/output_sort_utils.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (s *singleVersionInfoSorter) Less(i, j int) bool {
8080
}
8181

8282
func (s *singleVersionInfoSorter) Swap(i, j int) {
83-
s.packageDiffs[i], s.packageDiffs[j] = s.packageDiffs[i], s.packageDiffs[j]
83+
s.packageDiffs[i], s.packageDiffs[j] = s.packageDiffs[j], s.packageDiffs[i]
8484
}
8585

8686
var singleInfoNameSort = func(a, b *Info) bool {
@@ -89,11 +89,7 @@ var singleInfoNameSort = func(a, b *Info) bool {
8989

9090
// Sorts MultiVersionInfos by package instance with the largest size in the first image, in descending order
9191
var singleInfoSizeSort = func(a, b *Info) bool {
92-
aInfo1 := a.Info1
93-
bInfo1 := b.Info1
94-
95-
// Compares the sizes of the packages in the first image
96-
return aInfo1.Size > bInfo1.Size
92+
return a.Info1.Size > b.Info1.Size
9793
}
9894

9995
type multiInfoBy func(a, b *MultiVersionInfo) bool
@@ -120,7 +116,7 @@ func (s *multiVersionInfoSorter) Less(i, j int) bool {
120116
}
121117

122118
func (s *multiVersionInfoSorter) Swap(i, j int) {
123-
s.packageDiffs[i], s.packageDiffs[j] = s.packageDiffs[i], s.packageDiffs[j]
119+
s.packageDiffs[i], s.packageDiffs[j] = s.packageDiffs[j], s.packageDiffs[i]
124120
}
125121

126122
var multiInfoNameSort = func(a, b *MultiVersionInfo) bool {
@@ -250,7 +246,7 @@ func (s *entryDiffSorter) Less(i, j int) bool {
250246
}
251247

252248
func (s *entryDiffSorter) Swap(i, j int) {
253-
s.entryDiffs[i], s.entryDiffs[j] = s.entryDiffs[i], s.entryDiffs[j]
249+
s.entryDiffs[i], s.entryDiffs[j] = s.entryDiffs[j], s.entryDiffs[i]
254250
}
255251

256252
var entryDiffNameSort = func(a, b *EntryDiff) bool {

0 commit comments

Comments
 (0)