Skip to content

Commit

Permalink
feat: add string to map in httpx parse method (#2459)
Browse files Browse the repository at this point in the history
* chore: add string to map in httpx parse method

* feat: add httpx parse stringToMap method test
  • Loading branch information
pipi-lv authored Sep 29, 2022
1 parent 4f6a900 commit 05737f6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
19 changes: 19 additions & 0 deletions core/mapping/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions core/mapping/unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 05737f6

Please sign in to comment.