From 7822a4c1cb2862e8883e68b7ea5bc61d00c3024a Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 13 Jan 2024 22:11:19 +0800 Subject: [PATCH] chore: refactor mapping errors (#3843) --- core/mapping/unmarshaler.go | 3 ++- core/mapping/unmarshaler_test.go | 2 +- core/mapping/utils.go | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index b06385b4..277edc5e 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -250,8 +250,9 @@ func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect. func (u *Unmarshaler) fillSliceValue(slice reflect.Value, index int, baseKind reflect.Kind, value any, fullName string) error { if value == nil { - return errUnsupportedType + return errNilSliceElement } + ithVal := slice.Index(index) switch v := value.(type) { case fmt.Stringer: diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index 18104537..d783670b 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -1340,7 +1340,7 @@ func TestUnmarshalNullableSlice(t *testing.T) { } ast := assert.New(t) - ast.Equal(UnmarshalKey(m, &v), errUnsupportedType) + ast.Equal(UnmarshalKey(m, &v), errNilSliceElement) } func TestUnmarshalIntSlice(t *testing.T) { diff --git a/core/mapping/utils.go b/core/mapping/utils.go index 67ea0ae3..d7c84af4 100644 --- a/core/mapping/utils.go +++ b/core/mapping/utils.go @@ -36,6 +36,7 @@ const ( var ( errUnsupportedType = errors.New("unsupported type on setting field value") errNumberRange = errors.New("wrong number range setting") + errNilSliceElement = errors.New("null element for slice") optionsCache = make(map[string]optionsCacheValue) cacheLock sync.RWMutex structRequiredCache = make(map[reflect.Type]requiredCacheValue)