1
0
mirror of https://github.com/json-iterator/go.git synced 2025-08-07 21:52:55 +02:00

rewrite how eface and iface are handled

This commit is contained in:
Tao Wen
2018-02-21 12:16:50 +08:00
parent ea6403326b
commit 2fcbb23d96
13 changed files with 301 additions and 243 deletions

78
value_tests/eface_test.go Normal file
View File

@ -0,0 +1,78 @@
package test
func init() {
var pEFace = func(val interface{}) *interface{} {
return &val
}
var pInt = func(val int) *int {
return &val
}
unmarshalCases = append(unmarshalCases, unmarshalCase{
ptr: (**interface{})(nil),
input: `"hello"`,
}, unmarshalCase{
ptr: (**interface{})(nil),
input: `1e1`,
}, unmarshalCase{
ptr: (**interface{})(nil),
input: `1.0e1`,
}, unmarshalCase{
ptr: (*[]interface{})(nil),
input: `[1.0e1]`,
}, unmarshalCase{
ptr: (*struct {
Field interface{}
})(nil),
input: `{"field":"hello"}`,
}, unmarshalCase{
obj: func() interface{} {
type TestData struct {
Name string `json:"name"`
}
o := &TestData{}
return &o
},
input: `{"name":"value"}`,
}, unmarshalCase{
obj: func() interface{} {
b := true
return &struct {
Field interface{} `json:"field"`
}{&b}
},
input: `{"field": null}`,
}, unmarshalCase{
obj: func() interface{} {
var pb *bool
return &struct {
Field interface{} `json:"field"`
}{&pb}
},
input: `{"field": null}`,
}, unmarshalCase{
obj: func() interface{} {
b := true
pb := &b
return &struct {
Field interface{} `json:"field"`
}{&pb}
},
input: `{"field": null}`,
})
marshalCases = append(marshalCases,
pEFace("hello"),
struct {
Field interface{}
}{"hello"},
struct {
Field interface{}
}{struct{
field chan int
}{}},
struct {
Field interface{}
}{struct{
Field *int
}{pInt(100)}},
)
}

45
value_tests/iface_test.go Normal file
View File

@ -0,0 +1,45 @@
package test
import "io"
func init() {
var pCloser1 = func(str string) *io.Closer {
closer := io.Closer(strCloser1(str))
return &closer
}
var pCloser2 = func(str string) *io.Closer {
strCloser := strCloser2(str)
closer := io.Closer(&strCloser)
return &closer
}
marshalCases = append(marshalCases,
pCloser1("hello"),
pCloser2("hello"),
)
unmarshalCases = append(unmarshalCases, unmarshalCase{
ptr: (*[]io.Closer)(nil),
input: "[null]",
}, unmarshalCase{
obj: func() interface{} {
strCloser := strCloser2("")
return &struct {
Field io.Closer
}{
&strCloser,
}
},
input: `{"Field": "hello"}`,
})
}
type strCloser1 string
func (closer strCloser1) Close() error {
return nil
}
type strCloser2 string
func (closer *strCloser2) Close() error {
return nil
}

View File

@ -1,25 +1,39 @@
package test
func init() {
var pEFace = func(val interface{}) *interface{} {
return &val
}
var pInt = func(val int) *int {
return &val
}
unmarshalCases = append(unmarshalCases, unmarshalCase{
ptr: (**interface{})(nil),
input: `"hello"`,
}, unmarshalCase{
ptr: (**interface{})(nil),
input: `1e1`,
}, unmarshalCase{
ptr: (**interface{})(nil),
input: `1.0e1`,
})
marshalCases = append(marshalCases,
pEFace("hello"),
(*int)(nil),
pInt(100),
)
unmarshalCases = append(unmarshalCases, unmarshalCase{
obj: func() interface{} {
var i int
return &i
},
input: "null",
}, unmarshalCase{
obj: func() interface{} {
var i *int
return &i
},
input: "10",
}, unmarshalCase{
obj: func() interface{} {
var i int
pi := &i
return &pi
},
input: "null",
}, unmarshalCase{
obj: func() interface{} {
var i int
pi := &i
ppi := &pi
return &ppi
},
input: "null",
})
}

View File

@ -10,8 +10,10 @@ import (
)
type unmarshalCase struct {
obj func() interface{}
ptr interface{}
input string
selected bool
}
var unmarshalCases []unmarshalCase
@ -25,16 +27,31 @@ type selectedMarshalCase struct {
}
func Test_unmarshal(t *testing.T) {
should := require.New(t)
for _, testCase := range unmarshalCases {
valType := reflect.TypeOf(testCase.ptr).Elem()
ptr1Val := reflect.New(valType)
err1 := json.Unmarshal([]byte(testCase.input), ptr1Val.Interface())
should.NoError(err1)
ptr2Val := reflect.New(valType)
err2 := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(testCase.input), ptr2Val.Interface())
should.NoError(err2)
should.Equal(ptr1Val.Interface(), ptr2Val.Interface())
if testCase.selected {
unmarshalCases = []unmarshalCase{testCase}
break
}
}
for i, testCase := range unmarshalCases {
t.Run(fmt.Sprintf("[%v]%s", i, testCase.input), func(t *testing.T) {
should := require.New(t)
var obj1 interface{}
var obj2 interface{}
if testCase.obj != nil {
obj1 = testCase.obj()
obj2 = testCase.obj()
} else {
valType := reflect.TypeOf(testCase.ptr).Elem()
obj1 = reflect.New(valType).Interface()
obj2 = reflect.New(valType).Interface()
}
err1 := json.Unmarshal([]byte(testCase.input), obj1)
should.NoError(err1, "json")
err2 := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(testCase.input), obj2)
should.NoError(err2, "jsoniter")
should.Equal(obj1, obj2)
})
}
}