Skip to content

Commit

Permalink
Add test and fix for copy of anonymous field with unexported fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Hardy committed Dec 10, 2021
1 parent 15ca5e0 commit b369e8a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,10 @@ func deepFields(reflectType reflect.Type) []reflect.StructField {
// field name. It is empty for upper case (exported) field names.
// See https://golang.org/ref/spec#Uniqueness_of_identifiers
if v.PkgPath == "" {
fields = append(fields, v)
if v.Anonymous {
// also consider fields of anonymous fields as fields of the root
fields = append(fields, deepFields(v.Type)...)
} else {
fields = append(fields, v)
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions copier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1453,3 +1453,20 @@ func TestDeepCopySimpleTime(t *testing.T) {
t.Errorf("to (%v) value should equal from (%v) value", to, from)
}
}

type TimeWrapper struct{
time.Time
}

func TestDeepCopyAnonymousFieldTime(t *testing.T) {
from := TimeWrapper{time.Now()}
to := TimeWrapper{}

err := copier.CopyWithOption(&to, from, copier.Option{DeepCopy: true})
if err != nil {
t.Error("should not error")
}
if !from.Time.Equal(to.Time) {
t.Errorf("to (%v) value should equal from (%v) value", to.Time, from.Time)
}
}

0 comments on commit b369e8a

Please sign in to comment.