Procházet zdrojové kódy

unmarshal should be struct

kevin před 4 roky
rodič
revize
e5d7c3ab04
2 změnil soubory, kde provedl 12 přidání a 0 odebrání
  1. 5 0
      core/mapping/unmarshaler.go
  2. 7 0
      core/mapping/unmarshaler_test.go

+ 5 - 0
core/mapping/unmarshaler.go

@@ -23,6 +23,7 @@ const (
 var (
 var (
 	errTypeMismatch     = errors.New("type mismatch")
 	errTypeMismatch     = errors.New("type mismatch")
 	errValueNotSettable = errors.New("value is not settable")
 	errValueNotSettable = errors.New("value is not settable")
+	errValueNotStruct   = errors.New("value type is not struct")
 	keyUnmarshaler      = NewUnmarshaler(defaultKeyName)
 	keyUnmarshaler      = NewUnmarshaler(defaultKeyName)
 	cacheKeys           atomic.Value
 	cacheKeys           atomic.Value
 	cacheKeysLock       sync.Mutex
 	cacheKeysLock       sync.Mutex
@@ -80,6 +81,10 @@ func (u *Unmarshaler) unmarshalWithFullName(m Valuer, v interface{}, fullName st
 	}
 	}
 
 
 	rte := reflect.TypeOf(v).Elem()
 	rte := reflect.TypeOf(v).Elem()
+	if rte.Kind() != reflect.Struct {
+		return errValueNotStruct
+	}
+
 	rve := rv.Elem()
 	rve := rv.Elem()
 	numFields := rte.NumField()
 	numFields := rte.NumField()
 	for i := 0; i < numFields; i++ {
 	for i := 0; i < numFields; i++ {

+ 7 - 0
core/mapping/unmarshaler_test.go

@@ -14,6 +14,13 @@ import (
 // so we only can test to 62 bits.
 // so we only can test to 62 bits.
 const maxUintBitsToTest = 62
 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) {
 func TestUnmarshalWithoutTagName(t *testing.T) {
 	type inner struct {
 	type inner struct {
 		Optional bool `key:",optional"`
 		Optional bool `key:",optional"`