From ada2941e879079abc143998d1dd2dbcf6102f57b Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Fri, 30 Sep 2022 14:57:40 +0800 Subject: [PATCH] chore: sort methods (#2470) --- core/mapping/unmarshaler.go | 42 ++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index d8c71b7b..a4cdf3f6 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -205,10 +205,10 @@ func (u *Unmarshaler) processFieldNotFromString(field reflect.StructField, value return u.processFieldStruct(field, value, mapValue, fullName) case valueKind == reflect.Map && typeKind == reflect.Map: return u.fillMap(field, value, mapValue) - case valueKind == reflect.String && typeKind == reflect.Slice: - return u.fillSliceFromString(fieldType, value, mapValue) case valueKind == reflect.String && typeKind == reflect.Map: return u.fillMapFromString(value, mapValue) + case valueKind == reflect.String && typeKind == reflect.Slice: + return u.fillSliceFromString(fieldType, value, mapValue) case valueKind == reflect.String && derefedFieldType == durationType: return fillDurationValue(fieldType.Kind(), value, mapValue.(string)) default: @@ -444,6 +444,27 @@ func (u *Unmarshaler) fillMap(field reflect.StructField, value reflect.Value, ma return nil } +func (u *Unmarshaler) fillMapFromString(value reflect.Value, mapValue interface{}) error { + if !value.CanSet() { + return errValueNotSettable + } + + switch v := mapValue.(type) { + case fmt.Stringer: + if err := jsonx.UnmarshalFromString(v.String(), value.Addr().Interface()); err != nil { + return err + } + case string: + if err := jsonx.UnmarshalFromString(v, value.Addr().Interface()); err != nil { + return err + } + default: + return errUnsupportedType + } + + return nil +} + func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, mapValue interface{}) error { if !value.CanSet() { return errValueNotSettable @@ -532,23 +553,6 @@ func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect. return nil } -func (u *Unmarshaler) fillMapFromString(value reflect.Value, mapValue interface{}) error { - switch v := mapValue.(type) { - case fmt.Stringer: - if err := jsonx.UnmarshalFromString(v.String(), value.Addr().Interface()); err != nil { - return err - } - case string: - if err := jsonx.UnmarshalFromString(v, value.Addr().Interface()); err != nil { - return err - } - default: - return errUnsupportedType - } - - return nil -} - func (u *Unmarshaler) fillSliceValue(slice reflect.Value, index int, baseKind reflect.Kind, value interface{}) error { ithVal := slice.Index(index)