@@ -17,7 +17,6 @@ package komega
17
17
import (
18
18
"fmt"
19
19
"reflect"
20
- "strconv"
21
20
"strings"
22
21
23
22
"github.com/google/go-cmp/cmp"
@@ -33,15 +32,15 @@ var (
33
32
// IgnoreAutogeneratedMetadata contains the paths for all the metadata fields that are commonly set by the
34
33
// client and APIServer. This is used as a MatchOption for situations when only user-provided metadata is relevant.
35
34
IgnoreAutogeneratedMetadata = IgnorePaths {
36
- { "ObjectMeta" , "UID" } ,
37
- { "ObjectMeta" , "Generation" } ,
38
- { "ObjectMeta" , "CreationTimestamp" } ,
39
- { "ObjectMeta" , "ResourceVersion" } ,
40
- { "ObjectMeta" , "ManagedFields" } ,
41
- { "ObjectMeta" , "DeletionGracePeriodSeconds" } ,
42
- { "ObjectMeta" , "DeletionTimestamp" } ,
43
- { "ObjectMeta" , "SelfLink" } ,
44
- { "ObjectMeta" , "GenerateName" } ,
35
+ "metadata.uid" ,
36
+ "metadata.generation" ,
37
+ "metadata.creationTimestamp" ,
38
+ "metadata.resourceVersion" ,
39
+ "metadata.managedFields" ,
40
+ "metadata.deletionGracePeriodSeconds" ,
41
+ "metadata.deletionTimestamp" ,
42
+ "metadata.selfLink" ,
43
+ "metadata.generateName" ,
45
44
}
46
45
)
47
46
@@ -112,70 +111,59 @@ func (d diffPath) String() string {
112
111
// diffReporter is a custom recorder for cmp.Diff which records all paths that are
113
112
// different between two objects.
114
113
type diffReporter struct {
115
- stack []cmp.PathStep
116
- path []string
117
- jsonPath []string
114
+ stack []cmp.PathStep
118
115
119
116
diffPaths []diffPath
120
117
}
121
118
122
119
func (r * diffReporter ) PushStep (s cmp.PathStep ) {
123
120
r .stack = append (r .stack , s )
124
- if len (r .stack ) <= 1 {
125
- return
126
- }
127
- switch s := s .(type ) {
128
- case cmp.SliceIndex :
129
- r .path = append (r .path , strconv .Itoa (s .Key ()))
130
- r .jsonPath = append (r .jsonPath , strconv .Itoa (s .Key ()))
131
- case cmp.MapIndex :
132
- key := fmt .Sprintf ("%v" , s .Key ())
133
- // if strings.ContainsAny(key, ".[]/\\") {
134
- // key = fmt.Sprintf("[%s]", key)
135
- // } else {
136
- // key = "." + key
137
- // }
138
- r .path = append (r .path , key )
139
- r .jsonPath = append (r .jsonPath , key )
140
- case cmp.StructField :
141
- field := r .stack [len (r .stack )- 2 ].Type ().Field (s .Index ())
142
- jsonName := strings .Split (field .Tag .Get ("json" ), "," )[0 ]
143
- r .path = append (r .path , s .String ()[1 :])
144
- r .jsonPath = append (r .jsonPath , jsonName )
145
- }
146
121
}
147
122
148
123
func (r * diffReporter ) Report (res cmp.Result ) {
149
124
if ! res .Equal () {
150
- r .diffPaths = append (r .diffPaths , diffPath { types : r . path , json : r . jsonPath } )
125
+ r .diffPaths = append (r .diffPaths , r . currPath () )
151
126
}
152
127
}
153
128
154
- // func (r *diffReporter) currPath() string {
155
- // p := []string{}
156
- // for _, s := range r.stack[1:] {
157
- // switch s := s.(type) {
158
- // case cmp.StructField, cmp.SliceIndex, cmp.MapIndex:
159
- // p = append(p, s.String())
160
- // }
161
- // }
162
- // return strings.Join(p, "")[1:]
163
- // }
129
+ func (r * diffReporter ) currPath () diffPath {
130
+ p := diffPath {types : []string {"" }, json : []string {"" }}
131
+ i := 0
132
+ for si , s := range r .stack [1 :] {
133
+ switch s := s .(type ) {
134
+ case cmp.StructField :
135
+ p .types = append (p .types , s .String ()[1 :])
136
+ field := r .stack [si ].Type ().Field (s .Index ())
137
+ p .json = append (p .json , strings .Split (field .Tag .Get ("json" ), "," )[0 ])
138
+ i ++
139
+ case cmp.SliceIndex :
140
+ key := fmt .Sprintf ("[%d]" , s .Key ())
141
+ p .types [i ] += key
142
+ p .json [i ] += key
143
+ case cmp.MapIndex :
144
+ key := fmt .Sprintf ("%v" , s .Key ())
145
+ if strings .ContainsAny (key , ".[]/\\ " ) {
146
+ key = fmt .Sprintf ("[%s]" , key )
147
+ p .types [i ] += key
148
+ p .json [i ] += key
149
+ } else {
150
+ p .types = append (p .types , key )
151
+ p .json = append (p .json , key )
152
+ i ++
153
+ }
154
+ }
155
+ }
156
+ // empty strings were added as the first element, if they're still empty remove them again
157
+ if len (p .json ) > 0 && len (p .json [0 ]) == 0 {
158
+ p .json = p .json [1 :]
159
+ p .types = p .types [1 :]
160
+ }
161
+ fmt .Println (p )
162
+ return p
163
+ }
164
164
165
165
func (r * diffReporter ) PopStep () {
166
- popped := r .stack [len (r .stack )- 1 ]
167
166
r .stack = r .stack [:len (r .stack )- 1 ]
168
- if _ , ok := popped .(cmp.Indirect ); ok {
169
- return
170
- }
171
- if len (r .stack ) <= 1 {
172
- return
173
- }
174
- switch popped .(type ) {
175
- case cmp.SliceIndex , cmp.MapIndex , cmp.StructField :
176
- r .path = r .path [:len (r .path )- 1 ]
177
- r .jsonPath = r .jsonPath [:len (r .jsonPath )- 1 ]
178
- }
179
167
}
180
168
181
169
// calculateDiff calculates the difference between two objects and returns the
@@ -210,7 +198,7 @@ func filterDiffPaths(opts EqualObjectOptions, paths []diffPath) []diffPath {
210
198
211
199
func matchesPath (path []string , prefix []string ) bool {
212
200
for i , p := range prefix {
213
- if i >= len (path ) || p != path [i ] {
201
+ if i >= len (path ) || ! strings . HasPrefix ( path [i ], p ) {
214
202
return false
215
203
}
216
204
}
@@ -249,23 +237,22 @@ func (o *EqualObjectOptions) ApplyOptions(opts []EqualObjectOption) *EqualObject
249
237
return o
250
238
}
251
239
252
- // func parsePath(path string) []string {
253
- // s := strings.Split(path, ".")
254
- // return s
255
- // }
256
-
257
240
// IgnorePaths instructs the Matcher to ignore given paths when computing a diff.
258
- type IgnorePaths [][] string
241
+ type IgnorePaths []string
259
242
260
243
// ApplyToEqualObjectMatcher applies this configuration to the given MatchOptions.
261
244
func (i IgnorePaths ) ApplyToEqualObjectMatcher (opts * EqualObjectOptions ) {
262
- opts .ignorePaths = append (opts .ignorePaths , i ... )
245
+ for _ , p := range i {
246
+ opts .ignorePaths = append (opts .ignorePaths , strings .Split (p , "." ))
247
+ }
263
248
}
264
249
265
250
// MatchPaths instructs the Matcher to restrict its diff to the given paths. If empty the Matcher will look at all paths.
266
- type MatchPaths [][] string
251
+ type MatchPaths []string
267
252
268
253
// ApplyToEqualObjectMatcher applies this configuration to the given MatchOptions.
269
254
func (i MatchPaths ) ApplyToEqualObjectMatcher (opts * EqualObjectOptions ) {
270
- opts .matchPaths = append (opts .matchPaths , i ... )
255
+ for _ , p := range i {
256
+ opts .ignorePaths = append (opts .ignorePaths , strings .Split (p , "." ))
257
+ }
271
258
}
0 commit comments