Skip to content
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

Ambiguous merging of nullable structs with StructToStruct #45

Open
uabjabborov opened this issue Jun 17, 2024 · 1 comment
Open

Ambiguous merging of nullable structs with StructToStruct #45

uabjabborov opened this issue Jun 17, 2024 · 1 comment

Comments

@uabjabborov
Copy link

uabjabborov commented Jun 17, 2024

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)
}
@mennanov
Copy link
Owner

I think the current behavior is correct: if the source is nil then the destination should also be nil as a result.

If you need to keep the non-nil destination fields then you would need smth like a MapVisitor but for the structToStruct function.
It is not implemented yet, contributions are welcome though.

If you decide to implement it then consider adding a generic visitor option that can be used in both functions: StructToMap and StructToStruct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants