-
Notifications
You must be signed in to change notification settings - Fork 63
Specifying {}
to remove nested fields should yield {}
, not nil
#259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package typed_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"sigs.k8s.io/structured-merge-diff/v4/internal/fixture" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't the other tests usually importing everything from fixture? |
||
"sigs.k8s.io/structured-merge-diff/v4/typed" | ||
) | ||
|
||
var updateParser = func() fixture.Parser { | ||
parser, err := typed.NewParser(` | ||
types: | ||
- name: nestedOptionalFields | ||
map: | ||
fields: | ||
- name: nestedList | ||
type: | ||
list: | ||
elementRelationship: associative | ||
keys: | ||
- name | ||
elementType: | ||
map: | ||
fields: | ||
- name: name | ||
type: | ||
scalar: string | ||
- name: value | ||
type: | ||
scalar: numeric | ||
- name: nestedMap | ||
type: | ||
map: | ||
elementType: | ||
scalar: numeric | ||
- name: nested | ||
type: | ||
map: | ||
fields: | ||
- name: numeric | ||
type: | ||
scalar: numeric | ||
- name: string | ||
type: | ||
scalar: string | ||
`) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return fixture.SameVersionParser{T: parser.Type("nestedOptionalFields")} | ||
}() | ||
|
||
func TestUpdate(t *testing.T) { | ||
tests := map[string]fixture.TestCase{ | ||
"delete_nested_fields_struct": { | ||
Ops: []fixture.Operation{ | ||
fixture.Apply{ | ||
Manager: "default", | ||
APIVersion: "v1", | ||
Object: ` | ||
nested: | ||
numeric: 1 | ||
string: my string | ||
`, | ||
}, | ||
fixture.Apply{ | ||
Manager: "default", | ||
APIVersion: "v1", | ||
Object: ` | ||
nested: {} | ||
`, | ||
}, | ||
}, | ||
APIVersion: `v1`, | ||
Object: `{nested: {}}`, | ||
}, | ||
"delete_nested_fields_list": { | ||
Ops: []fixture.Operation{ | ||
fixture.Apply{ | ||
Manager: "default", | ||
APIVersion: "v1", | ||
Object: ` | ||
nestedList: | ||
- name: first | ||
- name: second | ||
`, | ||
}, | ||
fixture.Apply{ | ||
Manager: "default", | ||
APIVersion: "v1", | ||
Object: ` | ||
nestedList: [] | ||
`, | ||
}, | ||
}, | ||
APIVersion: `v1`, | ||
Object: `{nestedList: []}`, | ||
}, | ||
"delete_nested_fields_map": { | ||
Ops: []fixture.Operation{ | ||
fixture.Apply{ | ||
Manager: "default", | ||
APIVersion: "v1", | ||
Object: ` | ||
nestedMap: | ||
first: 1 | ||
second: 2 | ||
|
||
`, | ||
}, | ||
fixture.Apply{ | ||
Manager: "default", | ||
APIVersion: "v1", | ||
Object: ` | ||
nestedMap: {} | ||
`, | ||
}, | ||
}, | ||
APIVersion: `v1`, | ||
Object: `{nestedMap: {}}`, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, so I guess now I'm confused about:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will add cases for 1 & 2 Re: 3. If you wanted to make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the null case naively I would expect it to keep null if you specified null. |
||
} | ||
|
||
for name, tc := range tests { | ||
tc2 := tc | ||
t.Run(name, func(t *testing.T) { | ||
typed.REMOVEKEEPEMPTYCOLLECTIONS = true | ||
if err := tc.Test(updateParser); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
|
||
t.Run(name+"Nil", func(t *testing.T) { | ||
typed.REMOVEKEEPEMPTYCOLLECTIONS = false | ||
if err := tc2.Test(updateParser); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we treat separately empty and nil here? Is that part of the problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change wasn't directly part of the problem I was trying to solve but I think something like this will be needed to have a consistent solution. The behavior of this right now always results in a
nil
list even if you specify[]