Skip to content

Commit

Permalink
Fix add support for anonymous field
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jan 12, 2021
1 parent b522e72 commit bee69a9
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,10 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
}

if fromField := source.FieldByName(name); fromField.IsValid() && !shouldIgnore(fromField, opt.IgnoreEmpty) {
// has field

// handle anonymous field
// process for nested anonymous field
destFieldNotSet := false
if f, ok := dest.Type().FieldByName(name); ok {
for _, x := range f.Index {
for idx, x := range f.Index {
destFieldKind := dest.Field(x).Kind()
if destFieldKind != reflect.Ptr {
continue
Expand All @@ -210,15 +208,16 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error)
break
}

newValue := reflect.New(dest.Field(x).Type().Elem())
newValue := reflect.New(dest.FieldByIndex(f.Index[0 : idx+1]).Type().Elem())
dest.Field(x).Set(newValue)
}
}

toField := reflect.Value{}
if !destFieldNotSet {
toField = dest.FieldByName(name)
if destFieldNotSet {
break
}

toField := dest.FieldByName(name)
if toField.IsValid() {
if toField.CanSet() {
if !set(toField, fromField, opt.DeepCopy) {
Expand Down

0 comments on commit bee69a9

Please sign in to comment.