mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
Merge pull request #51356 from robmry/compat-replace-nil-pointer
API compat: replace nil values when adding fields
This commit is contained in:
@@ -46,7 +46,7 @@ func appendFields(src, dst map[string]any) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if _, ok := dst[k]; !ok {
|
||||
if existing, ok := dst[k]; !ok || existing == nil {
|
||||
dst[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/moby/moby/v2/daemon/internal/compat"
|
||||
"gotest.tools/v3/assert"
|
||||
is "gotest.tools/v3/assert/cmp"
|
||||
)
|
||||
|
||||
type Info struct {
|
||||
@@ -80,6 +82,49 @@ func TestWrap(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapNilPtrField(t *testing.T) {
|
||||
type bStruct struct {
|
||||
StringField string `json:"stringfield"`
|
||||
}
|
||||
type aStruct struct {
|
||||
IntField *int `json:"intfield"`
|
||||
StructField *bStruct `json:"structfield"`
|
||||
}
|
||||
info := &aStruct{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
options []compat.Option
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "none",
|
||||
expected: `{"intfield":null,"structfield":null}`,
|
||||
},
|
||||
{
|
||||
name: "replace nil int",
|
||||
options: []compat.Option{compat.WithExtraFields(map[string]any{"intfield": 42})},
|
||||
expected: `{"intfield":42,"structfield":null}`,
|
||||
},
|
||||
{
|
||||
name: "replace nil struct",
|
||||
options: []compat.Option{compat.WithExtraFields(map[string]any{
|
||||
"structfield": map[string]any{"stringfield": "hello"},
|
||||
})},
|
||||
expected: `{"intfield":null,"structfield":{"stringfield":"hello"}}`,
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
resp := compat.Wrap(info, tc.options...)
|
||||
data, err := json.Marshal(resp)
|
||||
if assert.Check(t, err) {
|
||||
assert.Check(t, is.Equal(string(data), tc.expected))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNestedCompat(t *testing.T) {
|
||||
info := &Info{
|
||||
Name: "daemon",
|
||||
|
||||
Reference in New Issue
Block a user