fix custom array type with omitempty
This commit is contained in:
parent
df897aec9d
commit
2410a4c5e9
|
@ -589,6 +589,8 @@ func IsNilForMarshaler(v interface{}) bool {
|
|||
return rv.IsNil()
|
||||
case reflect.Slice:
|
||||
return rv.IsNil() || rv.Len() == 0
|
||||
case reflect.Array:
|
||||
return rv.IsZero()
|
||||
case reflect.String:
|
||||
return rv.Len() == 0
|
||||
}
|
||||
|
|
|
@ -8,6 +8,14 @@ import (
|
|||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
type customArray [2]int
|
||||
|
||||
type customArrayWithMarshaler [2]int
|
||||
|
||||
func (b customArrayWithMarshaler) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal([2]int(b))
|
||||
}
|
||||
|
||||
func TestCoverArray(t *testing.T) {
|
||||
type structArray struct {
|
||||
A [2]int `json:"a"`
|
||||
|
@ -47,6 +55,24 @@ func TestCoverArray(t *testing.T) {
|
|||
A *[2]*int `json:"a,string"`
|
||||
}
|
||||
|
||||
type structCustomArrayOmitEmpty struct {
|
||||
A customArray `json:"a,omitempty"`
|
||||
}
|
||||
|
||||
type structSecondFieldCustomArrayOmitEmpty struct {
|
||||
C [2]int `json:"c"`
|
||||
A customArray `json:"a,omitempty"`
|
||||
}
|
||||
|
||||
type structCustomArrayWithMarshalerOmitEmpty struct {
|
||||
A customArrayWithMarshaler `json:"a,omitempty"`
|
||||
}
|
||||
|
||||
type structSecondFieldCustomArrayWithMarshalerOmitEmpty struct {
|
||||
C [2]int `json:"c"`
|
||||
A customArrayWithMarshaler `json:"a,omitempty"`
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
data interface{}
|
||||
|
@ -1421,6 +1447,46 @@ func TestCoverArray(t *testing.T) {
|
|||
B: [2]int{1},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "AnonymousHeadCustomArrayOmitEmpty",
|
||||
data: struct {
|
||||
structCustomArrayOmitEmpty
|
||||
B customArray `json:"b,omitempty"`
|
||||
}{
|
||||
structCustomArrayOmitEmpty: structCustomArrayOmitEmpty{A: customArray{-1}},
|
||||
B: [2]int{1},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "AnonymousHeadCustomArrayOmitEmpty",
|
||||
data: struct {
|
||||
structCustomArrayOmitEmpty
|
||||
B customArray `json:"b,omitempty"`
|
||||
}{
|
||||
structCustomArrayOmitEmpty: structCustomArrayOmitEmpty{A: customArray{-1}},
|
||||
B: [2]int{1},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "AnonymousHeadCustomArrayWithMarshalerOmitEmpty",
|
||||
data: struct {
|
||||
structCustomArrayWithMarshalerOmitEmpty
|
||||
B customArrayWithMarshaler `json:"b,omitempty"`
|
||||
}{
|
||||
structCustomArrayWithMarshalerOmitEmpty: structCustomArrayWithMarshalerOmitEmpty{A: customArrayWithMarshaler{-1}},
|
||||
B: [2]int{1},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "AnonymousHeadCustomArrayWithMarshalerOmitEmpty",
|
||||
data: struct {
|
||||
structCustomArrayWithMarshalerOmitEmpty
|
||||
B customArrayWithMarshaler `json:"b,omitempty"`
|
||||
}{
|
||||
structCustomArrayWithMarshalerOmitEmpty: structCustomArrayWithMarshalerOmitEmpty{A: customArrayWithMarshaler{-1}},
|
||||
B: [2]int{1},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "AnonymousHeadArrayString",
|
||||
data: struct {
|
||||
|
|
Loading…
Reference in a new issue