From e5d7c3ab0444393ace95435a6d7eb02a7030093b Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 28 Sep 2020 15:19:30 +0800 Subject: [PATCH] unmarshal should be struct --- core/mapping/unmarshaler.go | 5 +++++ core/mapping/unmarshaler_test.go | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index a3d0d614..409be72b 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -23,6 +23,7 @@ const ( var ( errTypeMismatch = errors.New("type mismatch") errValueNotSettable = errors.New("value is not settable") + errValueNotStruct = errors.New("value type is not struct") keyUnmarshaler = NewUnmarshaler(defaultKeyName) cacheKeys atomic.Value cacheKeysLock sync.Mutex @@ -80,6 +81,10 @@ func (u *Unmarshaler) unmarshalWithFullName(m Valuer, v interface{}, fullName st } rte := reflect.TypeOf(v).Elem() + if rte.Kind() != reflect.Struct { + return errValueNotStruct + } + rve := rv.Elem() numFields := rte.NumField() for i := 0; i < numFields; i++ { diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index 6ef7886f..da23d599 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -14,6 +14,13 @@ import ( // so we only can test to 62 bits. const maxUintBitsToTest = 62 +func TestUnmarshalWithFullNameNotStruct(t *testing.T) { + var s map[string]interface{} + content := []byte(`{"name":"xiaoming"}`) + err := UnmarshalJsonBytes(content, &s) + assert.Equal(t, errValueNotStruct, err) +} + func TestUnmarshalWithoutTagName(t *testing.T) { type inner struct { Optional bool `key:",optional"`