Skip to content

Commit 88e3665

Browse files
authored
internal: structdiff: ignore fields with json:"-" annotation (#4273)
## Why Certain structs in SDK (e.g. catalog.CreateMonitor) have this annotation for fields that are part of the path. We need those fields in input schema and state, so we typically add our own replacement for it. The original field still shows up in diff though, until this PR. ## Tests Unit tests.
1 parent 751b1d3 commit 88e3665

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

libs/structs/structdiff/diff.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ func diffStruct(ctx *diffContext, path *structpath.PathNode, s1, s2 reflect.Valu
202202

203203
// Resolve field name from JSON tag or fall back to Go field name
204204
fieldName := jsonTag.Name()
205+
if fieldName == "-" {
206+
continue
207+
}
208+
205209
if fieldName == "" {
206210
fieldName = sf.Name
207211
}

libs/structs/structdiff/diff_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import (
1010
type B struct{ S string }
1111

1212
type A struct {
13-
XX int `json:"xx"`
14-
X int `json:"x,omitempty"`
15-
B B `json:"b,omitempty"`
16-
P *B `json:"p,omitempty"`
17-
M map[string]int `json:"m,omitempty"`
18-
L []string `json:"l,omitempty"`
13+
XX int `json:"xx"`
14+
X int `json:"x,omitempty"`
15+
B B `json:"b,omitempty"`
16+
P *B `json:"p,omitempty"`
17+
M map[string]int `json:"m,omitempty"`
18+
L []string `json:"l,omitempty"`
19+
Ignored string `json:"-"`
1920
}
2021

2122
type C struct {
@@ -155,6 +156,12 @@ func TestGetStructDiff(t *testing.T) {
155156
b: A{L: []string{"a", "b"}},
156157
want: []ResolvedChange{{Field: "l", Old: []string{"a"}, New: []string{"a", "b"}}},
157158
},
159+
{
160+
name: "ignored field change",
161+
a: A{X: 5, Ignored: "old"},
162+
b: A{X: 5, Ignored: "new"},
163+
want: nil,
164+
},
158165

159166
// ForceSendFields with non-empty fields (omitempty)
160167
{

libs/structs/structdiff/equal.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ func equalStruct(s1, s2 reflect.Value) bool {
110110

111111
jsonTag := structtag.JSONTag(sf.Tag.Get("json"))
112112

113+
// Skip fields with json:"-"
114+
if jsonTag.Name() == "-" {
115+
continue
116+
}
117+
113118
v1Field := s1.Field(i)
114119
v2Field := s2.Field(i)
115120

0 commit comments

Comments
 (0)