@@ -11,11 +11,11 @@ import (
1111)
1212
1313// CompareEnums returns added, deleted enum values.
14- func CompareEnums (left , right []interface {} ) []TypeDiff {
14+ func CompareEnums (left , right []any ) []TypeDiff {
1515 diffs := []TypeDiff {}
1616
17- leftStrs := []string {}
18- rightStrs := []string {}
17+ leftStrs := make ( []string , 0 , len ( left ))
18+ rightStrs := make ( []string , 0 , len ( right ))
1919 for _ , eachLeft := range left {
2020 leftStrs = append (leftStrs , fmt .Sprintf ("%v" , eachLeft ))
2121 }
@@ -36,7 +36,12 @@ func CompareEnums(left, right []interface{}) []TypeDiff {
3636}
3737
3838// CompareProperties recursive property comparison.
39- func CompareProperties (location DifferenceLocation , schema1 * spec.Schema , schema2 * spec.Schema , getRefFn1 SchemaFromRefFn , getRefFn2 SchemaFromRefFn , cmp CompareSchemaFn ) []SpecDifference {
39+ func CompareProperties (
40+ location DifferenceLocation ,
41+ schema1 , schema2 * spec.Schema ,
42+ getRefFn1 , getRefFn2 SchemaFromRefFn ,
43+ cmp CompareSchemaFn ,
44+ ) []SpecDifference {
4045 propDiffs := []SpecDifference {}
4146
4247 if schema1 .Properties == nil && schema2 .Properties == nil {
@@ -82,47 +87,67 @@ func CompareProperties(location DifferenceLocation, schema1 *spec.Schema, schema
8287// CompareFloatValues compares a float data item.
8388func CompareFloatValues (fieldName string , val1 * float64 , val2 * float64 , ifGreaterCode SpecChangeCode , ifLessCode SpecChangeCode ) []TypeDiff {
8489 diffs := []TypeDiff {}
90+
8591 if val1 != nil && val2 != nil {
86- if * val2 > * val1 {
92+ switch {
93+ case * val2 > * val1 :
8794 diffs = append (diffs , TypeDiff {Change : ifGreaterCode , Description : fmt .Sprintf ("%s %f->%f" , fieldName , * val1 , * val2 )})
88- } else if * val2 < * val1 {
95+ case * val2 < * val1 :
8996 diffs = append (diffs , TypeDiff {Change : ifLessCode , Description : fmt .Sprintf ("%s %f->%f" , fieldName , * val1 , * val2 )})
9097 }
91- } else {
92- if val1 != val2 {
93- if val1 != nil {
94- diffs = append (diffs , TypeDiff {Change : DeletedConstraint , Description : fmt .Sprintf ("%s(%f)" , fieldName , * val1 )})
95- } else {
96- diffs = append (diffs , TypeDiff {Change : AddedConstraint , Description : fmt .Sprintf ("%s(%f)" , fieldName , * val2 )})
97- }
98- }
98+
99+ return diffs
99100 }
101+
102+ if val1 == val2 {
103+ return diffs
104+ }
105+
106+ if val1 != nil { // thus val2 = nil
107+ diffs = append (diffs , TypeDiff {Change : DeletedConstraint , Description : fmt .Sprintf ("%s(%f)" , fieldName , * val1 )})
108+
109+ return diffs
110+ }
111+
112+ // val1 = nil, val2 != nil
113+ diffs = append (diffs , TypeDiff {Change : AddedConstraint , Description : fmt .Sprintf ("%s(%f)" , fieldName , * val2 )})
114+
100115 return diffs
101116}
102117
103118// CompareIntValues compares to int data items.
104119func CompareIntValues (fieldName string , val1 * int64 , val2 * int64 , ifGreaterCode SpecChangeCode , ifLessCode SpecChangeCode ) []TypeDiff {
105120 diffs := []TypeDiff {}
121+
106122 if val1 != nil && val2 != nil {
107- if * val2 > * val1 {
123+ switch {
124+ case * val2 > * val1 :
108125 diffs = append (diffs , TypeDiff {Change : ifGreaterCode , Description : fmt .Sprintf ("%s %d->%d" , fieldName , * val1 , * val2 )})
109- } else if * val2 < * val1 {
126+ case * val2 < * val1 :
110127 diffs = append (diffs , TypeDiff {Change : ifLessCode , Description : fmt .Sprintf ("%s %d->%d" , fieldName , * val1 , * val2 )})
111128 }
112- } else {
113- if val1 != val2 {
114- if val1 != nil {
115- diffs = append (diffs , TypeDiff {Change : DeletedConstraint , Description : fmt .Sprintf ("%s(%d)" , fieldName , * val1 )})
116- } else {
117- diffs = append (diffs , TypeDiff {Change : AddedConstraint , Description : fmt .Sprintf ("%s(%d)" , fieldName , * val2 )})
118- }
119- }
129+
130+ return diffs
131+ }
132+
133+ if val1 == val2 {
134+ return diffs
135+ }
136+
137+ if val1 != nil { // thus val2 = nil
138+ diffs = append (diffs , TypeDiff {Change : DeletedConstraint , Description : fmt .Sprintf ("%s(%d)" , fieldName , * val1 )})
139+
140+ return diffs
120141 }
142+
143+ // val1 = nil, val2 != nil
144+ diffs = append (diffs , TypeDiff {Change : AddedConstraint , Description : fmt .Sprintf ("%s(%d)" , fieldName , * val2 )})
145+
121146 return diffs
122147}
123148
124149// CheckToFromPrimitiveType check for diff to or from a primitive.
125- func CheckToFromPrimitiveType (diffs []TypeDiff , type1 , type2 interface {} ) []TypeDiff {
150+ func CheckToFromPrimitiveType (diffs []TypeDiff , type1 , type2 any ) []TypeDiff {
126151 type1IsPrimitive := isPrimitive (type1 )
127152 type2IsPrimitive := isPrimitive (type2 )
128153
@@ -137,7 +162,7 @@ func CheckToFromPrimitiveType(diffs []TypeDiff, type1, type2 interface{}) []Type
137162}
138163
139164// CheckRefChange has the property ref changed.
140- func CheckRefChange (diffs []TypeDiff , type1 , type2 interface {} ) (diffReturn []TypeDiff ) {
165+ func CheckRefChange (diffs []TypeDiff , type1 , type2 any ) (diffReturn []TypeDiff ) {
141166 diffReturn = diffs
142167 if isRefType (type1 ) && isRefType (type2 ) {
143168 // both refs but to different objects (TODO detect renamed object)
@@ -154,35 +179,42 @@ func CheckRefChange(diffs []TypeDiff, type1, type2 interface{}) (diffReturn []Ty
154179
155180// checkNumericTypeChanges checks for changes to or from a numeric type.
156181func checkNumericTypeChanges (diffs []TypeDiff , type1 , type2 * spec.SchemaProps ) []TypeDiff {
157- // Number
158182 _ , type1IsNumeric := numberWideness [type1 .Type [0 ]]
159183 _ , type2IsNumeric := numberWideness [type2 .Type [0 ]]
160184
161- if type1IsNumeric && type2IsNumeric {
162- foundDiff := false
163- if type1 .ExclusiveMaximum && ! type2 .ExclusiveMaximum {
164- diffs = addTypeDiff (diffs , TypeDiff {Change : WidenedType , Description : fmt .Sprintf ("Exclusive Maximum Removed:%v->%v" , type1 .ExclusiveMaximum , type2 .ExclusiveMaximum )})
165- foundDiff = true
166- }
167- if ! type1 .ExclusiveMaximum && type2 .ExclusiveMaximum {
168- diffs = addTypeDiff (diffs , TypeDiff {Change : NarrowedType , Description : fmt .Sprintf ("Exclusive Maximum Added:%v->%v" , type1 .ExclusiveMaximum , type2 .ExclusiveMaximum )})
169- foundDiff = true
170- }
171- if type1 .ExclusiveMinimum && ! type2 .ExclusiveMinimum {
172- diffs = addTypeDiff (diffs , TypeDiff {Change : WidenedType , Description : fmt .Sprintf ("Exclusive Minimum Removed:%v->%v" , type1 .ExclusiveMaximum , type2 .ExclusiveMaximum )})
173- foundDiff = true
174- }
175- if ! type1 .ExclusiveMinimum && type2 .ExclusiveMinimum {
176- diffs = addTypeDiff (diffs , TypeDiff {Change : NarrowedType , Description : fmt .Sprintf ("Exclusive Minimum Added:%v->%v" , type1 .ExclusiveMinimum , type2 .ExclusiveMinimum )})
177- foundDiff = true
178- }
179- if ! foundDiff {
180- maxDiffs := CompareFloatValues ("Maximum" , type1 .Maximum , type2 .Maximum , WidenedType , NarrowedType )
181- diffs = append (diffs , maxDiffs ... )
182- minDiffs := CompareFloatValues ("Minimum" , type1 .Minimum , type2 .Minimum , NarrowedType , WidenedType )
183- diffs = append (diffs , minDiffs ... )
184- }
185+ if ! type1IsNumeric || ! type2IsNumeric {
186+ return diffs
185187 }
188+
189+ foundDiff := false
190+
191+ if type1 .ExclusiveMaximum && ! type2 .ExclusiveMaximum {
192+ diffs = addTypeDiff (diffs , TypeDiff {Change : WidenedType , Description : fmt .Sprintf ("Exclusive Maximum Removed:%v->%v" , type1 .ExclusiveMaximum , type2 .ExclusiveMaximum )})
193+ foundDiff = true
194+ }
195+
196+ if ! type1 .ExclusiveMaximum && type2 .ExclusiveMaximum {
197+ diffs = addTypeDiff (diffs , TypeDiff {Change : NarrowedType , Description : fmt .Sprintf ("Exclusive Maximum Added:%v->%v" , type1 .ExclusiveMaximum , type2 .ExclusiveMaximum )})
198+ foundDiff = true
199+ }
200+
201+ if type1 .ExclusiveMinimum && ! type2 .ExclusiveMinimum {
202+ diffs = addTypeDiff (diffs , TypeDiff {Change : WidenedType , Description : fmt .Sprintf ("Exclusive Minimum Removed:%v->%v" , type1 .ExclusiveMaximum , type2 .ExclusiveMaximum )})
203+ foundDiff = true
204+ }
205+
206+ if ! type1 .ExclusiveMinimum && type2 .ExclusiveMinimum {
207+ diffs = addTypeDiff (diffs , TypeDiff {Change : NarrowedType , Description : fmt .Sprintf ("Exclusive Minimum Added:%v->%v" , type1 .ExclusiveMinimum , type2 .ExclusiveMinimum )})
208+ foundDiff = true
209+ }
210+
211+ if ! foundDiff {
212+ maxDiffs := CompareFloatValues ("Maximum" , type1 .Maximum , type2 .Maximum , WidenedType , NarrowedType )
213+ diffs = append (diffs , maxDiffs ... )
214+ minDiffs := CompareFloatValues ("Minimum" , type1 .Minimum , type2 .Minimum , NarrowedType , WidenedType )
215+ diffs = append (diffs , minDiffs ... )
216+ }
217+
186218 return diffs
187219}
188220
@@ -254,7 +286,7 @@ func getTypeHierarchyChange(type1, type2 string) TypeDiff {
254286 return TypeDiff {Change : ChangedType , Description : diffDescription }
255287}
256288
257- func isRefType (item interface {} ) bool {
289+ func isRefType (item any ) bool {
258290 switch s := item .(type ) {
259291 case spec.Refable :
260292 return s .Ref .String () != ""
0 commit comments