chore: refactoring mapping string to slice (#1959)
This commit is contained in:
@@ -496,17 +496,20 @@ func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, map
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.Value, mapValue interface{}) error {
|
func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.Value,
|
||||||
|
mapValue interface{}) error {
|
||||||
var slice []interface{}
|
var slice []interface{}
|
||||||
switch v := mapValue.(type) {
|
switch v := mapValue.(type) {
|
||||||
case json.Number:
|
case fmt.Stringer:
|
||||||
if err := jsonx.UnmarshalFromString(v.String(), &slice); err != nil {
|
if err := jsonx.UnmarshalFromString(v.String(), &slice); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
default:
|
case string:
|
||||||
if err := jsonx.UnmarshalFromString(mapValue.(string), &slice); err != nil {
|
if err := jsonx.UnmarshalFromString(v, &slice); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
return errUnsupportedType
|
||||||
}
|
}
|
||||||
|
|
||||||
baseFieldType := Deref(fieldType.Elem())
|
baseFieldType := Deref(fieldType.Elem())
|
||||||
|
|||||||
@@ -2777,6 +2777,36 @@ func TestUnmarshalJsonReaderComplex(t *testing.T) {
|
|||||||
assert.Equal(t, "txt", req.Txt)
|
assert.Equal(t, "txt", req.Txt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalJsonReaderArrayBool(t *testing.T) {
|
||||||
|
payload := `{"id": false}`
|
||||||
|
var res struct {
|
||||||
|
ID []string `json:"id"`
|
||||||
|
}
|
||||||
|
reader := strings.NewReader(payload)
|
||||||
|
err := UnmarshalJsonReader(reader, &res)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalJsonReaderArrayInt(t *testing.T) {
|
||||||
|
payload := `{"id": 123}`
|
||||||
|
var res struct {
|
||||||
|
ID []string `json:"id"`
|
||||||
|
}
|
||||||
|
reader := strings.NewReader(payload)
|
||||||
|
err := UnmarshalJsonReader(reader, &res)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalJsonReaderArrayString(t *testing.T) {
|
||||||
|
payload := `{"id": "123"}`
|
||||||
|
var res struct {
|
||||||
|
ID []string `json:"id"`
|
||||||
|
}
|
||||||
|
reader := strings.NewReader(payload)
|
||||||
|
err := UnmarshalJsonReader(reader, &res)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkDefaultValue(b *testing.B) {
|
func BenchmarkDefaultValue(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
var a struct {
|
var a struct {
|
||||||
@@ -2789,14 +2819,3 @@ func BenchmarkDefaultValue(b *testing.B) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshalJsonReaderArray(t *testing.T) {
|
|
||||||
payload := "{\"id\": 123}"
|
|
||||||
var res struct {
|
|
||||||
ID []string `json:"id"`
|
|
||||||
}
|
|
||||||
reader := strings.NewReader(payload)
|
|
||||||
err := UnmarshalJsonReader(reader, &res)
|
|
||||||
assert.Nil(t, err)
|
|
||||||
assert.Equal(t, 1, len(res.ID))
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user