From 9a805a35226996b6da62a190016c7f1f6909c179 Mon Sep 17 00:00:00 2001 From: "xiejinchi.xjc" Date: Fri, 16 Sep 2022 18:12:29 +0800 Subject: [PATCH 1/2] fix empty slice to nil issue --- copier.go | 30 ++++++++++++++++++------------ copier_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/copier.go b/copier.go index 286ddb6..20c65c3 100644 --- a/copier.go +++ b/copier.go @@ -176,26 +176,27 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error) return } - if from.Kind() == reflect.Slice && to.Kind() == reflect.Slice && fromType.ConvertibleTo(toType) { + if from.Kind() == reflect.Slice && to.Kind() == reflect.Slice { if to.IsNil() { slice := reflect.MakeSlice(reflect.SliceOf(to.Type().Elem()), from.Len(), from.Cap()) to.Set(slice) } + if fromType.ConvertibleTo(toType) { + for i := 0; i < from.Len(); i++ { + if to.Len() < i+1 { + to.Set(reflect.Append(to, reflect.New(to.Type().Elem()).Elem())) + } - for i := 0; i < from.Len(); i++ { - if to.Len() < i+1 { - to.Set(reflect.Append(to, reflect.New(to.Type().Elem()).Elem())) - } - - if !set(to.Index(i), from.Index(i), opt.DeepCopy, converters) { - // ignore error while copy slice element - err = copier(to.Index(i).Addr().Interface(), from.Index(i).Interface(), opt) - if err != nil { - continue + if !set(to.Index(i), from.Index(i), opt.DeepCopy, converters) { + // ignore error while copy slice element + err = copier(to.Index(i).Addr().Interface(), from.Index(i).Interface(), opt) + if err != nil { + continue + } } } + return } - return } if fromType.Kind() != reflect.Struct || toType.Kind() != reflect.Struct { @@ -210,6 +211,11 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error) } } + //if to.IsNil() { + // slice := reflect.MakeSlice(reflect.SliceOf(to.Type().Elem()), from.Len(), from.Cap()) + // to.Set(slice) + //} + for i := 0; i < amount; i++ { var dest, source reflect.Value diff --git a/copier_test.go b/copier_test.go index 4cc85b4..ec097cc 100644 --- a/copier_test.go +++ b/copier_test.go @@ -1666,3 +1666,33 @@ func TestSqlNullFiled(t *testing.T) { t.Errorf("to (%v) value should equal from (%v) value", to.MkExpiryDateType, from.MkExpiryDateType.Int32) } } + +func TestEmptySlice(t *testing.T) { + type Str1 string + type Str2 string + type Input1 struct { + Val Str1 + } + type Input2 struct { + Val Str2 + } + to := []*Input1(nil) + from := []*Input2{} + err := copier.Copy(&to, &from) + if err != nil { + t.Error("should not error") + } + if from == nil { + t.Error("from should be empty slice not nil") + } + + to = []*Input1(nil) + from = []*Input2(nil) + err = copier.Copy(&to, &from) + if err != nil { + t.Error("should not error") + } + if from != nil { + t.Error("from should be empty slice nil") + } +} From 6b861a874915d80d3ee699bcbace2ca6dd0749a7 Mon Sep 17 00:00:00 2001 From: "xiejinchi.xjc" Date: Fri, 16 Sep 2022 18:26:53 +0800 Subject: [PATCH 2/2] remove useless comment --- copier.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/copier.go b/copier.go index 20c65c3..84efd25 100644 --- a/copier.go +++ b/copier.go @@ -211,11 +211,6 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error) } } - //if to.IsNil() { - // slice := reflect.MakeSlice(reflect.SliceOf(to.Type().Elem()), from.Len(), from.Cap()) - // to.Set(slice) - //} - for i := 0; i < amount; i++ { var dest, source reflect.Value