feat: Handle `omitzero` in `structfield` linter by Not-Dhananjay-Mishra · Pull Request #3881 · google/go-github

Should I go ahead with this :

Allowing only - map[...]... , *Struct , []*Struct and []buildin as suggested.

I tried a few small experiments with different field shapes. From what I observed, even when the data is “empty”, these values are still considered non-zero and therefore are not omitted during marshaling.

I tested with the following example.

type DummyDataForOmitZero struct {
	OmitZeroWithoutPointerSlice []int  `json:"omit_zero_without_pointer_slice,omitzero"`
	OmitZeroWithPointerSlice    []*int `json:"omit_zero_with_pointer_slice,omitzero"`

	OmitZeroWithoutPointerStruct Struct  `json:"omit_zero_without_pointer_struct,omitzero"`
	OmitZeroWithPointerStruct    *Struct `json:"omit_zero_with_pointer_struct,omitzero"`

	OmitZeroWithoutPointerSliceofStruct []Struct  `json:"omit_zero_without_pointer_sliceofstruct,omitzero"`
	OmitZeroWithPointerSliceofStruct    []*Struct `json:"omit_zero_with_pointer_sliceofstruct,omitzero"`

	OmitZeroWithoutPointerMap map[string]string  `json:"omit_zero_without_pointer_map,omitzero"`
	OmitZeroWithPointerMap    *map[string]string `json:"omit_zero_with_pointer_map,omitzero"`
}

Empty input:

		data := DummyDataForOmitZero{
			OmitZeroWithoutPointerSlice:         []int{},
			OmitZeroWithPointerSlice:            []*int{},
			OmitZeroWithoutPointerSliceofStruct: []Struct{},
			OmitZeroWithPointerSliceofStruct:    []*Struct{},
			OmitZeroWithoutPointerStruct:        Struct{},
			OmitZeroWithPointerStruct:           &Struct{},
			OmitZeroWithoutPointerMap:           map[string]string{},
			OmitZeroWithPointerMap:              &map[string]string{},
		}

Marshaled output:

{
  "omit_zero_without_pointer_slice": [],
  "omit_zero_with_pointer_slice": [],
  "omit_zero_with_pointer_struct": {},
  "omit_zero_without_pointer_sliceofstruct": [],
  "omit_zero_with_pointer_sliceofstruct": [],
  "omit_zero_without_pointer_map": {},
  "omit_zero_with_pointer_map": {}
}

or should non-pointer values be allowed as well?
any opinion?