mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-11 00:40:00 +08:00
chore: refactor mapping unmarshaler (#4145)
This commit is contained in:
@@ -5761,7 +5761,7 @@ func TestUnmarshalWithIgnoreFields(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshal_JsonUnmarshaler(t *testing.T) {
|
||||
func TestUnmarshal_Unmarshaler(t *testing.T) {
|
||||
t.Run("success", func(t *testing.T) {
|
||||
v := struct {
|
||||
Foo *mockUnmarshaler `json:"name"`
|
||||
@@ -5778,6 +5778,30 @@ func TestUnmarshal_JsonUnmarshaler(t *testing.T) {
|
||||
body := `{"name": "hello"}`
|
||||
assert.Error(t, UnmarshalJsonBytes([]byte(body), &v))
|
||||
})
|
||||
|
||||
t.Run("not json unmarshaler", func(t *testing.T) {
|
||||
v := struct {
|
||||
Foo *struct {
|
||||
Name string
|
||||
} `key:"name"`
|
||||
}{}
|
||||
u := NewUnmarshaler(defaultKeyName)
|
||||
assert.Error(t, u.Unmarshal(map[string]any{
|
||||
"name": "hello",
|
||||
}, &v))
|
||||
})
|
||||
|
||||
t.Run("not with json key", func(t *testing.T) {
|
||||
v := struct {
|
||||
Foo *mockUnmarshaler `json:"name"`
|
||||
}{}
|
||||
u := NewUnmarshaler(defaultKeyName)
|
||||
// with different key, ignore
|
||||
assert.NoError(t, u.Unmarshal(map[string]any{
|
||||
"name": "hello",
|
||||
}, &v))
|
||||
assert.Nil(t, v.Foo)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkDefaultValue(b *testing.B) {
|
||||
|
||||
Reference in New Issue
Block a user