feat: add string to map in httpx parse method (#2459)

* chore: add string to map in httpx parse method

* feat: add httpx parse stringToMap method test
master
Halo 2 years ago committed by GitHub
parent 4f6a900fd4
commit 05737f6519
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -207,6 +207,8 @@ func (u *Unmarshaler) processFieldNotFromString(field reflect.StructField, value
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 && derefedFieldType == durationType:
return fillDurationValue(fieldType.Kind(), value, mapValue.(string))
default:
@ -530,6 +532,23 @@ 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)

@ -467,6 +467,39 @@ func TestUnmarshalIntSliceFromString(t *testing.T) {
ast.Equal(2, v.Values[1])
}
func TestUnmarshalStringMapFromString(t *testing.T) {
var v struct {
Sort map[string]string `key:"sort"`
}
m := map[string]interface{}{
"sort": `{"value":"ascend","emptyStr":""}`,
}
ast := assert.New(t)
ast.Nil(UnmarshalKey(m, &v))
ast.Equal(2, len(v.Sort))
ast.Equal("ascend", v.Sort["value"])
ast.Equal("", v.Sort["emptyStr"])
}
func TestUnmarshalStringSliceMapFromString(t *testing.T) {
var v struct {
Filter map[string][]string `key:"filter"`
}
m := map[string]interface{}{
"filter": `{"assignType":null,"status":["process","comment"],"rate":[]}`,
}
ast := assert.New(t)
ast.Nil(UnmarshalKey(m, &v))
ast.Equal(3, len(v.Filter))
ast.Equal([]string(nil), v.Filter["assignType"])
ast.Equal(2, len(v.Filter["status"]))
ast.Equal("process", v.Filter["status"][0])
ast.Equal("comment", v.Filter["status"][1])
ast.Equal(0, len(v.Filter["rate"]))
}
func TestUnmarshalStruct(t *testing.T) {
type address struct {
City string `key:"city"`

Loading…
Cancel
Save