File tree Expand file tree Collapse file tree 3 files changed +12
-0
lines changed Expand file tree Collapse file tree 3 files changed +12
-0
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ type JSON struct {
3131 Omit bool
3232 Inline bool
3333 Omitempty bool
34+ OmitZero bool
3435}
3536
3637func (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
Original file line number Diff line number Diff 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" ),
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments