diff --git a/loader.go b/loader.go index 9a08545..d03451f 100644 --- a/loader.go +++ b/loader.go @@ -75,7 +75,7 @@ func (l *loader) loadDiff(old, newT any) error { if oElem.Type().Field(i).Anonymous { // If the embedded field is a pointer, dereference it if oElem.Field(i).Kind() == reflect.Ptr { - if !oElem.Field(i).IsNil() && !nElem.Field(i).IsNil() { + if !oElem.Field(i).IsNil() && !nElem.Field(i).IsNil() { // If both are not nil, we need to recursively call LoadDiff if err := l.loadDiff(oElem.Field(i).Interface(), nElem.Field(i).Interface()); err != nil { return err } @@ -107,9 +107,7 @@ func (l *loader) loadDiff(old, newT any) error { // Compare the old and new fields. // - // New fields take priority over old fields if they are provided. - // - // We calculate whether we should be updating the field based off the LoaderOption's provided. + // New fields take priority over old fields if they are provided based on the configuration. if !nElem.Field(i).IsZero() || l.includeZeroValues { oElem.Field(i).Set(nElem.Field(i)) } else if nElem.Field(i).Kind() == reflect.Ptr && nElem.Field(i).IsNil() && l.includeNilValues { diff --git a/loader_test.go b/loader_test.go index cafa9af..5d7b580 100644 --- a/loader_test.go +++ b/loader_test.go @@ -46,6 +46,30 @@ func (s *loadDiffSuite) TestLoadDiff_Success() { s.Equal(26, old.Age) } +func (s *loadDiffSuite) TestLoadDiff_Success_Pointed_Fields() { + s.l.includeNilValues = true + + type testStruct struct { + Name *string + Age *int + } + + old := testStruct{ + Name: ptr("John"), + Age: ptr(25), + } + + n := testStruct{ + Name: ptr("John Smith"), + Age: ptr(26), + } + + err := s.l.loadDiff(&old, &n) + s.NoError(err) + s.Equal("John Smith", *old.Name) + s.Equal(26, *old.Age) +} + func (s *loadDiffSuite) TestLoadDiff_Success_ZeroValue() { type testStruct struct { Name string @@ -135,6 +159,69 @@ func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedStruct() { s.Equal(24, old.Partner.Age) } +func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedStruct_Reverse() { + type testStruct struct { + Name string + Age int + Partner *testStruct + } + + old := testStruct{ + Name: "John", + Age: 25, + Partner: &testStruct{ + Name: "Sarah", + Age: 24, + }, + } + + n := testStruct{ + Partner: &testStruct{ + Name: "Sarah Thompson", + Age: 27, + }, + } + + err := s.l.loadDiff(&old, &n) + s.NoError(err) + s.Equal("John", old.Name) + s.Equal(25, old.Age) + s.Equal("Sarah Thompson", old.Partner.Name) + s.Equal(27, old.Partner.Age) +} + +func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedStruct_NotPointed() { + type testEmbed struct { + Name string + Age int + } + + type testStruct struct { + Name string + Age int + Partner testEmbed + } + + old := testStruct{ + Name: "John", + Age: 25, + } + + n := testStruct{ + Partner: testEmbed{ + Name: "Sarah", + Age: 24, + }, + } + + err := s.l.loadDiff(&old, &n) + s.NoError(err) + s.Equal("John", old.Name) + s.Equal(25, old.Age) + s.Equal("Sarah", old.Partner.Name) + s.Equal(24, old.Partner.Age) +} + func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedStructWithNewValue() { type testStruct struct { Name string