fieldmask-utils
fieldmask-utils copied to clipboard
Ambiguous merging of nullable structs with StructToStruct
The following new test case for copy.go
is failing. I added it in my local copy.
I wonder what the correct behavior should be in this case.
func TestStructToStruct_NestedNilParentStruct_NonNilDst(t *testing.T) {
type A struct {
Field1 string
Field2 int
}
type B struct {
Field1 string
Field2 int
A *A
}
type C struct {
Field1 string
B B
}
// Given: src.B.A is nil, dst.B.A is not nil
src := &C{
Field1: "src C field1",
B: B{
Field1: "src StringerB field1",
Field2: 1,
A: nil, // nil struct
},
}
dst := &C{
Field1: "dst C field1",
B: B{
Field1: "dst StringerB field1",
Field2: 2,
A: &A{
Field1: "dst StringerA field1",
Field2: 10,
},
},
}
// Given: mask contains subfield of nil field in src, i.e. src.B.A
mask := fieldmask_utils.MaskFromString("B{Field1,A{Field2}}")
// When: StructToStruct is called
err := fieldmask_utils.StructToStruct(mask, src, dst)
// Then: no error is returned
require.NoError(t, err)
// Then: dst B.A is not modified
assert.Equal(t, &C{
Field1: "dst C field1",
B: B{
Field1: src.B.Field1,
Field2: 2,
A: &A{
Field1: "dst StringerA field1",
Field2: 10,
},
},
}, dst)
}