|
9 | 9 |
|
10 | 10 | "github.com/databricks/cli/libs/structs/structaccess" |
11 | 11 | "github.com/databricks/cli/libs/structs/structpath" |
12 | | - "github.com/databricks/cli/libs/structs/structtag" |
13 | 12 | ) |
14 | 13 |
|
15 | 14 | type Change struct { |
@@ -218,69 +217,35 @@ func equalLeaf(v1, v2 reflect.Value) bool { |
218 | 217 | } |
219 | 218 |
|
220 | 219 | func diffStruct(ctx *diffContext, path *structpath.PathNode, s1, s2 reflect.Value, changes *[]Change) error { |
221 | | - t := s1.Type() |
222 | | - forced1 := getForceSendFields(s1) |
223 | | - forced2 := getForceSendFields(s2) |
| 220 | + info := structaccess.GetStructInfo(s1.Type()) |
| 221 | + forced1 := info.ForceSendFields(s1) |
| 222 | + forced2 := info.ForceSendFields(s2) |
224 | 223 |
|
225 | | - for i := range t.NumField() { |
226 | | - sf := t.Field(i) |
227 | | - if !sf.IsExported() || sf.Name == "ForceSendFields" { |
228 | | - continue |
229 | | - } |
| 224 | + for _, f := range info.Fields { |
| 225 | + v1Field := s1.Field(f.Index) |
| 226 | + v2Field := s2.Field(f.Index) |
230 | 227 |
|
231 | | - // Continue traversing embedded structs. Do not add the key to the path though. An |
232 | | - // anonymous field carrying a json name is not one of these: encoding/json serializes it |
233 | | - // as a nested object, so it is handled as a named field below. |
234 | | - if structaccess.IsFlattenedEmbed(sf) { |
235 | | - if err := diffValues(ctx, path, s1.Field(i), s2.Field(i), changes); err != nil { |
| 228 | + // Continue traversing embedded structs. Do not add the key to the path though. |
| 229 | + if f.Flattened { |
| 230 | + if err := diffValues(ctx, path, v1Field, v2Field, changes); err != nil { |
236 | 231 | return err |
237 | 232 | } |
238 | 233 | continue |
239 | 234 | } |
240 | 235 |
|
241 | | - // Only the exact tag json:"-" skips a field; json:"-," names it "-". |
242 | | - if structaccess.IsSkippedField(sf) { |
243 | | - continue |
244 | | - } |
245 | | - |
246 | | - jsonTag := structtag.JSONTag(sf.Tag.Get("json")) |
247 | | - |
248 | | - // Resolve field name from JSON tag or fall back to Go field name |
249 | | - fieldName := jsonTag.Name() |
250 | | - |
251 | | - isEmbed := sf.Name == structaccess.EmbeddedSliceFieldName |
252 | | - |
253 | | - if fieldName == "" || isEmbed { |
254 | | - fieldName = sf.Name |
255 | | - } |
256 | | - |
257 | | - v1Field := s1.Field(i) |
258 | | - v2Field := s2.Field(i) |
259 | | - |
260 | | - zero1 := v1Field.IsZero() |
261 | | - zero2 := v2Field.IsZero() |
262 | | - |
263 | | - if zero1 || zero2 { |
264 | | - if jsonTag.OmitEmpty() { |
265 | | - if zero1 { |
266 | | - if !slices.Contains(forced1, sf.Name) { |
267 | | - v1Field = reflect.ValueOf(nil) |
268 | | - } |
269 | | - } |
270 | | - if zero2 { |
271 | | - if !slices.Contains(forced2, sf.Name) { |
272 | | - v2Field = reflect.ValueOf(nil) |
273 | | - } |
274 | | - } |
| 236 | + if f.OmitEmpty { |
| 237 | + if v1Field.IsZero() && !slices.Contains(forced1, f.StructField.Name) { |
| 238 | + v1Field = reflect.ValueOf(nil) |
| 239 | + } |
| 240 | + if v2Field.IsZero() && !slices.Contains(forced2, f.StructField.Name) { |
| 241 | + v2Field = reflect.ValueOf(nil) |
275 | 242 | } |
276 | 243 | } |
277 | 244 |
|
278 | 245 | // EmbeddedSlice: diff at parent path level without adding field name. |
279 | | - var node *structpath.PathNode |
280 | | - if isEmbed { |
281 | | - node = path |
282 | | - } else { |
283 | | - node = structpath.NewDotString(path, fieldName) |
| 246 | + node := path |
| 247 | + if !f.EmbeddedSlice { |
| 248 | + node = structpath.NewDotString(path, f.Name) |
284 | 249 | } |
285 | 250 |
|
286 | 251 | if err := diffValues(ctx, node, v1Field, v2Field, changes); err != nil { |
@@ -352,21 +317,6 @@ func diffStringMaps(path *structpath.PathNode, m1, m2 map[string]string, changes |
352 | 317 | } |
353 | 318 | } |
354 | 319 |
|
355 | | -func getForceSendFields(v reflect.Value) []string { |
356 | | - if !v.IsValid() || v.Kind() != reflect.Struct { |
357 | | - return nil |
358 | | - } |
359 | | - fsField := v.FieldByName("ForceSendFields") |
360 | | - if !fsField.IsValid() || fsField.Kind() != reflect.Slice { |
361 | | - return nil |
362 | | - } |
363 | | - result, ok := reflect.TypeAssert[[]string](fsField) |
364 | | - if ok { |
365 | | - return result |
366 | | - } |
367 | | - return nil |
368 | | -} |
369 | | - |
370 | 320 | // findKeyFunc returns the KeyFunc for the given path, or nil if none matches. |
371 | 321 | // Path patterns support [*] to match any slice index. |
372 | 322 | func (ctx *diffContext) findKeyFunc(path *structpath.PathNode) KeyFunc { |
|
0 commit comments