From a00c770d0673117bc681f985734878857e3cec30 Mon Sep 17 00:00:00 2001 From: "jerry.wang" Date: Fri, 24 Nov 2023 12:09:45 +0800 Subject: [PATCH] refactor: rm unnecessary codes --- copier.go | 29 ++++------------------------- copier_issue170_test.go | 3 ++- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/copier.go b/copier.go index 836ce44..638bc86 100644 --- a/copier.go +++ b/copier.go @@ -250,13 +250,6 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error) return } - if len(converters) > 0 { - if ok, e := set(to, from, opt.DeepCopy, converters); e == nil && ok { - // converter supported - return - } - } - if from.Kind() == reflect.Slice || to.Kind() == reflect.Slice { isSlice = true if from.Kind() == reflect.Slice { @@ -281,24 +274,10 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error) dest = indirect(to) } + isSet := false if len(converters) > 0 { - if ok, e := set(dest, source, opt.DeepCopy, converters); e == nil && ok { - if isSlice { - // FIXME: maybe should check the other types? - if to.Type().Elem().Kind() == reflect.Ptr { - to.Index(i).Set(dest.Addr()) - } else { - if to.Len() < i+1 { - reflect.Append(to, dest) - } else { - to.Index(i).Set(dest) - } - } - } else { - to.Set(dest) - } - - continue + if isSet, err = set(dest, source, opt.DeepCopy, converters); err != nil { + return err } } @@ -316,7 +295,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error) } // check source - if source.IsValid() { + if source.IsValid() && !isSet { copyUnexportedStructFields(dest, source) // Copy from source field to dest field or method diff --git a/copier_issue170_test.go b/copier_issue170_test.go index f97278b..5e370b0 100644 --- a/copier_issue170_test.go +++ b/copier_issue170_test.go @@ -1,9 +1,10 @@ package copier_test import ( - "github.com/jinzhu/copier" "reflect" "testing" + + "github.com/jinzhu/copier" ) type A struct {