fix: type matching supports string to int (#2038)

* fix: type matching supports string to int

* feat: type matching supports string to int

Co-authored-by: 程家福 <chengjiafu@uniontech.com>
This commit is contained in:
家福
2022-07-01 23:21:31 +08:00
committed by GitHub
parent 6a4885ba64
commit f3b8fef34f
5 changed files with 126 additions and 3 deletions

View File

@@ -2681,7 +2681,7 @@ func TestUnmarshalJsonReaderMultiArray(t *testing.T) {
assert.Equal(t, 2, len(res.B))
}
func TestUnmarshalJsonReaderPtrMultiArray(t *testing.T) {
func TestUnmarshalJsonReaderPtrMultiArrayString(t *testing.T) {
payload := `{"a": "133", "b": [["add", "cccd"], ["eeee"]]}`
var res struct {
A string `json:"a"`
@@ -2694,6 +2694,32 @@ func TestUnmarshalJsonReaderPtrMultiArray(t *testing.T) {
assert.Equal(t, 2, len(res.B[0]))
}
func TestUnmarshalJsonReaderPtrMultiArrayString_Int(t *testing.T) {
payload := `{"a": "133", "b": [[11, 22], [33]]}`
var res struct {
A string `json:"a"`
B [][]*string `json:"b"`
}
reader := strings.NewReader(payload)
err := UnmarshalJsonReader(reader, &res)
assert.Nil(t, err)
assert.Equal(t, 2, len(res.B))
assert.Equal(t, 2, len(res.B[0]))
}
func TestUnmarshalJsonReaderPtrMultiArrayInt(t *testing.T) {
payload := `{"a": "133", "b": [[11, 22], [33]]}`
var res struct {
A string `json:"a"`
B [][]*int `json:"b"`
}
reader := strings.NewReader(payload)
err := UnmarshalJsonReader(reader, &res)
assert.Nil(t, err)
assert.Equal(t, 2, len(res.B))
assert.Equal(t, 2, len(res.B[0]))
}
func TestUnmarshalJsonReaderPtrArray(t *testing.T) {
payload := `{"a": "133", "b": ["add", "cccd", "eeee"]}`
var res struct {
@@ -2706,6 +2732,30 @@ func TestUnmarshalJsonReaderPtrArray(t *testing.T) {
assert.Equal(t, 3, len(res.B))
}
func TestUnmarshalJsonReaderPtrArray_Int(t *testing.T) {
payload := `{"a": "133", "b": [11, 22, 33]}`
var res struct {
A string `json:"a"`
B []*string `json:"b"`
}
reader := strings.NewReader(payload)
err := UnmarshalJsonReader(reader, &res)
assert.Nil(t, err)
assert.Equal(t, 3, len(res.B))
}
func TestUnmarshalJsonReaderPtrInt(t *testing.T) {
payload := `{"a": "133", "b": [11, 22, 33]}`
var res struct {
A string `json:"a"`
B []*string `json:"b"`
}
reader := strings.NewReader(payload)
err := UnmarshalJsonReader(reader, &res)
assert.Nil(t, err)
assert.Equal(t, 3, len(res.B))
}
func TestUnmarshalJsonWithoutKey(t *testing.T) {
payload := `{"A": "1", "B": "2"}`
var res struct {