-
Notifications
You must be signed in to change notification settings - Fork 174
Fix dynamic representation of zero values in maps and slices #1154
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
Changes from all commits
513e014
4107c38
4d97777
36c8654
10380a4
ff51fec
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 |
|---|---|---|
|
|
@@ -68,6 +68,190 @@ func TestFromTypedStructSetFieldsRetainLocationIfUnchanged(t *testing.T) { | |
| assert.Equal(t, dyn.NewValue("qux", dyn.Location{}), nv.Get("bar")) | ||
| } | ||
|
|
||
| func TestFromTypedStringMapWithZeroValue(t *testing.T) { | ||
| ref := dyn.NilValue | ||
| src := map[string]string{ | ||
| "foo": "", | ||
| "bar": "fuzz", | ||
| } | ||
|
|
||
| nv, err := FromTyped(src, ref) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, dyn.V(map[string]dyn.Value{ | ||
| "foo": dyn.V(""), | ||
| "bar": dyn.V("fuzz"), | ||
| }), nv) | ||
| } | ||
|
|
||
| func TestFromTypedStringSliceWithZeroValue(t *testing.T) { | ||
| ref := dyn.NilValue | ||
| src := []string{"a", "", "c"} | ||
|
|
||
| nv, err := FromTyped(src, ref) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, dyn.V([]dyn.Value{ | ||
| dyn.V("a"), dyn.V(""), dyn.V("c"), | ||
| }), nv) | ||
| } | ||
|
|
||
| func TestFromTypedStringStructWithZeroValue(t *testing.T) { | ||
| type Tmp struct { | ||
| Foo string `json:"foo"` | ||
| Bar string `json:"bar"` | ||
|
Contributor
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. This raises an interesting point; what about Can deal with this later, as it is not handled now either.
Contributor
Author
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 fair point. I'm taking a look right now whether we need to handle it as a followup |
||
| } | ||
|
|
||
| ref := dyn.NilValue | ||
| src := Tmp{ | ||
| Foo: "foo", | ||
| Bar: "", | ||
| } | ||
|
|
||
| // Note, the zero value is not included in the output. | ||
| nv, err := FromTyped(src, ref) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, dyn.V(map[string]dyn.Value{ | ||
| "foo": dyn.V("foo"), | ||
| }), nv) | ||
| } | ||
|
|
||
| func TestFromTypedBoolMapWithZeroValue(t *testing.T) { | ||
| ref := dyn.NilValue | ||
| src := map[string]bool{ | ||
| "foo": false, | ||
| "bar": true, | ||
| } | ||
|
|
||
| nv, err := FromTyped(src, ref) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, dyn.V(map[string]dyn.Value{ | ||
| "foo": dyn.V(false), | ||
| "bar": dyn.V(true), | ||
| }), nv) | ||
| } | ||
|
|
||
| func TestFromTypedBoolSliceWithZeroValue(t *testing.T) { | ||
| ref := dyn.NilValue | ||
| src := []bool{true, false, true} | ||
|
|
||
| nv, err := FromTyped(src, ref) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, dyn.V([]dyn.Value{ | ||
| dyn.V(true), dyn.V(false), dyn.V(true), | ||
| }), nv) | ||
| } | ||
|
|
||
| func TestFromTypedBoolStructWithZeroValue(t *testing.T) { | ||
| type Tmp struct { | ||
| Foo bool `json:"foo"` | ||
| Bar bool `json:"bar"` | ||
| } | ||
|
|
||
| ref := dyn.NilValue | ||
| src := Tmp{ | ||
| Foo: true, | ||
| Bar: false, | ||
| } | ||
|
|
||
| // Note, the zero value is not included in the output. | ||
| nv, err := FromTyped(src, ref) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, dyn.V(map[string]dyn.Value{ | ||
| "foo": dyn.V(true), | ||
| }), nv) | ||
| } | ||
|
|
||
| func TestFromTypedIntMapWithZeroValue(t *testing.T) { | ||
| ref := dyn.NilValue | ||
| src := map[string]int{ | ||
| "foo": 0, | ||
| "bar": 1, | ||
| } | ||
|
|
||
| nv, err := FromTyped(src, ref) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, dyn.V(map[string]dyn.Value{ | ||
| "foo": dyn.V(int64(0)), | ||
| "bar": dyn.V(int64(1)), | ||
| }), nv) | ||
| } | ||
|
|
||
| func TestFromTypedIntSliceWithZeroValue(t *testing.T) { | ||
| ref := dyn.NilValue | ||
| src := []int{1, 0, 2} | ||
|
|
||
| nv, err := FromTyped(src, ref) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, dyn.V([]dyn.Value{ | ||
| dyn.V(int64(1)), dyn.V(int64(0)), dyn.V(int64(2)), | ||
| }), nv) | ||
| } | ||
|
|
||
| func TestFromTypedIntStructWithZeroValue(t *testing.T) { | ||
| type Tmp struct { | ||
| Foo int `json:"foo"` | ||
| Bar int `json:"bar"` | ||
| } | ||
|
|
||
| ref := dyn.NilValue | ||
| src := Tmp{ | ||
| Foo: 1, | ||
| Bar: 0, | ||
| } | ||
|
|
||
| // Note, the zero value is not included in the output. | ||
| nv, err := FromTyped(src, ref) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, dyn.V(map[string]dyn.Value{ | ||
| "foo": dyn.V(int64(1)), | ||
| }), nv) | ||
| } | ||
|
|
||
| func TestFromTypedFloatMapWithZeroValue(t *testing.T) { | ||
| ref := dyn.NilValue | ||
| src := map[string]float64{ | ||
| "foo": 0.0, | ||
| "bar": 1.0, | ||
| } | ||
|
|
||
| nv, err := FromTyped(src, ref) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, dyn.V(map[string]dyn.Value{ | ||
| "foo": dyn.V(0.0), | ||
| "bar": dyn.V(1.0), | ||
| }), nv) | ||
| } | ||
|
|
||
| func TestFromTypedFloatSliceWithZeroValue(t *testing.T) { | ||
| ref := dyn.NilValue | ||
| src := []float64{1.0, 0.0, 2.0} | ||
|
|
||
| nv, err := FromTyped(src, ref) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, dyn.V([]dyn.Value{ | ||
| dyn.V(1.0), dyn.V(0.0), dyn.V(2.0), | ||
| }), nv) | ||
| } | ||
|
|
||
| func TestFromTypedFloatStructWithZeroValue(t *testing.T) { | ||
| type Tmp struct { | ||
| Foo float64 `json:"foo"` | ||
| Bar float64 `json:"bar"` | ||
| } | ||
|
|
||
| ref := dyn.NilValue | ||
| src := Tmp{ | ||
| Foo: 1.0, | ||
| Bar: 0.0, | ||
| } | ||
|
|
||
| // Note, the zero value is not included in the output. | ||
| nv, err := FromTyped(src, ref) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, dyn.V(map[string]dyn.Value{ | ||
| "foo": dyn.V(1.0), | ||
| }), nv) | ||
| } | ||
|
|
||
| func TestFromTypedMapNil(t *testing.T) { | ||
| var src map[string]string = nil | ||
|
|
||
|
|
@@ -139,7 +323,7 @@ func TestFromTypedMapFieldWithZeroValue(t *testing.T) { | |
| nv, err := FromTyped(src, ref) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, dyn.V(map[string]dyn.Value{ | ||
| "foo": dyn.NilValue, | ||
| "foo": dyn.V(""), | ||
| }), nv) | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.