Skip to content

Commit

Permalink
fix copy basicSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
quiet-xu committed Apr 10, 2023
1 parent 5a3fac1 commit f11f617
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 22 deletions.
58 changes: 36 additions & 22 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package struct_copy

import (
"errors"
"fmt"
"reflect"
)

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
//结构体赋值
Expand Down
24 changes: 24 additions & 0 deletions copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type A1 struct {
I []string
K []*C1
J []C1
L []int32
M []int64
N []*int32
}
type B1 struct {
A []string
Expand All @@ -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"}}
Expand All @@ -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)
Expand All @@ -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
}

0 comments on commit f11f617

Please sign in to comment.