diff --git a/copy.go b/copy.go index acf1856..57aa1c8 100644 --- a/copy.go +++ b/copy.go @@ -2,7 +2,6 @@ package struct_copy import ( "errors" - "fmt" "reflect" ) @@ -34,6 +33,12 @@ func StructCopy(dst any, src any, depth int) (err error) { return } func checkTypeAndConv(newValue reflect.Value, dstField reflect.Value) (fValue any, err error) { + if newValue.Kind() == reflect.Ptr { + newValue = newValue.Elem() + } + if dstField.Kind() == reflect.Ptr { + dstField = dstField.Elem() + } switch newValue.Interface().(type) { case int32: switch dstField.Interface().(type) { @@ -258,39 +263,48 @@ func sliceCopy(dst reflect.Value, src any, depth int, current int) (err error) { } //基础数组赋值 if basicSlice > 0 { - var fValue interface{} - fValue, err = checkTypeAndConv(reflect.ValueOf(src), reflect.ValueOf(convDst)) - if err != nil { - return - } + fValue := src if convDst.Type() == reflect.ValueOf(fValue).Type() { convDst.Set(reflect.ValueOf(fValue)) dst.Set(convDst) return } fv := reflect.ValueOf(fValue) + convPrtType := 0 if convDst.Kind() == reflect.Ptr { - for i := 0; i < fv.Len(); i++ { - item := reflect.New(convDst.Elem().Type().Elem()) - item.Elem().Set(reflect.ValueOf(fv.Index(i).Interface())) - convDst.Elem().Set(reflect.Append(convDst.Elem(), item.Elem())) + convPrtType = 1 + } else if convDst.Type().Elem().Kind() == reflect.Ptr { + convPrtType = 2 + } + for i := 0; i < fv.Len(); i++ { + var item reflect.Value + if convPrtType == 1 { + item = reflect.New(convDst.Elem().Type().Elem()) + } else if convPrtType == 2 { + item = reflect.New(convDst.Type().Elem().Elem()) + } else { + item = reflect.New(convDst.Type().Elem()) } - if dst.Type() == convDst.Type() { - dst.Set(convDst) + var f any + f, err = checkTypeAndConv(reflect.ValueOf(fv.Index(i).Interface()), item) + if err != nil { + return } - return - } else if convDst.Type().Elem().Kind() == reflect.Ptr { - for i := 0; i < fv.Len(); i++ { - item := reflect.New(convDst.Type().Elem().Elem()) - item.Elem().Set(reflect.ValueOf(fv.Index(i).Interface())) + if convPrtType == 1 { + item.Elem().Set(reflect.ValueOf(f)) + //item.Elem().Set(reflect.ValueOf(fv.Index(i).Interface())) + convDst.Elem().Set(reflect.Append(convDst.Elem(), item.Elem())) + } else if convPrtType == 2 { + item.Elem().Set(reflect.ValueOf(f)) convDst.Set(reflect.Append(convDst, item)) + } else { + item.Elem().Set(reflect.ValueOf(f)) + convDst.Set(reflect.Append(convDst, item.Elem())) } - if dst.Type() == convDst.Type() { - dst.Set(convDst) - } - fmt.Println(1) } - + if dst.Type() == convDst.Type() { + dst.Set(convDst) + } return } //结构体赋值 diff --git a/copy_test.go b/copy_test.go index 5a3aa19..a64946c 100644 --- a/copy_test.go +++ b/copy_test.go @@ -17,6 +17,9 @@ type A1 struct { I []string K []*C1 J []C1 + L []int32 + M []int64 + N []*int32 } type B1 struct { A []string @@ -30,16 +33,21 @@ type B1 struct { I []*string K []C1 J []*C1 + L []int + M []int + N []int } type C1 struct { C1A string } func TestName(t *testing.T) { + var one A1 //要复制的 //one.C = []string{"1", "2"} //one.D = PStr("1") + one.D = "1" one.A = []string{"1", "2"} one.B = []C1{{C1A: "1"}, {C1A: "2"}} @@ -50,6 +58,12 @@ func TestName(t *testing.T) { one.I = []string{"1", "2"} one.K = []*C1{{C1A: "23"}} one.J = []C1{{C1A: "2323"}, {C1A: "2323"}} + one.L = []int32{1, 2, 3, 4} + one.M = []int64{1, 2, 3, 4} + one.N = []*int32{ + PInt32(1), + PInt32(2), + } var two B1 //被赋值的 err := StructCopy(&two, one, 1<<4) t.Log(err, two) @@ -59,3 +73,13 @@ func TestName(t *testing.T) { func PStr(a string) *string { return &a } + +func PInt(a int) *int { + return &a +} +func PInt64(a int64) *int64 { + return &a +} +func PInt32(a int32) *int32 { + return &a +}