diff --git a/toolkit/copier/copier.go b/toolkit/copier/copier.go index 7e532a47..da0a9fee 100644 --- a/toolkit/copier/copier.go +++ b/toolkit/copier/copier.go @@ -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()) { diff --git a/toolkit/copier/copier_test.go b/toolkit/copier/copier_test.go index f55e7631..73eba1df 100644 --- a/toolkit/copier/copier_test.go +++ b/toolkit/copier/copier_test.go @@ -171,7 +171,7 @@ 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() @@ -179,8 +179,8 @@ func TestDeepCopy3(t *testing.T) { //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 {