mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
feat/conf_map_required (#4405)
Co-authored-by: aiden.ma <Aiden.ma@yijinin.com>
This commit is contained in:
parent
11c47d23df
commit
12071d17b4
@ -919,12 +919,16 @@ func (u *Unmarshaler) processNamedFieldWithoutValue(fieldType reflect.Type, valu
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch fieldKind {
|
switch fieldKind {
|
||||||
case reflect.Array, reflect.Map, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
if !opts.optional() {
|
if !opts.optional() {
|
||||||
return u.processFieldNotFromString(fieldType, value, valueWithParent{
|
return u.processFieldNotFromString(fieldType, value, valueWithParent{
|
||||||
value: emptyMap,
|
value: emptyMap,
|
||||||
}, opts, fullName)
|
}, opts, fullName)
|
||||||
}
|
}
|
||||||
|
case reflect.Map:
|
||||||
|
if !opts.optional() {
|
||||||
|
return newInitError(fullName)
|
||||||
|
}
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if !opts.optional() {
|
if !opts.optional() {
|
||||||
required, err := structValueRequired(u.key, derefedType)
|
required, err := structValueRequired(u.key, derefedType)
|
||||||
|
@ -2432,6 +2432,42 @@ func TestUnmarshalMapOfStruct(t *testing.T) {
|
|||||||
}
|
}
|
||||||
assert.Error(t, UnmarshalKey(m, &v))
|
assert.Error(t, UnmarshalKey(m, &v))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("map set", func(t *testing.T) {
|
||||||
|
type Inner1 struct {
|
||||||
|
M map[string]string
|
||||||
|
}
|
||||||
|
assert.Error(t, UnmarshalKey(map[string]any{}, &Inner1{}))
|
||||||
|
assert.NoError(t, UnmarshalKey(map[string]any{
|
||||||
|
"M": map[string]string{},
|
||||||
|
}, &Inner1{}))
|
||||||
|
|
||||||
|
type Inner2 struct {
|
||||||
|
Inner1
|
||||||
|
}
|
||||||
|
assert.Error(t, UnmarshalKey(map[string]any{}, &Inner2{}))
|
||||||
|
assert.NoError(t, UnmarshalKey(map[string]any{
|
||||||
|
"M": map[string]string{},
|
||||||
|
}, &Inner2{}))
|
||||||
|
|
||||||
|
type Inner3 struct {
|
||||||
|
C Inner1
|
||||||
|
}
|
||||||
|
assert.Error(t, UnmarshalKey(map[string]any{}, &Inner3{}))
|
||||||
|
assert.NoError(t, UnmarshalKey(map[string]any{
|
||||||
|
"C": map[string]any{
|
||||||
|
"M": map[string]string{},
|
||||||
|
},
|
||||||
|
}, &Inner3{}))
|
||||||
|
|
||||||
|
type Inner4 struct {
|
||||||
|
M map[string]string `json:",optional"`
|
||||||
|
}
|
||||||
|
assert.NoError(t, UnmarshalKey(map[string]any{}, &Inner4{}))
|
||||||
|
assert.NoError(t, UnmarshalKey(map[string]any{
|
||||||
|
"M": map[string]string{},
|
||||||
|
}, &Inner4{}))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshalSlice(t *testing.T) {
|
func TestUnmarshalSlice(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user