mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
feat: support query array in httpx.Parse (#4440)
This commit is contained in:
@@ -50,6 +50,7 @@ type (
|
||||
|
||||
unmarshalOptions struct {
|
||||
fillDefault bool
|
||||
fromArray bool
|
||||
fromString bool
|
||||
opaqueKeys bool
|
||||
canonicalKey func(key string) string
|
||||
@@ -811,6 +812,19 @@ func (u *Unmarshaler) processNamedField(field reflect.StructField, value reflect
|
||||
return u.processNamedFieldWithoutValue(field.Type, value, opts, fullName)
|
||||
}
|
||||
|
||||
if u.opts.fromArray {
|
||||
fieldKind := field.Type.Kind()
|
||||
if fieldKind != reflect.Slice && fieldKind != reflect.Array {
|
||||
valueKind := reflect.TypeOf(mapValue).Kind()
|
||||
if valueKind == reflect.Slice || valueKind == reflect.Array {
|
||||
val := reflect.ValueOf(mapValue)
|
||||
if val.Len() > 0 {
|
||||
mapValue = val.Index(0).Interface()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return u.processNamedFieldWithValue(field.Type, value, valueWithParent{
|
||||
value: mapValue,
|
||||
parent: valuer,
|
||||
@@ -990,6 +1004,16 @@ func WithDefault() UnmarshalOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithFromArray customizes an Unmarshaler with converting array values to non-array types.
|
||||
// For example, if the field type is []string, and the value is [hello],
|
||||
// the field type can be `string`, instead of `[]string`.
|
||||
// Typically, this option is used for unmarshaling from form values.
|
||||
func WithFromArray() UnmarshalOption {
|
||||
return func(opt *unmarshalOptions) {
|
||||
opt.fromArray = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithOpaqueKeys customizes an Unmarshaler with opaque keys.
|
||||
// Opaque keys are keys that are not processed by the unmarshaler.
|
||||
func WithOpaqueKeys() UnmarshalOption {
|
||||
|
||||
@@ -5639,6 +5639,62 @@ func TestUnmarshalFromStringSliceForTypeMismatch(t *testing.T) {
|
||||
}, &v))
|
||||
}
|
||||
|
||||
func TestUnmarshalWithFromArray(t *testing.T) {
|
||||
t.Run("array", func(t *testing.T) {
|
||||
var v struct {
|
||||
Value []string `key:"value"`
|
||||
}
|
||||
unmarshaler := NewUnmarshaler("key", WithFromArray())
|
||||
if assert.NoError(t, unmarshaler.Unmarshal(map[string]any{
|
||||
"value": []string{"foo", "bar"},
|
||||
}, &v)) {
|
||||
assert.ElementsMatch(t, []string{"foo", "bar"}, v.Value)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("not array", func(t *testing.T) {
|
||||
var v struct {
|
||||
Value string `key:"value"`
|
||||
}
|
||||
unmarshaler := NewUnmarshaler("key", WithFromArray())
|
||||
if assert.NoError(t, unmarshaler.Unmarshal(map[string]any{
|
||||
"value": []string{"foo"},
|
||||
}, &v)) {
|
||||
assert.Equal(t, "foo", v.Value)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("not array and empty", func(t *testing.T) {
|
||||
var v struct {
|
||||
Value string `key:"value"`
|
||||
}
|
||||
unmarshaler := NewUnmarshaler("key", WithFromArray())
|
||||
if assert.NoError(t, unmarshaler.Unmarshal(map[string]any{
|
||||
"value": []string{""},
|
||||
}, &v)) {
|
||||
assert.Empty(t, v.Value)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("not array and no value", func(t *testing.T) {
|
||||
var v struct {
|
||||
Value string `key:"value"`
|
||||
}
|
||||
unmarshaler := NewUnmarshaler("key", WithFromArray())
|
||||
assert.Error(t, unmarshaler.Unmarshal(map[string]any{}, &v))
|
||||
})
|
||||
|
||||
t.Run("not array and no value and optional", func(t *testing.T) {
|
||||
var v struct {
|
||||
Value string `key:"value,optional"`
|
||||
}
|
||||
unmarshaler := NewUnmarshaler("key", WithFromArray())
|
||||
if assert.NoError(t, unmarshaler.Unmarshal(map[string]any{}, &v)) {
|
||||
assert.Empty(t, v.Value)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestUnmarshalWithOpaqueKeys(t *testing.T) {
|
||||
var v struct {
|
||||
Opaque string `key:"opaque.key"`
|
||||
|
||||
Reference in New Issue
Block a user