Skip to content

Commit

Permalink
logic fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamashou committed Dec 24, 2024
1 parent d506d50 commit 88a613e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
10 changes: 5 additions & 5 deletions clientv2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,19 +651,19 @@ func (e *Encoder) isSkipOmitemptyField(v reflect.Value, field fieldInfo) bool {
return false
}

if !v.IsValid() {
return true
if !field.omitempty {
return false
}

if v.Kind() == reflect.Ptr && v.IsNil() {
if !v.IsValid() {
return true
}

if field.omitempty && v.IsZero() {
if v.Kind() == reflect.Ptr && v.IsNil() {
return true
}

return false
return v.IsZero()
}

// encodeStruct encodes a struct value
Expand Down
4 changes: 4 additions & 0 deletions clientv2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,7 @@ func TestEncoder_encodeStruct(t *testing.T) {
Name string `json:"name"`
Age int64 `json:"age,omitempty"`
Email *string `json:"email,omitempty"`
Email2 *string `json:"email2"`
Address Address `json:"address"`
Tags []string `json:"tags,omitempty"`
Nickname string `json:"nickname,omitempty"`
Expand Down Expand Up @@ -1002,6 +1003,7 @@ func TestEncoder_encodeStruct(t *testing.T) {
"name": "John",
"age": int64(30),
"email": "test@example.com",
"email2": nil,
"address": map[string]any{"city": "Tokyo", "country": "Japan", "zip": "123-4567"},
"tags": []any{"tag1", "tag2"},
"nickname": "Johnny",
Expand All @@ -1016,6 +1018,7 @@ func TestEncoder_encodeStruct(t *testing.T) {
enableOmitemptyTag: true,
want: map[string]any{
"name": "John",
"email2": nil,
"address": map[string]any{"city": "Tokyo"},
},
},
Expand All @@ -1030,6 +1033,7 @@ func TestEncoder_encodeStruct(t *testing.T) {
"name": "John",
"age": int64(0),
"email": nil,
"email2": nil,
"address": map[string]any{"city": "Tokyo", "country": "", "zip": nil},
"tags": []any{},
"nickname": "",
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/urfave/cli/v2"
)

const version = "0.30.0"
const version = "0.30.1"

var versionCmd = &cli.Command{
Name: "version",
Expand Down

0 comments on commit 88a613e

Please sign in to comment.