Skip to content

Commit

Permalink
copier DeepCopy
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin1989 committed Aug 21, 2024
1 parent 6f2a328 commit e390e32
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
60 changes: 60 additions & 0 deletions toolkit/copier/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package copier
import (
"bytes"
"fmt"
"github.com/spf13/cast"
"reflect"
"strings"

Expand Down Expand Up @@ -110,6 +111,65 @@ func setStructField(structObj any, fieldName string, fieldValue any) error {
}
}

if val.Kind() == reflect.String {
switch fieldVal.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldVal.SetInt(cast.ToInt64(val.String()))
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fieldVal.SetUint(cast.ToUint64(val.String()))
return nil
case reflect.Float32, reflect.Float64:
fieldVal.SetFloat(cast.ToFloat64(val.String()))
return nil
case reflect.Ptr:
v := reflect.New(fieldVal.Type().Elem())
switch fieldVal.Type().Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v.Elem().SetInt(cast.ToInt64(val.String()))
fieldVal.Set(v)
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v.Elem().SetUint(cast.ToUint64(val.String()))
fieldVal.Set(v)
return nil
case reflect.Float32, reflect.Float64:
v.Elem().SetFloat(cast.ToFloat64(val.String()))
fieldVal.Set(v)
return nil
}
}
} else if val.Kind() == reflect.Ptr && val.Type().Elem().Kind() == reflect.String {
underlyingV := val.Elem().String()
switch fieldVal.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldVal.SetInt(cast.ToInt64(underlyingV))
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fieldVal.SetUint(cast.ToUint64(underlyingV))
return nil
case reflect.Float32, reflect.Float64:
fieldVal.SetFloat(cast.ToFloat64(underlyingV))
return nil
case reflect.Ptr:
v := reflect.New(fieldVal.Type().Elem())
switch fieldVal.Type().Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v.Elem().SetInt(cast.ToInt64(underlyingV))
fieldVal.Set(v)
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v.Elem().SetUint(cast.ToUint64(underlyingV))
fieldVal.Set(v)
return nil
case reflect.Float32, reflect.Float64:
v.Elem().SetFloat(cast.ToFloat64(underlyingV))
fieldVal.Set(v)
return nil
}
}
}

if m, ok := fieldValue.(map[string]any); ok {

if fieldVal.Kind() == reflect.Struct {
Expand Down
23 changes: 23 additions & 0 deletions toolkit/copier/copier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,26 @@ func TestDeepCopy3(t *testing.T) {
//{jack 18}

}

func TestDeepCopy4(t *testing.T) {
//t1 := `{"name":"jack", "age": 18.0, "school": "beijing"}`
p := make(map[string]interface{})
p["name"] = nil
p["age"] = "18"
type Student struct {
Name *string `json:"name"`
Age *int64 `json:"age,string"`
}
var s Student
if err := DeepCopy(p, &s); err != nil {
panic(err)
}

fmt.Println(p)
fmt.Println(s)

// Output:
// {jack 18}
//{jack 18}

}

0 comments on commit e390e32

Please sign in to comment.