Skip to content

Commit b22feca

Browse files
authored
Merge pull request #306 from carsontham/support-omitzero-in-LookupJSON
Added support for omitzero in LookupJSON (v2/parser/tags)
2 parents cc0d9c4 + c990d91 commit b22feca

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

v2/parser/tags/json.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type JSON struct {
3131
Omit bool
3232
Inline bool
3333
Omitempty bool
34+
OmitZero bool
3435
}
3536

3637
func (t JSON) String() string {
@@ -41,6 +42,9 @@ func (t JSON) String() string {
4142
if t.Omitempty {
4243
tag += ",omitempty"
4344
}
45+
if t.OmitZero {
46+
tag += ",omitzero"
47+
}
4448
// "inline" isn't (yet) a standard json tag, but it is used by
4549
// gengo to indicate that the field should be inlined.
4650
if t.Inline {
@@ -59,6 +63,7 @@ func LookupJSON(m types.Member) (JSON, bool) {
5963
// gengo to indicate that the field should be inlined.
6064
inline := (m.Embedded && name == "") || opts.Contains("inline")
6165
omitempty := opts.Contains("omitempty")
66+
omitzero := opts.Contains("omitzero")
6267
if !inline && name == "" {
6368
name = m.Name
6469
}
@@ -67,6 +72,7 @@ func LookupJSON(m types.Member) (JSON, bool) {
6772
Omit: false,
6873
Inline: inline,
6974
Omitempty: omitempty,
75+
OmitZero: omitzero,
7076
}, true
7177
}
7278

v2/parser/tags/json_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ func TestJSON(t *testing.T) {
7373
member: member("T1", "E"),
7474
expected: JSON{Name: "E"},
7575
},
76+
{
77+
name: "omitzero",
78+
member: member("T1", "F"),
79+
expected: JSON{Name: "f", OmitZero: true},
80+
},
7681
{
7782
name: "embedded struct",
7883
member: member("T1", "T2"),

v2/parser/tags/testdata/tags/file.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type T1 struct {
66
C string `json:",inline"`
77
D string `json:"-"`
88
E string `json:""`
9+
F string `json:"f,omitzero"`
910

1011
T2
1112
*T3

0 commit comments

Comments
 (0)