Skip to content

Commit 7103b69

Browse files
committed
chore: relinted the diff package
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 590936f commit 7103b69

14 files changed

+215
-192
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ linters:
1414
- recvcheck
1515
- testpackage
1616
- thelper
17+
- tagliatelle
1718
- tparallel
1819
- varnamelen
1920
- whitespace

diff/array_diff.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,25 @@ func (f fromArrayStruct) DiffsTo(toArray []string) (added, deleted, common []str
5555

5656
// fromMapStruct utility struct to encompass diffing of string arrays.
5757
type fromMapStruct struct {
58-
srcMap map[string]interface{}
58+
srcMap map[string]any
5959
}
6060

6161
// fromStringMap starts a comparison by declaring a source map.
62-
func fromStringMap(srcMap map[string]interface{}) fromMapStruct {
62+
func fromStringMap(srcMap map[string]any) fromMapStruct {
6363
return fromMapStruct{srcMap}
6464
}
6565

6666
// Pair stores a pair of items which share a key in two maps.
6767
type Pair struct {
68-
First interface{}
69-
Second interface{}
68+
First any
69+
Second any
7070
}
7171

7272
// DiffsTo - generates diffs for a comparison.
73-
func (f fromMapStruct) DiffsTo(destMap map[string]interface{}) (added, deleted, common map[string]interface{}) {
74-
added = make(map[string]interface{})
75-
deleted = make(map[string]interface{})
76-
common = make(map[string]interface{})
73+
func (f fromMapStruct) DiffsTo(destMap map[string]any) (added, deleted, common map[string]any) {
74+
added = make(map[string]any)
75+
deleted = make(map[string]any)
76+
common = make(map[string]any)
7777

7878
inSrc := 1
7979
inDest := 2

diff/array_diff_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ func TestArrayDiff(t *testing.T) {
2424
}
2525

2626
func TestMapDiff(t *testing.T) {
27-
mapA := map[string]interface{}{"abc": 1, "def": 2, "ghi": 3, "jkl": 4}
27+
mapA := map[string]any{"abc": 1, "def": 2, "ghi": 3, "jkl": 4}
2828
added, deleted, common := fromStringMap(mapA).DiffsTo(mapA)
29-
require.Equal(t, map[string]interface{}{}, added)
30-
require.Equal(t, map[string]interface{}{}, deleted)
29+
require.Equal(t, map[string]any{}, added)
30+
require.Equal(t, map[string]any{}, deleted)
3131

32-
commonDiffs := map[string]interface{}{"abc": Pair{1, 1}, "def": Pair{2, 2}, "ghi": Pair{3, 3}, "jkl": Pair{4, 4}}
32+
commonDiffs := map[string]any{"abc": Pair{1, 1}, "def": Pair{2, 2}, "ghi": Pair{3, 3}, "jkl": Pair{4, 4}}
3333
require.Equal(t, commonDiffs, common)
3434

35-
mapB := map[string]interface{}{"abc": 2, "ghi": 3, "jkl": 4, "xyz": 5, "fgh": 6}
35+
mapB := map[string]any{"abc": 2, "ghi": 3, "jkl": 4, "xyz": 5, "fgh": 6}
3636
added, deleted, common = fromStringMap(mapA).DiffsTo(mapB)
37-
require.Equal(t, map[string]interface{}{"xyz": 5, "fgh": 6}, added)
38-
require.Equal(t, map[string]interface{}{"def": 2}, deleted)
39-
commonDiffs = map[string]interface{}{"abc": Pair{1, 2}, "ghi": Pair{3, 3}, "jkl": Pair{4, 4}}
37+
require.Equal(t, map[string]any{"xyz": 5, "fgh": 6}, added)
38+
require.Equal(t, map[string]any{"def": 2}, deleted)
39+
commonDiffs = map[string]any{"abc": Pair{1, 2}, "ghi": Pair{3, 3}, "jkl": Pair{4, 4}}
4040
require.Equal(t, commonDiffs, common)
4141
}

diff/checks.go

Lines changed: 84 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
8388
func 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.
104119
func 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.
156181
func 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

Comments
 (0)