Skip to content

Commit

Permalink
Merge pull request #224 from golanglemonade/omitempty
Browse files Browse the repository at this point in the history
Respect the `omitempty` flag on structs when the field value is empty
  • Loading branch information
Yamashou authored Apr 24, 2024
2 parents 8309b96 + c970a94 commit b4a7b81
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
23 changes: 18 additions & 5 deletions clientv2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,10 @@ func encodeString(v reflect.Value) ([]byte, error) {
}

type fieldInfo struct {
name string
jsonName string
typ reflect.Type
name string
jsonName string
omitempty bool
typ reflect.Type
}

func prepareFields(t reflect.Type) []fieldInfo {
Expand All @@ -521,16 +522,24 @@ func prepareFields(t reflect.Type) []fieldInfo {
if jsonTag == "-" {
continue // Skip fields explicitly marked to be ignored
}

jsonName := f.Name
if jsonTag != "" {
parts := strings.Split(jsonTag, ",")
jsonName = parts[0] // Use the name specified in the JSON tag
}
fields = append(fields, fieldInfo{

fi := fieldInfo{
name: f.Name,
jsonName: jsonName,
typ: f.Type,
})
}

if strings.Contains(jsonTag, "omitempty") {
fi.omitempty = true
}

fields = append(fields, fi)
}

return fields
Expand All @@ -545,6 +554,10 @@ func encodeStruct(v reflect.Value) ([]byte, error) {
continue // Skip invalid or nil pointers to avoid panics
}

if field.omitempty && fieldValue.IsZero() {
continue // Skip nil fields marked with omitempty
}

encodedValue, err := encode(fieldValue)
if err != nil {
return nil, err
Expand Down
38 changes: 38 additions & 0 deletions clientv2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,13 @@ func TestMarshalJSON(t *testing.T) {
Name string `json:"name"`
Number Number `json:"number"`
}

// example with omitted fields
type Input struct {
ID string `json:"id"`
Tags []string `json:"tags,omitempty"`
}

testDate := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
type args struct {
v any
Expand Down Expand Up @@ -642,6 +649,37 @@ func TestMarshalJSON(t *testing.T) {
},
want: []byte(`{"time":"2021-01-01T00:00:00Z"}`),
},
{
name: "marshal omitted fields",
args: args{
v: Request{
OperationName: "query",
Query: `query ($input: Number!) { input }`,
Variables: map[string]any{
"input": Input{
ID: "1",
},
},
},
},
want: []byte(`{"operationName":"query", "query":"query ($input: Number!) { input }","variables":{"input":{"id":"1"}}}`),
},
{
name: "marshal fields",
args: args{
v: Request{
OperationName: "query",
Query: `query ($input: Number!) { input }`,
Variables: map[string]any{
"input": Input{
ID: "1",
Tags: []string{"tag1", "tag2"},
},
},
},
},
want: []byte(`{"operationName":"query", "query":"query ($input: Number!) { input }","variables":{"input":{"id":"1", "tags":["tag1","tag2"]}}}`),
},
{
name: "marshal time.Time",
args: args{
Expand Down

0 comments on commit b4a7b81

Please # to comment.