From 05737f6519c2c7545be04a77065ec03f79756e5b Mon Sep 17 00:00:00 2001 From: Halo <1447191636@qq.com> Date: Thu, 29 Sep 2022 22:34:58 +0800 Subject: [PATCH] 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 --- core/mapping/unmarshaler.go | 19 ++++++++++++++++++ core/mapping/unmarshaler_test.go | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index 497a9db8..d8c71b7b 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -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) diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index 2efed625..9e91432d 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -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"`