Skip to content

Commit

Permalink
copier DeepCopy
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin48435 committed Aug 21, 2024
1 parent d4ab04d commit 6f2a328
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions toolkit/copier/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func setStructField(structObj any, fieldName string, fieldValue any) error {

val := reflect.ValueOf(fieldValue)

if !val.IsValid() {
return nil
}

if fieldVal.Type() != val.Type() {

if val.CanConvert(fieldVal.Type()) {
Expand Down
6 changes: 3 additions & 3 deletions toolkit/copier/copier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,16 @@ func TestDeepCopy2(t *testing.T) {
func TestDeepCopy3(t *testing.T) {
//t1 := `{"name":"jack", "age": 18.0, "school": "beijing"}`
p := make(map[string]interface{})
p["name"] = lo.ToPtr("jack")
p["name"] = nil
p["age"] = lo.ToPtr(18)
//dec := decoder.NewDecoder(t1)
//dec.UseInt64()
//dec.Decode(&p)
//ddd, _ := json.Marshal(p)
//fmt.Println(string(ddd))
type Student struct {
Name string `json:"name"`
Age int `json:"age,string"`
Name *string `json:"name"`
Age *int `json:"age,string"`
}
var s Student
if err := DeepCopy(p, &s); err != nil {
Expand Down

0 comments on commit 6f2a328

Please sign in to comment.