Skip to content

Commit 3b1d281

Browse files
denikIsaac
andauthored
structdiff: diff json:"-," fields like encoding/json does (#7207)
`GetStructDiff` skipped every field whose JSON name is `-`, including `json:"-,"` / `json:"-,omitempty"` fields that encoding/json serializes under the key `"-"`. It now skips only the exact tag `json:"-"` via `structaccess.IsSkippedField`, which `IsEqual`, `structaccess` and `structwalk` already use. Before this change `IsEqual` could report two values as different while `GetStructDiff` returned no changes. The change is reported at path `-`, the same form `structwalk` emits and `structaccess.Get` resolves. No type in this repo or in databricks-sdk-go uses such a tag (only test fixtures do), so this is a consistency fix with no user-visible effect today. This pull request and its description were written by Isaac. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 251d760 commit 3b1d281

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

‎libs/structs/structdiff/diff.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,15 @@ func diffStruct(ctx *diffContext, path *structpath.PathNode, s1, s2 reflect.Valu
219219
continue
220220
}
221221

222+
// Only the exact tag json:"-" skips a field; json:"-," names it "-".
223+
if structaccess.IsSkippedField(sf) {
224+
continue
225+
}
226+
222227
jsonTag := structtag.JSONTag(sf.Tag.Get("json"))
223228

224229
// Resolve field name from JSON tag or fall back to Go field name
225230
fieldName := jsonTag.Name()
226-
if fieldName == "-" {
227-
continue
228-
}
229231

230232
isEmbed := sf.Name == structaccess.EmbeddedSliceFieldName
231233

‎libs/structs/structdiff/diff_test.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"testing"
77
"time"
88

9+
"github.com/databricks/cli/libs/structs/structaccess"
10+
911
"github.com/databricks/databricks-sdk-go/common/types/duration"
1012
sdktime "github.com/databricks/databricks-sdk-go/common/types/time"
1113
"github.com/databricks/databricks-sdk-go/service/jobs"
@@ -1034,3 +1036,26 @@ func TestGetStructDiffWholeBlock(t *testing.T) {
10341036
})
10351037
}
10361038
}
1039+
1040+
type dashNamed struct {
1041+
Skipped string `json:"-"`
1042+
Named string `json:"-,omitempty"` //nolint:staticcheck // odd tag is the point
1043+
}
1044+
1045+
// TestGetStructDiffDashNamedField checks that only the exact tag json:"-" skips a
1046+
// field: encoding/json serializes json:"-,omitempty" under the key "-", and
1047+
// structaccess resolves the path "-" to it.
1048+
func TestGetStructDiffDashNamedField(t *testing.T) {
1049+
before := &dashNamed{Skipped: "a", Named: "a"}
1050+
after := &dashNamed{Skipped: "b", Named: "b"}
1051+
1052+
changes, err := GetStructDiff(before, after, nil)
1053+
require.NoError(t, err)
1054+
assert.Equal(t, []ResolvedChange{{Field: "-", Old: "a", New: "b"}}, resolveChanges(changes))
1055+
1056+
value, err := structaccess.Get(after, changes[0].Path)
1057+
require.NoError(t, err)
1058+
assert.Equal(t, "b", value)
1059+
1060+
assert.Equal(t, len(changes) == 0, IsEqual(before, after))
1061+
}

0 commit comments

Comments
 (0)