fix(serialization): improve nil handling in mapUnmarshalValidate

This commit is contained in:
yusing
2025-09-13 23:20:10 +08:00
parent 0d2229cca0
commit 190d5e1ece

View File

@@ -189,15 +189,7 @@ func mapUnmarshalValidate(src SerializedObject, dst any, checkValidateTag bool)
dstV := reflect.ValueOf(dst)
dstT := dstV.Type()
if src == nil {
if dstV.CanSet() {
dstV.Set(reflect.Zero(dstT))
return nil
}
return gperr.Errorf("deserialize: src is %w and dst is not settable", ErrNilValue)
}
if dstT.Implements(mapUnmarshalerType) {
if src != nil && dstT.Implements(mapUnmarshalerType) {
dstV, _, err = dive(dstV)
if err != nil {
return err
@@ -210,6 +202,14 @@ func mapUnmarshalValidate(src SerializedObject, dst any, checkValidateTag bool)
return err
}
if src == nil {
if dstV.CanSet() {
dstV.Set(reflect.Zero(dstT))
return nil
}
return gperr.Errorf("deserialize: src is %w and dst is not settable", ErrNilValue)
}
// convert data fields to lower no-snake
// convert target fields to lower no-snake
// then check if the field of data is in the target