Skip to content

Commit

Permalink
fix(params-check): contemplate omitempty in json tag for _id and _rev…
Browse files Browse the repository at this point in the history
… check
  • Loading branch information
rozanecm committed Mar 26, 2024
1 parent 3e7c1be commit 6c76c07
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"reflect"
"regexp"
"strings"
)

var ErrMissingID = errors.New("missing _id field")
Expand Down Expand Up @@ -34,9 +35,9 @@ func checkParameter(param interface{}) error {
for i := 0; i < fields.NumField(); i++ {
field := fields.Field(i)
tag := field.Tag.Get("json")
if tag == "_id" {
if strings.Contains(tag, "_id") {
idTagExists = true
} else if tag == "_rev" {
} else if strings.Contains(tag, "_rev") {
revTagExists = true
}
if idTagExists && revTagExists {
Expand Down
10 changes: 10 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type structWithMixedTags struct {
Rev string
}

type structWithOmitEmpty struct {
ID string `json:"_id,omitempty"`
Rev string `json:"_rev,omitempty"`
}

// TestCheckParameter tests the checkParameter function
func TestCheckParameter(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -80,6 +85,11 @@ func TestCheckParameter(t *testing.T) {
param: nil,
expected: errors.New("unsupported type"), // Adjust this expected error message if needed
},
{
name: "Test structWithOmitEmpty with _id and _rev",
param: structWithOmitEmpty{ID: "123", Rev: "456"},
expected: nil,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 6c76c07

Please sign in to comment.