go-json/cover_string_test.go

2933 lines
50 KiB
Go
Raw Normal View History

2021-01-16 16:16:26 +03:00
package json_test
import (
"bytes"
"testing"
"github.com/goccy/go-json"
)
func TestCoverString(t *testing.T) {
type structString struct {
A string `json:"a"`
}
2021-01-20 18:22:20 +03:00
type structStringOmitEmpty struct {
A string `json:"a,omitempty"`
}
type structStringString struct {
A string `json:"a,string"`
}
2021-01-16 16:16:26 +03:00
type structStringPtr struct {
A *string `json:"a"`
}
2021-01-20 18:22:20 +03:00
type structStringPtrOmitEmpty struct {
A *string `json:"a,omitempty"`
}
type structStringPtrString struct {
A *string `json:"a,string"`
}
2021-01-16 16:16:26 +03:00
tests := []struct {
2021-01-20 18:22:20 +03:00
name string
expected string
indentExpected string
data interface{}
2021-01-16 16:16:26 +03:00
}{
2021-01-20 18:22:20 +03:00
// HeadStringZero
2021-01-16 16:16:26 +03:00
{
name: "HeadStringZero",
expected: `{"a":""}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": ""
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A string `json:"a"`
}{},
},
2021-01-20 18:22:20 +03:00
{
name: "HeadStringZeroOmitEmpty",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
A string `json:"a,omitempty"`
}{},
},
{
name: "HeadStringZeroString",
expected: `{"a":""}`,
indentExpected: `
{
"a": ""
}
`,
data: struct {
A string `json:"a,string"`
}{},
},
// HeadString
2021-01-16 16:16:26 +03:00
{
name: "HeadString",
expected: `{"a":"foo"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A string `json:"a"`
}{A: "foo"},
},
2021-01-20 18:22:20 +03:00
{
name: "HeadStringOmitEmpty",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: struct {
A string `json:"a,omitempty"`
}{A: "foo"},
},
{
name: "HeadStringString",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: struct {
A string `json:"a,string"`
}{A: "foo"},
},
// HeadStringPtr
2021-01-16 16:16:26 +03:00
{
name: "HeadStringPtr",
expected: `{"a":"foo"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A *string `json:"a"`
}{A: stringptr("foo")},
},
2021-01-20 18:22:20 +03:00
{
name: "HeadStringPtrOmitEmpty",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: struct {
A *string `json:"a,omitempty"`
}{A: stringptr("foo")},
},
{
name: "HeadStringPtrString",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: struct {
A *string `json:"a,string"`
}{A: stringptr("foo")},
},
// HeadStringPtrNil
2021-01-16 16:16:26 +03:00
{
name: "HeadStringPtrNil",
expected: `{"a":null}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": null
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A *string `json:"a"`
}{A: nil},
},
2021-01-20 18:22:20 +03:00
{
name: "HeadStringPtrNilOmitEmpty",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
A *string `json:"a,omitempty"`
}{A: nil},
},
{
name: "HeadStringPtrNilString",
expected: `{"a":null}`,
indentExpected: `
{
"a": null
}
`,
data: struct {
A *string `json:"a,string"`
}{A: nil},
},
// PtrHeadStringZero
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringZero",
expected: `{"a":""}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": ""
}
`,
2021-01-16 16:16:26 +03:00
data: &struct {
A string `json:"a"`
}{},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringZeroOmitEmpty",
expected: `{}`,
indentExpected: `
{}
`,
data: &struct {
A string `json:"a,omitempty"`
}{},
},
{
name: "PtrHeadStringZeroString",
expected: `{"a":""}`,
indentExpected: `
{
"a": ""
}
`,
data: &struct {
A string `json:"a,string"`
}{},
},
// PtrHeadString
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadString",
expected: `{"a":"foo"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo"
}
`,
2021-01-16 16:16:26 +03:00
data: &struct {
A string `json:"a"`
}{A: "foo"},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringOmitEmpty",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: &struct {
A string `json:"a,omitempty"`
}{A: "foo"},
},
{
name: "PtrHeadStringString",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: &struct {
A string `json:"a,string"`
}{A: "foo"},
},
// PtrHeadStringPtr
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringPtr",
expected: `{"a":"foo"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo"
}
`,
2021-01-16 16:16:26 +03:00
data: &struct {
A *string `json:"a"`
}{A: stringptr("foo")},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringPtrOmitEmpty",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: &struct {
A *string `json:"a,omitempty"`
}{A: stringptr("foo")},
},
{
name: "PtrHeadStringPtrString",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: &struct {
A *string `json:"a,string"`
}{A: stringptr("foo")},
},
// PtrHeadStringPtrNil
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringPtrNil",
expected: `{"a":null}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": null
}
`,
2021-01-16 16:16:26 +03:00
data: &struct {
A *string `json:"a"`
}{A: nil},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringPtrNilOmitEmpty",
expected: `{}`,
indentExpected: `
{}
`,
data: &struct {
A *string `json:"a,omitempty"`
}{A: nil},
},
{
name: "PtrHeadStringPtrNilString",
expected: `{"a":null}`,
indentExpected: `
{
"a": null
}
`,
data: &struct {
A *string `json:"a,string"`
}{A: nil},
},
// PtrHeadStringNil
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringNil",
expected: `null`,
2021-01-20 18:22:20 +03:00
indentExpected: `
null
`,
2021-01-16 16:16:26 +03:00
data: (*struct {
A *string `json:"a"`
})(nil),
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringNilOmitEmpty",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *string `json:"a,omitempty"`
})(nil),
},
{
name: "PtrHeadStringNilString",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *string `json:"a,string"`
})(nil),
},
// HeadStringZeroMultiFields
2021-01-16 16:16:26 +03:00
{
name: "HeadStringZeroMultiFields",
expected: `{"a":"","b":""}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "",
"b": ""
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A string `json:"a"`
B string `json:"b"`
}{},
},
2021-01-20 18:22:20 +03:00
{
name: "HeadStringZeroMultiFieldsOmitEmpty",
expected: `{"a":"","b":""}`,
indentExpected: `
{}
`,
data: struct {
A string `json:"a,omitempty"`
B string `json:"b,omitempty"`
}{},
},
{
name: "HeadStringZeroMultiFieldsString",
expected: `{"a":"","b":""}`,
indentExpected: `
{
"a": "",
"b": ""
}
`,
data: struct {
A string `json:"a,string"`
B string `json:"b,string"`
}{},
},
// HeadStringMultiFields
2021-01-16 16:16:26 +03:00
{
name: "HeadStringMultiFields",
expected: `{"a":"foo","b":"bar"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A string `json:"a"`
B string `json:"b"`
}{A: "foo", B: "bar"},
},
2021-01-20 18:22:20 +03:00
{
name: "HeadStringMultiFieldsOmitEmpty",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: struct {
A string `json:"a,omitempty"`
B string `json:"b,omitempty"`
}{A: "foo", B: "bar"},
},
{
name: "HeadStringMultiFieldsString",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: struct {
A string `json:"a,string"`
B string `json:"b,string"`
}{A: "foo", B: "bar"},
},
// HeadStringPtrMultiFields
2021-01-16 16:16:26 +03:00
{
name: "HeadStringPtrMultiFields",
expected: `{"a":"foo","b":"bar"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A *string `json:"a"`
B *string `json:"b"`
}{A: stringptr("foo"), B: stringptr("bar")},
},
2021-01-20 18:22:20 +03:00
{
name: "HeadStringPtrMultiFieldsOmitEmpty",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: struct {
A *string `json:"a,omitempty"`
B *string `json:"b,omitempty"`
}{A: stringptr("foo"), B: stringptr("bar")},
},
{
name: "HeadStringPtrMultiFieldsString",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: struct {
A *string `json:"a,string"`
B *string `json:"b,string"`
}{A: stringptr("foo"), B: stringptr("bar")},
},
// HeadStringPtrNilMultiFields
2021-01-16 16:16:26 +03:00
{
name: "HeadStringPtrNilMultiFields",
expected: `{"a":null,"b":null}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": null,
"b": null
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A *string `json:"a"`
B *string `json:"b"`
}{A: nil, B: nil},
},
2021-01-20 18:22:20 +03:00
{
name: "HeadStringPtrNilMultiFieldsOmitEmpty",
expected: `{"a":null,"b":null}`,
indentExpected: `
{}
`,
data: struct {
A *string `json:"a,omitempty"`
B *string `json:"b,omitempty"`
}{A: nil, B: nil},
},
{
name: "HeadStringPtrNilMultiFieldsString",
expected: `{"a":null,"b":null}`,
indentExpected: `
{
"a": null,
"b": null
}
`,
data: struct {
A *string `json:"a,string"`
B *string `json:"b,string"`
}{A: nil, B: nil},
},
// PtrHeadStringZeroMultiFields
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringZeroMultiFields",
expected: `{"a":"","b":""}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "",
"b": ""
}
`,
2021-01-16 16:16:26 +03:00
data: &struct {
A string `json:"a"`
B string `json:"b"`
}{},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringZeroMultiFieldsOmitEmpty",
expected: `{"a":"","b":""}`,
indentExpected: `
{}
`,
data: &struct {
A string `json:"a,omitempty"`
B string `json:"b,omitempty"`
}{},
},
{
name: "PtrHeadStringZeroMultiFieldsString",
expected: `{"a":"","b":""}`,
indentExpected: `
{
"a": "",
"b": ""
}
`,
data: &struct {
A string `json:"a,string"`
B string `json:"b,string"`
}{},
},
// PtrHeadStringMultiFields
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringMultiFields",
expected: `{"a":"foo","b":"bar"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
2021-01-16 16:16:26 +03:00
data: &struct {
A string `json:"a"`
B string `json:"b"`
}{A: "foo", B: "bar"},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringMultiFieldsOmitEmpty",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: &struct {
A string `json:"a,omitempty"`
B string `json:"b,omitempty"`
}{A: "foo", B: "bar"},
},
{
name: "PtrHeadStringMultiFieldsString",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: &struct {
A string `json:"a,string"`
B string `json:"b,string"`
}{A: "foo", B: "bar"},
},
// PtrHeadStringPtrMultiFields
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringPtrMultiFields",
expected: `{"a":"foo","b":"bar"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
2021-01-16 16:16:26 +03:00
data: &struct {
A *string `json:"a"`
B *string `json:"b"`
}{A: stringptr("foo"), B: stringptr("bar")},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringPtrMultiFieldsOmitEmpty",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: &struct {
A *string `json:"a,omitempty"`
B *string `json:"b,omitempty"`
}{A: stringptr("foo"), B: stringptr("bar")},
},
{
name: "PtrHeadStringPtrMultiFieldsString",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: &struct {
A *string `json:"a,string"`
B *string `json:"b,string"`
}{A: stringptr("foo"), B: stringptr("bar")},
},
// PtrHeadStringPtrNilMultiFields
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringPtrNilMultiFields",
expected: `{"a":null,"b":null}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": null,
"b": null
}
`,
2021-01-16 16:16:26 +03:00
data: &struct {
A *string `json:"a"`
B *string `json:"b"`
}{A: nil, B: nil},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringPtrNilMultiFieldsOmitEmpty",
expected: `{"a":null,"b":null}`,
indentExpected: `
{}
`,
data: &struct {
A *string `json:"a,omitempty"`
B *string `json:"b,omitempty"`
}{A: nil, B: nil},
},
{
name: "PtrHeadStringPtrNilMultiFieldsString",
expected: `{"a":null,"b":null}`,
indentExpected: `
{
"a": null,
"b": null
}
`,
data: &struct {
A *string `json:"a,string"`
B *string `json:"b,string"`
}{A: nil, B: nil},
},
// PtrHeadStringNilMultiFields
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringNilMultiFields",
expected: `null`,
2021-01-20 18:22:20 +03:00
indentExpected: `
null
`,
2021-01-16 16:16:26 +03:00
data: (*struct {
A *string `json:"a"`
B *string `json:"b"`
})(nil),
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringNilMultiFieldsOmitEmpty",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *string `json:"a,omitempty"`
B *string `json:"b,omitempty"`
})(nil),
},
{
name: "PtrHeadStringNilMultiFieldsString",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *string `json:"a,string"`
B *string `json:"b,string"`
})(nil),
},
// HeadStringZeroNotRoot
2021-01-16 16:16:26 +03:00
{
name: "HeadStringZeroNotRoot",
expected: `{"A":{"a":""}}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": {
"a": ""
}
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A struct {
A string `json:"a"`
}
}{},
},
{
2021-01-20 18:22:20 +03:00
name: "HeadStringZeroNotRootOmitEmpty",
expected: `{"A":{}}`,
indentExpected: `
{
"A": {}
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A struct {
2021-01-20 18:22:20 +03:00
A string `json:"a,omitempty"`
2021-01-16 16:16:26 +03:00
}
2021-01-20 18:22:20 +03:00
}{},
2021-01-16 16:16:26 +03:00
},
{
2021-01-20 18:22:20 +03:00
name: "HeadStringZeroNotRootString",
expected: `{"A":{"a":""}}`,
indentExpected: `
{
"A": {
"a": ""
}
}
`,
data: struct {
A struct {
A string `json:"a,string"`
}
}{},
},
// HeadStringNotRoot
{
name: "HeadStringNotRoot",
expected: `{"A":{"a":"foo"}}`,
indentExpected: `
{
"A": {
"a": "foo"
}
}
`,
data: struct {
A struct {
A string `json:"a"`
}
}{A: struct {
A string `json:"a"`
}{A: "foo"}},
},
{
name: "HeadStringNotRootOmitEmpty",
expected: `{"A":{"a":"foo"}}`,
indentExpected: `
{
"A": {
"a": "foo"
}
}
`,
data: struct {
A struct {
A string `json:"a,omitempty"`
}
}{A: struct {
A string `json:"a,omitempty"`
}{A: "foo"}},
},
{
name: "HeadStringNotRootString",
expected: `{"A":{"a":"foo"}}`,
indentExpected: `
{
"A": {
"a": "foo"
}
}
`,
data: struct {
A struct {
A string `json:"a,string"`
}
}{A: struct {
A string `json:"a,string"`
}{A: "foo"}},
},
// HeadStringPtrNotRoot
{
name: "HeadStringPtrNotRoot",
expected: `{"A":{"a":"foo"}}`,
indentExpected: `
{
"A": {
"a": "foo"
}
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A struct {
A *string `json:"a"`
}
}{A: struct {
A *string `json:"a"`
}{stringptr("foo")}},
},
2021-01-20 18:22:20 +03:00
{
name: "HeadStringPtrNotRootOmitEmpty",
expected: `{"A":{"a":"foo"}}`,
indentExpected: `
{
"A": {
"a": "foo"
}
}
`,
data: struct {
A struct {
A *string `json:"a,omitempty"`
}
}{A: struct {
A *string `json:"a,omitempty"`
}{stringptr("foo")}},
},
{
name: "HeadStringPtrNotRootString",
expected: `{"A":{"a":"foo"}}`,
indentExpected: `
{
"A": {
"a": "foo"
}
}
`,
data: struct {
A struct {
A *string `json:"a,string"`
}
}{A: struct {
A *string `json:"a,string"`
}{stringptr("foo")}},
},
// HeadStringPtrNilNotRoot
2021-01-16 16:16:26 +03:00
{
name: "HeadStringPtrNilNotRoot",
expected: `{"A":{"a":null}}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": {
"a": null
}
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A struct {
A *string `json:"a"`
}
}{},
},
2021-01-20 18:22:20 +03:00
{
name: "HeadStringPtrNilNotRootOmitEmpty",
expected: `{"A":{}}`,
indentExpected: `
{
"A": {}
}
`,
data: struct {
A struct {
A *string `json:"a,omitempty"`
}
}{},
},
{
name: "HeadStringPtrNilNotRootString",
expected: `{"A":{"a":null}}`,
indentExpected: `
{
"A": {
"a": null
}
}
`,
data: struct {
A struct {
A *string `json:"a,string"`
}
}{},
},
// PtrHeadStringZeroNotRoot
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringZeroNotRoot",
expected: `{"A":{"a":""}}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": {
"a": ""
}
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A *struct {
A string `json:"a"`
}
}{A: new(struct {
A string `json:"a"`
})},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringZeroNotRootOmitEmpty",
expected: `{"A":{}}`,
indentExpected: `
{
"A": {}
}
`,
data: struct {
A *struct {
A string `json:"a,omitempty"`
}
}{A: new(struct {
A string `json:"a,omitempty"`
})},
},
{
name: "PtrHeadStringZeroNotRootString",
expected: `{"A":{"a":""}}`,
indentExpected: `
{
"A": {
"a": ""
}
}
`,
data: struct {
A *struct {
A string `json:"a,string"`
}
}{A: new(struct {
A string `json:"a,string"`
})},
},
// PtrHeadStringNotRoot
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringNotRoot",
expected: `{"A":{"a":"foo"}}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": {
"a": "foo"
}
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A *struct {
A string `json:"a"`
}
}{A: &(struct {
A string `json:"a"`
}{A: "foo"})},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringNotRootOmitEmpty",
expected: `{"A":{"a":"foo"}}`,
indentExpected: `
{
"A": {
"a": "foo"
}
}
`,
data: struct {
A *struct {
A string `json:"a,omitempty"`
}
}{A: &(struct {
A string `json:"a,omitempty"`
}{A: "foo"})},
},
{
name: "PtrHeadStringNotRootString",
expected: `{"A":{"a":"foo"}}`,
indentExpected: `
{
"A": {
"a": "foo"
}
}
`,
data: struct {
A *struct {
A string `json:"a,string"`
}
}{A: &(struct {
A string `json:"a,string"`
}{A: "foo"})},
},
// PtrHeadStringPtrNotRoot
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringPtrNotRoot",
expected: `{"A":{"a":"foo"}}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": {
"a": "foo"
}
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A *struct {
A *string `json:"a"`
}
}{A: &(struct {
A *string `json:"a"`
}{A: stringptr("foo")})},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringPtrNotRootOmitEmpty",
expected: `{"A":{"a":"foo"}}`,
indentExpected: `
{
"A": {
"a": "foo"
}
}
`,
data: struct {
A *struct {
A *string `json:"a,omitempty"`
}
}{A: &(struct {
A *string `json:"a,omitempty"`
}{A: stringptr("foo")})},
},
{
name: "PtrHeadStringPtrNotRootString",
expected: `{"A":{"a":"foo"}}`,
indentExpected: `
{
"A": {
"a": "foo"
}
}
`,
data: struct {
A *struct {
A *string `json:"a,string"`
}
}{A: &(struct {
A *string `json:"a,string"`
}{A: stringptr("foo")})},
},
// PtrHeadStringPtrNilNotRoot
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringPtrNilNotRoot",
expected: `{"A":{"a":null}}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": {
"a": null
}
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A *struct {
A *string `json:"a"`
}
}{A: &(struct {
A *string `json:"a"`
}{A: nil})},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringPtrNilNotRootOmitEmpty",
expected: `{"A":{}}`,
indentExpected: `
{
"A": {}
}
`,
data: struct {
A *struct {
A *string `json:"a,omitempty"`
}
}{A: &(struct {
A *string `json:"a,omitempty"`
}{A: nil})},
},
{
name: "PtrHeadStringPtrNilNotRootString",
expected: `{"A":{"a":null}}`,
indentExpected: `
{
"A": {
"a": null
}
}
`,
data: struct {
A *struct {
A *string `json:"a,string"`
}
}{A: &(struct {
A *string `json:"a,string"`
}{A: nil})},
},
// PtrHeadStringNilNotRoot
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringNilNotRoot",
expected: `{"A":null}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": null
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A *struct {
A *string `json:"a"`
}
}{A: nil},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringNilNotRootOmitEmpty",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
A *struct {
A *string `json:"a,omitempty"`
} `json:",omitempty"`
}{A: nil},
},
{
name: "PtrHeadStringNilNotRootString",
expected: `{"A":null}`,
indentExpected: `
{
"A": null
}
`,
data: struct {
A *struct {
A *string `json:"a,string"`
} `json:",string"`
}{A: nil},
},
// HeadStringZeroMultiFieldsNotRoot
2021-01-16 16:16:26 +03:00
{
name: "HeadStringZeroMultiFieldsNotRoot",
expected: `{"A":{"a":""},"B":{"b":""}}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": {
"a": ""
},
"B": {
"b": ""
}
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A struct {
A string `json:"a"`
}
B struct {
B string `json:"b"`
}
}{},
},
2021-01-20 18:22:20 +03:00
{
name: "HeadStringZeroMultiFieldsNotRootOmitEmpty",
expected: `{"A":{},"B":{}}`,
indentExpected: `
{
"A": {},
"B": {}
}
`,
data: struct {
A struct {
A string `json:"a,omitempty"`
}
B struct {
B string `json:"b,omitempty"`
}
}{},
},
{
name: "HeadStringZeroMultiFieldsNotRootString",
expected: `{"A":{"a":""},"B":{"b":""}}`,
indentExpected: `
{
"A": {
"a": ""
},
"B": {
"b": ""
}
}
`,
data: struct {
A struct {
A string `json:"a,string"`
}
B struct {
B string `json:"b,string"`
}
}{},
},
// HeadStringMultiFieldsNotRoot
2021-01-16 16:16:26 +03:00
{
name: "HeadStringMultiFieldsNotRoot",
expected: `{"A":{"a":"foo"},"B":{"b":"bar"}}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": {
"a": "foo"
},
"B": {
"b": "bar"
}
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A struct {
A string `json:"a"`
}
B struct {
B string `json:"b"`
}
}{A: struct {
A string `json:"a"`
}{A: "foo"}, B: struct {
B string `json:"b"`
}{B: "bar"}},
},
2021-01-20 18:22:20 +03:00
{
name: "HeadStringMultiFieldsNotRootOmitEmpty",
expected: `{"A":{"a":"foo"},"B":{"b":"bar"}}`,
indentExpected: `
{
"A": {
"a": "foo"
},
"B": {
"b": "bar"
}
}
`,
data: struct {
A struct {
A string `json:"a,omitempty"`
}
B struct {
B string `json:"b,omitempty"`
}
}{A: struct {
A string `json:"a,omitempty"`
}{A: "foo"}, B: struct {
B string `json:"b,omitempty"`
}{B: "bar"}},
},
{
name: "HeadStringMultiFieldsNotRootString",
expected: `{"A":{"a":"foo"},"B":{"b":"bar"}}`,
indentExpected: `
{
"A": {
"a": "foo"
},
"B": {
"b": "bar"
}
}
`,
data: struct {
A struct {
A string `json:"a,string"`
}
B struct {
B string `json:"b,string"`
}
}{A: struct {
A string `json:"a,string"`
}{A: "foo"}, B: struct {
B string `json:"b,string"`
}{B: "bar"}},
},
// HeadStringPtrMultiFieldsNotRoot
2021-01-16 16:16:26 +03:00
{
name: "HeadStringPtrMultiFieldsNotRoot",
expected: `{"A":{"a":"foo"},"B":{"b":"bar"}}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": {
"a": "foo"
},
"B": {
"b": "bar"
}
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A struct {
A *string `json:"a"`
}
B struct {
B *string `json:"b"`
}
}{A: struct {
A *string `json:"a"`
}{A: stringptr("foo")}, B: struct {
B *string `json:"b"`
}{B: stringptr("bar")}},
},
2021-01-20 18:22:20 +03:00
{
name: "HeadStringPtrMultiFieldsNotRootOmitEmpty",
expected: `{"A":{"a":"foo"},"B":{"b":"bar"}}`,
indentExpected: `
{
"A": {
"a": "foo"
},
"B": {
"b": "bar"
}
}
`,
data: struct {
A struct {
A *string `json:"a,omitempty"`
}
B struct {
B *string `json:"b,omitempty"`
}
}{A: struct {
A *string `json:"a,omitempty"`
}{A: stringptr("foo")}, B: struct {
B *string `json:"b,omitempty"`
}{B: stringptr("bar")}},
},
{
name: "HeadStringPtrMultiFieldsNotRootString",
expected: `{"A":{"a":"foo"},"B":{"b":"bar"}}`,
indentExpected: `
{
"A": {
"a": "foo"
},
"B": {
"b": "bar"
}
}
`,
data: struct {
A struct {
A *string `json:"a,string"`
}
B struct {
B *string `json:"b,string"`
}
}{A: struct {
A *string `json:"a,string"`
}{A: stringptr("foo")}, B: struct {
B *string `json:"b,string"`
}{B: stringptr("bar")}},
},
// HeadStringPtrNilMultiFieldsNotRoot
2021-01-16 16:16:26 +03:00
{
name: "HeadStringPtrNilMultiFieldsNotRoot",
expected: `{"A":{"a":null},"B":{"b":null}}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": {
"a": null
},
"B": {
"b": null
}
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
A struct {
A *string `json:"a"`
}
B struct {
B *string `json:"b"`
}
}{A: struct {
A *string `json:"a"`
}{A: nil}, B: struct {
B *string `json:"b"`
}{B: nil}},
},
{
2021-01-20 18:22:20 +03:00
name: "HeadStringPtrNilMultiFieldsNotRootOmitEmpty",
expected: `{"A":{"a":null},"B":{"b":null}}`,
indentExpected: `
{
"A": {},
"B": {}
}
`,
data: struct {
2021-01-16 16:16:26 +03:00
A struct {
2021-01-20 18:22:20 +03:00
A *string `json:"a,omitempty"`
2021-01-16 16:16:26 +03:00
}
B struct {
2021-01-20 18:22:20 +03:00
B *string `json:"b,omitempty"`
2021-01-16 16:16:26 +03:00
}
2021-01-20 18:22:20 +03:00
}{A: struct {
A *string `json:"a,omitempty"`
}{A: nil}, B: struct {
B *string `json:"b,omitempty"`
}{B: nil}},
},
{
name: "HeadStringPtrNilMultiFieldsNotRootString",
expected: `{"A":{"a":null},"B":{"b":null}}`,
indentExpected: `
{
"A": {
"a": null
},
"B": {
"b": null
}
}
`,
data: struct {
A struct {
A *string `json:"a,string"`
}
B struct {
B *string `json:"b,string"`
}
}{A: struct {
A *string `json:"a,string"`
}{A: nil}, B: struct {
B *string `json:"b,string"`
}{B: nil}},
},
// PtrHeadStringZeroMultiFieldsNotRoot
{
name: "PtrHeadStringZeroMultiFieldsNotRoot",
expected: `{"A":{"a":""},"B":{"b":""}}`,
indentExpected: `
{
"A": {
"a": ""
},
"B": {
"b": ""
}
}
`,
data: &struct {
A struct {
A string `json:"a"`
}
B struct {
B string `json:"b"`
}
}{},
},
{
name: "PtrHeadStringZeroMultiFieldsNotRootOmitEmpty",
expected: `{"A":{},"B":{}}`,
indentExpected: `
{
"A": {},
"B": {}
}
`,
data: &struct {
A struct {
A string `json:"a,omitempty"`
}
B struct {
B string `json:"b,omitempty"`
}
}{},
},
{
name: "PtrHeadStringZeroMultiFieldsNotRootString",
expected: `{"A":{"a":""},"B":{"b":""}}`,
indentExpected: `
{
"A": {
"a": ""
},
"B": {
"b": ""
}
}
`,
data: &struct {
A struct {
A string `json:"a,string"`
}
B struct {
B string `json:"b,string"`
}
}{},
},
// PtrHeadStringMultiFieldsNotRoot
{
2021-01-16 16:16:26 +03:00
name: "PtrHeadStringMultiFieldsNotRoot",
expected: `{"A":{"a":"foo"},"B":{"b":"bar"}}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": {
"a": "foo"
},
"B": {
"b": "bar"
}
}
`,
2021-01-16 16:16:26 +03:00
data: &struct {
A struct {
A string `json:"a"`
}
B struct {
B string `json:"b"`
}
}{A: struct {
A string `json:"a"`
}{A: "foo"}, B: struct {
B string `json:"b"`
}{B: "bar"}},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringMultiFieldsNotRootOmitEmpty",
expected: `{"A":{"a":"foo"},"B":{"b":"bar"}}`,
indentExpected: `
{
"A": {
"a": "foo"
},
"B": {
"b": "bar"
}
}
`,
data: &struct {
A struct {
A string `json:"a,omitempty"`
}
B struct {
B string `json:"b,omitempty"`
}
}{A: struct {
A string `json:"a,omitempty"`
}{A: "foo"}, B: struct {
B string `json:"b,omitempty"`
}{B: "bar"}},
},
{
name: "PtrHeadStringMultiFieldsNotRootString",
expected: `{"A":{"a":"foo"},"B":{"b":"bar"}}`,
indentExpected: `
{
"A": {
"a": "foo"
},
"B": {
"b": "bar"
}
}
`,
data: &struct {
A struct {
A string `json:"a,string"`
}
B struct {
B string `json:"b,string"`
}
}{A: struct {
A string `json:"a,string"`
}{A: "foo"}, B: struct {
B string `json:"b,string"`
}{B: "bar"}},
},
// PtrHeadStringPtrMultiFieldsNotRoot
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringPtrMultiFieldsNotRoot",
expected: `{"A":{"a":"foo"},"B":{"b":"bar"}}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": {
"a": "foo"
},
"B": {
"b": "bar"
}
}
`,
2021-01-16 16:16:26 +03:00
data: &struct {
A *struct {
A *string `json:"a"`
}
B *struct {
B *string `json:"b"`
}
}{A: &(struct {
A *string `json:"a"`
}{A: stringptr("foo")}), B: &(struct {
B *string `json:"b"`
}{B: stringptr("bar")})},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringPtrMultiFieldsNotRootOmitEmpty",
expected: `{"A":{"a":"foo"},"B":{"b":"bar"}}`,
indentExpected: `
{
"A": {
"a": "foo"
},
"B": {
"b": "bar"
}
}
`,
data: &struct {
A *struct {
A *string `json:"a,omitempty"`
}
B *struct {
B *string `json:"b,omitempty"`
}
}{A: &(struct {
A *string `json:"a,omitempty"`
}{A: stringptr("foo")}), B: &(struct {
B *string `json:"b,omitempty"`
}{B: stringptr("bar")})},
},
{
name: "PtrHeadStringPtrMultiFieldsNotRootString",
expected: `{"A":{"a":"foo"},"B":{"b":"bar"}}`,
indentExpected: `
{
"A": {
"a": "foo"
},
"B": {
"b": "bar"
}
}
`,
data: &struct {
A *struct {
A *string `json:"a,string"`
}
B *struct {
B *string `json:"b,string"`
}
}{A: &(struct {
A *string `json:"a,string"`
}{A: stringptr("foo")}), B: &(struct {
B *string `json:"b,string"`
}{B: stringptr("bar")})},
},
// PtrHeadStringPtrNilMultiFieldsNotRoot
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringPtrNilMultiFieldsNotRoot",
expected: `{"A":null,"B":null}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": null,
"B": null
}
`,
2021-01-16 16:16:26 +03:00
data: &struct {
A *struct {
A *string `json:"a"`
}
B *struct {
B *string `json:"b"`
}
}{A: nil, B: nil},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringPtrNilMultiFieldsNotRootOmitEmpty",
expected: `{}`,
indentExpected: `
{}
`,
data: &struct {
A *struct {
A *string `json:"a,omitempty"`
}
B *struct {
B *string `json:"b,omitempty"`
}
}{A: nil, B: nil},
},
{
name: "PtrHeadStringPtrNilMultiFieldsNotRootString",
expected: `{"A":null,"B":null}`,
indentExpected: `
{
"A": null,
"B": null
}
`,
data: &struct {
A *struct {
A *string `json:"a,string"`
}
B *struct {
B *string `json:"b,string"`
}
}{A: nil, B: nil},
},
// PtrHeadStringNilMultiFieldsNotRoot
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringNilMultiFieldsNotRoot",
expected: `null`,
2021-01-20 18:22:20 +03:00
indentExpected: `
null
`,
2021-01-16 16:16:26 +03:00
data: (*struct {
A *struct {
A *string `json:"a"`
}
B *struct {
B *string `json:"b"`
}
})(nil),
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringNilMultiFieldsNotRootOmitEmpty",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *struct {
A *string `json:"a,omitempty"`
}
B *struct {
B *string `json:"b,omitempty"`
}
})(nil),
},
{
name: "PtrHeadStringNilMultiFieldsNotRootString",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *struct {
A *string `json:"a,string"`
}
B *struct {
B *string `json:"b,string"`
}
})(nil),
},
// PtrHeadStringDoubleMultiFieldsNotRoot
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringDoubleMultiFieldsNotRoot",
expected: `{"A":{"a":"foo","b":"bar"},"B":{"a":"foo","b":"bar"}}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": {
"a": "foo",
"b": "bar"
},
"B": {
"a": "foo",
"b": "bar"
}
}
`,
2021-01-16 16:16:26 +03:00
data: &struct {
A *struct {
A string `json:"a"`
B string `json:"b"`
}
B *struct {
A string `json:"a"`
B string `json:"b"`
}
}{A: &(struct {
A string `json:"a"`
B string `json:"b"`
}{A: "foo", B: "bar"}), B: &(struct {
A string `json:"a"`
B string `json:"b"`
}{A: "foo", B: "bar"})},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringDoubleMultiFieldsNotRootOmitEmpty",
expected: `{"A":{"a":"foo","b":"bar"},"B":{"a":"foo","b":"bar"}}`,
indentExpected: `
{
"A": {
"a": "foo",
"b": "bar"
},
"B": {
"a": "foo",
"b": "bar"
}
}
`,
data: &struct {
A *struct {
A string `json:"a,omitempty"`
B string `json:"b,omitempty"`
}
B *struct {
A string `json:"a,omitempty"`
B string `json:"b,omitempty"`
}
}{A: &(struct {
A string `json:"a,omitempty"`
B string `json:"b,omitempty"`
}{A: "foo", B: "bar"}), B: &(struct {
A string `json:"a,omitempty"`
B string `json:"b,omitempty"`
}{A: "foo", B: "bar"})},
},
{
name: "PtrHeadStringDoubleMultiFieldsNotRootString",
expected: `{"A":{"a":"foo","b":"bar"},"B":{"a":"foo","b":"bar"}}`,
indentExpected: `
{
"A": {
"a": "foo",
"b": "bar"
},
"B": {
"a": "foo",
"b": "bar"
}
}
`,
data: &struct {
A *struct {
A string `json:"a,string"`
B string `json:"b,string"`
}
B *struct {
A string `json:"a,string"`
B string `json:"b,string"`
}
}{A: &(struct {
A string `json:"a,string"`
B string `json:"b,string"`
}{A: "foo", B: "bar"}), B: &(struct {
A string `json:"a,string"`
B string `json:"b,string"`
}{A: "foo", B: "bar"})},
},
// PtrHeadStringNilDoubleMultiFieldsNotRoot
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringNilDoubleMultiFieldsNotRoot",
expected: `{"A":null,"B":null}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"A": null,
"B": null
}
`,
2021-01-16 16:16:26 +03:00
data: &struct {
A *struct {
A string `json:"a"`
B string `json:"b"`
}
B *struct {
A string `json:"a"`
B string `json:"b"`
}
}{A: nil, B: nil},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrHeadStringNilDoubleMultiFieldsNotRootOmitEmpty",
expected: `{}`,
indentExpected: `
{}
`,
data: &struct {
A *struct {
A string `json:"a,omitempty"`
B string `json:"b,omitempty"`
}
B *struct {
A string `json:"a,omitempty"`
B string `json:"b,omitempty"`
}
}{A: nil, B: nil},
},
{
name: "PtrHeadStringNilDoubleMultiFieldsNotRootString",
expected: `{"A":null,"B":null}`,
indentExpected: `
{
"A": null,
"B": null
}
`,
data: &struct {
A *struct {
A string `json:"a,string"`
B string `json:"b,string"`
}
B *struct {
A string `json:"a,string"`
B string `json:"b,string"`
}
}{A: nil, B: nil},
},
// PtrHeadStringNilDoubleMultiFieldsNotRoot
2021-01-16 16:16:26 +03:00
{
name: "PtrHeadStringNilDoubleMultiFieldsNotRoot",
expected: `null`,
2021-01-20 18:22:20 +03:00
indentExpected: `
null
`,
2021-01-16 16:16:26 +03:00
data: (*struct {
A *struct {
A string `json:"a"`
B string `json:"b"`
}
B *struct {
2021-01-20 18:22:20 +03:00
A string `json:"a"`
B string `json:"b"`
}
})(nil),
},
{
name: "PtrHeadStringNilDoubleMultiFieldsNotRootOmitEmpty",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *struct {
A string `json:"a,omitempty"`
B string `json:"b,omitempty"`
}
B *struct {
A string `json:"a,omitempty"`
B string `json:"b,omitempty"`
}
})(nil),
},
{
name: "PtrHeadStringNilDoubleMultiFieldsNotRootString",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
A *struct {
A string `json:"a,string"`
B string `json:"b,string"`
}
B *struct {
A string `json:"a,string"`
B string `json:"b,string"`
}
})(nil),
},
// PtrHeadStringPtrDoubleMultiFieldsNotRoot
{
name: "PtrHeadStringPtrDoubleMultiFieldsNotRoot",
expected: `{"A":{"a":"foo","b":"bar"},"B":{"a":"foo","b":"bar"}}`,
indentExpected: `
{
"A": {
"a": "foo",
"b": "bar"
},
"B": {
"a": "foo",
"b": "bar"
}
}
`,
data: &struct {
A *struct {
A *string `json:"a"`
B *string `json:"b"`
}
B *struct {
A *string `json:"a"`
B *string `json:"b"`
}
}{A: &(struct {
A *string `json:"a"`
B *string `json:"b"`
}{A: stringptr("foo"), B: stringptr("bar")}), B: &(struct {
A *string `json:"a"`
B *string `json:"b"`
}{A: stringptr("foo"), B: stringptr("bar")})},
},
{
name: "PtrHeadStringPtrDoubleMultiFieldsNotRootOmitEmpty",
expected: `{"A":{"a":"foo","b":"bar"},"B":{"a":"foo","b":"bar"}}`,
indentExpected: `
{
"A": {
"a": "foo",
"b": "bar"
},
"B": {
"a": "foo",
"b": "bar"
}
}
`,
data: &struct {
A *struct {
A *string `json:"a,omitempty"`
B *string `json:"b,omitempty"`
}
B *struct {
A *string `json:"a,omitempty"`
B *string `json:"b,omitempty"`
}
}{A: &(struct {
A *string `json:"a,omitempty"`
B *string `json:"b,omitempty"`
}{A: stringptr("foo"), B: stringptr("bar")}), B: &(struct {
A *string `json:"a,omitempty"`
B *string `json:"b,omitempty"`
}{A: stringptr("foo"), B: stringptr("bar")})},
},
{
name: "PtrHeadStringPtrDoubleMultiFieldsNotRootString",
expected: `{"A":{"a":"foo","b":"bar"},"B":{"a":"foo","b":"bar"}}`,
indentExpected: `
{
"A": {
"a": "foo",
"b": "bar"
},
"B": {
"a": "foo",
"b": "bar"
}
}
`,
data: &struct {
A *struct {
A *string `json:"a,string"`
B *string `json:"b,string"`
}
B *struct {
A *string `json:"a,string"`
B *string `json:"b,string"`
}
}{A: &(struct {
A *string `json:"a,string"`
B *string `json:"b,string"`
}{A: stringptr("foo"), B: stringptr("bar")}), B: &(struct {
A *string `json:"a,string"`
B *string `json:"b,string"`
}{A: stringptr("foo"), B: stringptr("bar")})},
},
// PtrHeadStringPtrNilDoubleMultiFieldsNotRoot
{
name: "PtrHeadStringPtrNilDoubleMultiFieldsNotRoot",
expected: `{"A":null,"B":null}`,
indentExpected: `
{
"A": null,
"B": null
}
`,
data: &struct {
A *struct {
A *string `json:"a"`
B *string `json:"b"`
}
B *struct {
A *string `json:"a"`
B *string `json:"b"`
}
}{A: nil, B: nil},
},
{
name: "PtrHeadStringPtrNilDoubleMultiFieldsNotRootOmitEmpty",
expected: `{"A":null,"B":null}`,
indentExpected: `
{}
`,
data: &struct {
A *struct {
A *string `json:"a,omitempty"`
B *string `json:"b,omitempty"`
}
B *struct {
A *string `json:"a,omitempty"`
B *string `json:"b,omitempty"`
}
}{A: nil, B: nil},
},
{
name: "PtrHeadStringPtrNilDoubleMultiFieldsNotRootString",
expected: `{"A":null,"B":null}`,
indentExpected: `
{
"A": null,
"B": null
}
`,
data: &struct {
A *struct {
A *string `json:"a,string"`
B *string `json:"b,string"`
}
B *struct {
A *string `json:"a,string"`
B *string `json:"b,string"`
2021-01-16 16:16:26 +03:00
}
2021-01-20 18:22:20 +03:00
}{A: nil, B: nil},
2021-01-16 16:16:26 +03:00
},
2021-01-20 18:22:20 +03:00
// PtrHeadStringPtrNilDoubleMultiFieldsNotRoot
2021-01-16 16:16:26 +03:00
{
2021-01-20 18:22:20 +03:00
name: "PtrHeadStringPtrNilDoubleMultiFieldsNotRoot",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
2021-01-16 16:16:26 +03:00
A *struct {
A *string `json:"a"`
B *string `json:"b"`
}
B *struct {
A *string `json:"a"`
B *string `json:"b"`
}
2021-01-20 18:22:20 +03:00
})(nil),
2021-01-16 16:16:26 +03:00
},
{
2021-01-20 18:22:20 +03:00
name: "PtrHeadStringPtrNilDoubleMultiFieldsNotRootOmitEmpty",
expected: `null`,
indentExpected: `
null
`,
data: (*struct {
2021-01-16 16:16:26 +03:00
A *struct {
2021-01-20 18:22:20 +03:00
A *string `json:"a,omitempty"`
B *string `json:"b,omitempty"`
2021-01-16 16:16:26 +03:00
}
B *struct {
2021-01-20 18:22:20 +03:00
A *string `json:"a,omitempty"`
B *string `json:"b,omitempty"`
2021-01-16 16:16:26 +03:00
}
2021-01-20 18:22:20 +03:00
})(nil),
2021-01-16 16:16:26 +03:00
},
{
2021-01-20 18:22:20 +03:00
name: "PtrHeadStringPtrNilDoubleMultiFieldsNotRootString",
2021-01-16 16:16:26 +03:00
expected: `null`,
2021-01-20 18:22:20 +03:00
indentExpected: `
null
`,
2021-01-16 16:16:26 +03:00
data: (*struct {
A *struct {
2021-01-20 18:22:20 +03:00
A *string `json:"a,string"`
B *string `json:"b,string"`
2021-01-16 16:16:26 +03:00
}
B *struct {
2021-01-20 18:22:20 +03:00
A *string `json:"a,string"`
B *string `json:"b,string"`
2021-01-16 16:16:26 +03:00
}
})(nil),
},
2021-01-20 18:22:20 +03:00
// AnonymousHeadString
2021-01-16 16:16:26 +03:00
{
name: "AnonymousHeadString",
expected: `{"a":"foo","b":"bar"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
structString
B string `json:"b"`
}{
structString: structString{A: "foo"},
B: "bar",
},
},
2021-01-20 18:22:20 +03:00
{
name: "AnonymousHeadStringOmitEmpty",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: struct {
structStringOmitEmpty
B string `json:"b,omitempty"`
}{
structStringOmitEmpty: structStringOmitEmpty{A: "foo"},
B: "bar",
},
},
{
name: "AnonymousHeadStringString",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: struct {
structStringString
B string `json:"b,string"`
}{
structStringString: structStringString{A: "foo"},
B: "bar",
},
},
// PtrAnonymousHeadString
2021-01-16 16:16:26 +03:00
{
name: "PtrAnonymousHeadString",
expected: `{"a":"foo","b":"bar"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
*structString
B string `json:"b"`
}{
structString: &structString{A: "foo"},
B: "bar",
},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrAnonymousHeadStringOmitEmpty",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: struct {
*structStringOmitEmpty
B string `json:"b,omitempty"`
}{
structStringOmitEmpty: &structStringOmitEmpty{A: "foo"},
B: "bar",
},
},
{
name: "PtrAnonymousHeadStringString",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: struct {
*structStringString
B string `json:"b,string"`
}{
structStringString: &structStringString{A: "foo"},
B: "bar",
},
},
// NilPtrAnonymousHeadString
2021-01-16 16:16:26 +03:00
{
name: "NilPtrAnonymousHeadString",
expected: `{"b":"baz"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"b": "baz"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
*structString
B string `json:"b"`
}{
structString: nil,
B: "baz",
},
},
2021-01-20 18:22:20 +03:00
{
name: "NilPtrAnonymousHeadStringOmitEmpty",
expected: `{"b":"baz"}`,
indentExpected: `
{
"b": "baz"
}
`,
data: struct {
*structStringOmitEmpty
B string `json:"b,omitempty"`
}{
structStringOmitEmpty: nil,
B: "baz",
},
},
{
name: "NilPtrAnonymousHeadStringString",
expected: `{"b":"baz"}`,
indentExpected: `
{
"b": "baz"
}
`,
data: struct {
*structStringString
B string `json:"b,string"`
}{
structStringString: nil,
B: "baz",
},
},
// AnonymousHeadStringPtr
2021-01-16 16:16:26 +03:00
{
name: "AnonymousHeadStringPtr",
expected: `{"a":"foo","b":"bar"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
structStringPtr
B *string `json:"b"`
}{
structStringPtr: structStringPtr{A: stringptr("foo")},
B: stringptr("bar"),
},
},
2021-01-20 18:22:20 +03:00
{
name: "AnonymousHeadStringPtrOmitEmpty",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: struct {
structStringPtrOmitEmpty
B *string `json:"b,omitempty"`
}{
structStringPtrOmitEmpty: structStringPtrOmitEmpty{A: stringptr("foo")},
B: stringptr("bar"),
},
},
{
name: "AnonymousHeadStringPtrString",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: struct {
structStringPtrString
B *string `json:"b,string"`
}{
structStringPtrString: structStringPtrString{A: stringptr("foo")},
B: stringptr("bar"),
},
},
// AnonymousHeadStringPtrNil
2021-01-16 16:16:26 +03:00
{
name: "AnonymousHeadStringPtrNil",
expected: `{"a":null,"b":"foo"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": null,
"b": "foo"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
structStringPtr
B *string `json:"b"`
}{
structStringPtr: structStringPtr{A: nil},
B: stringptr("foo"),
},
},
2021-01-20 18:22:20 +03:00
{
name: "AnonymousHeadStringPtrNilOmitEmpty",
expected: `{"b":"foo"}`,
indentExpected: `
{
"b": "foo"
}
`,
data: struct {
structStringPtrOmitEmpty
B *string `json:"b,omitempty"`
}{
structStringPtrOmitEmpty: structStringPtrOmitEmpty{A: nil},
B: stringptr("foo"),
},
},
{
name: "AnonymousHeadStringPtrNilString",
expected: `{"a":null,"b":"foo"}`,
indentExpected: `
{
"a": null,
"b": "foo"
}
`,
data: struct {
structStringPtrString
B *string `json:"b,string"`
}{
structStringPtrString: structStringPtrString{A: nil},
B: stringptr("foo"),
},
},
// PtrAnonymousHeadStringPtr
2021-01-16 16:16:26 +03:00
{
name: "PtrAnonymousHeadStringPtr",
expected: `{"a":"foo","b":"bar"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
*structStringPtr
B *string `json:"b"`
}{
structStringPtr: &structStringPtr{A: stringptr("foo")},
B: stringptr("bar"),
},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrAnonymousHeadStringPtrOmitEmpty",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: struct {
*structStringPtrOmitEmpty
B *string `json:"b,omitempty"`
}{
structStringPtrOmitEmpty: &structStringPtrOmitEmpty{A: stringptr("foo")},
B: stringptr("bar"),
},
},
{
name: "PtrAnonymousHeadStringPtrString",
expected: `{"a":"foo","b":"bar"}`,
indentExpected: `
{
"a": "foo",
"b": "bar"
}
`,
data: struct {
*structStringPtrString
B *string `json:"b,string"`
}{
structStringPtrString: &structStringPtrString{A: stringptr("foo")},
B: stringptr("bar"),
},
},
// NilPtrAnonymousHeadStringPtr
2021-01-16 16:16:26 +03:00
{
name: "NilPtrAnonymousHeadStringPtr",
expected: `{"b":"foo"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"b": "foo"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
*structStringPtr
B *string `json:"b"`
}{
structStringPtr: nil,
B: stringptr("foo"),
},
},
2021-01-20 18:22:20 +03:00
{
name: "NilPtrAnonymousHeadStringPtrOmitEmpty",
expected: `{"b":"foo"}`,
indentExpected: `
{
"b": "foo"
}
`,
data: struct {
*structStringPtrOmitEmpty
B *string `json:"b,omitempty"`
}{
structStringPtrOmitEmpty: nil,
B: stringptr("foo"),
},
},
{
name: "NilPtrAnonymousHeadStringPtrString",
expected: `{"b":"foo"}`,
indentExpected: `
{
"b": "foo"
}
`,
data: struct {
*structStringPtrString
B *string `json:"b,string"`
}{
structStringPtrString: nil,
B: stringptr("foo"),
},
},
// AnonymousHeadStringOnly
2021-01-16 16:16:26 +03:00
{
name: "AnonymousHeadStringOnly",
expected: `{"a":"foo"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
structString
}{
structString: structString{A: "foo"},
},
},
2021-01-20 18:22:20 +03:00
{
name: "AnonymousHeadStringOnlyOmitEmpty",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: struct {
structStringOmitEmpty
}{
structStringOmitEmpty: structStringOmitEmpty{A: "foo"},
},
},
{
name: "AnonymousHeadStringOnlyString",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: struct {
structStringString
}{
structStringString: structStringString{A: "foo"},
},
},
// PtrAnonymousHeadStringOnly
2021-01-16 16:16:26 +03:00
{
name: "PtrAnonymousHeadStringOnly",
expected: `{"a":"foo"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
*structString
}{
structString: &structString{A: "foo"},
},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrAnonymousHeadStringOnlyOmitEmpty",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: struct {
*structStringOmitEmpty
}{
structStringOmitEmpty: &structStringOmitEmpty{A: "foo"},
},
},
{
name: "PtrAnonymousHeadStringOnlyString",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: struct {
*structStringString
}{
structStringString: &structStringString{A: "foo"},
},
},
// NilPtrAnonymousHeadStringOnly
2021-01-16 16:16:26 +03:00
{
name: "NilPtrAnonymousHeadStringOnly",
expected: `{}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{}
`,
2021-01-16 16:16:26 +03:00
data: struct {
*structString
}{
structString: nil,
},
},
2021-01-20 18:22:20 +03:00
{
name: "NilPtrAnonymousHeadStringOnlyOmitEmpty",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
*structStringOmitEmpty
}{
structStringOmitEmpty: nil,
},
},
{
name: "NilPtrAnonymousHeadStringOnlyString",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
*structStringString
}{
structStringString: nil,
},
},
// AnonymousHeadStringPtrOnly
2021-01-16 16:16:26 +03:00
{
name: "AnonymousHeadStringPtrOnly",
expected: `{"a":"foo"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
structStringPtr
}{
structStringPtr: structStringPtr{A: stringptr("foo")},
},
},
2021-01-20 18:22:20 +03:00
{
name: "AnonymousHeadStringPtrOnlyOmitEmpty",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: struct {
structStringPtrOmitEmpty
}{
structStringPtrOmitEmpty: structStringPtrOmitEmpty{A: stringptr("foo")},
},
},
{
name: "AnonymousHeadStringPtrOnlyString",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: struct {
structStringPtrString
}{
structStringPtrString: structStringPtrString{A: stringptr("foo")},
},
},
// AnonymousHeadStringPtrNilOnly
2021-01-16 16:16:26 +03:00
{
name: "AnonymousHeadStringPtrNilOnly",
expected: `{"a":null}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": null
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
structStringPtr
}{
structStringPtr: structStringPtr{A: nil},
},
},
2021-01-20 18:22:20 +03:00
{
name: "AnonymousHeadStringPtrNilOnlyOmitEmpty",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
structStringPtrOmitEmpty
}{
structStringPtrOmitEmpty: structStringPtrOmitEmpty{A: nil},
},
},
{
name: "AnonymousHeadStringPtrNilOnlyString",
expected: `{"a":null}`,
indentExpected: `
{
"a": null
}
`,
data: struct {
structStringPtrString
}{
structStringPtrString: structStringPtrString{A: nil},
},
},
// PtrAnonymousHeadStringPtrOnly
2021-01-16 16:16:26 +03:00
{
name: "PtrAnonymousHeadStringPtrOnly",
expected: `{"a":"foo"}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{
"a": "foo"
}
`,
2021-01-16 16:16:26 +03:00
data: struct {
*structStringPtr
}{
structStringPtr: &structStringPtr{A: stringptr("foo")},
},
},
2021-01-20 18:22:20 +03:00
{
name: "PtrAnonymousHeadStringPtrOnlyOmitEmpty",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: struct {
*structStringPtrOmitEmpty
}{
structStringPtrOmitEmpty: &structStringPtrOmitEmpty{A: stringptr("foo")},
},
},
{
name: "PtrAnonymousHeadStringPtrOnlyString",
expected: `{"a":"foo"}`,
indentExpected: `
{
"a": "foo"
}
`,
data: struct {
*structStringPtrString
}{
structStringPtrString: &structStringPtrString{A: stringptr("foo")},
},
},
// NilPtrAnonymousHeadStringPtrOnly
2021-01-16 16:16:26 +03:00
{
name: "NilPtrAnonymousHeadStringPtrOnly",
expected: `{}`,
2021-01-20 18:22:20 +03:00
indentExpected: `
{}
`,
2021-01-16 16:16:26 +03:00
data: struct {
*structStringPtr
}{
structStringPtr: nil,
},
},
2021-01-20 18:22:20 +03:00
{
name: "NilPtrAnonymousHeadStringPtrOnlyOmitEmpty",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
*structStringPtrOmitEmpty
}{
structStringPtrOmitEmpty: nil,
},
},
{
name: "NilPtrAnonymousHeadStringPtrOnlyString",
expected: `{}`,
indentExpected: `
{}
`,
data: struct {
*structStringPtrString
}{
structStringPtrString: nil,
},
},
2021-01-16 16:16:26 +03:00
}
for _, test := range tests {
2021-01-20 18:22:20 +03:00
for _, indent := range []bool{true, false} {
for _, htmlEscape := range []bool{true, false} {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(htmlEscape)
if indent {
enc.SetIndent("", " ")
}
if err := enc.Encode(test.data); err != nil {
t.Fatalf("%s(htmlEscape:%T): %s: %s", test.name, htmlEscape, test.expected, err)
}
stdresult := encodeByEncodingJSON(test.data, indent, htmlEscape)
if buf.String() != stdresult {
t.Errorf("%s(htmlEscape:%T): doesn't compatible with encoding/json. expected %q but got %q", test.name, htmlEscape, stdresult, buf.String())
}
2021-01-16 16:16:26 +03:00
}
}
}
}