Skip to content

Commit

Permalink
Feature: Support omit empty on dbtag (#12)
Browse files Browse the repository at this point in the history
* Feature: Support omit empty on dbtag

* lint
  • Loading branch information
zagronitay authored Jan 22, 2024
1 parent f11b0c7 commit 4b5f08b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
16 changes: 12 additions & 4 deletions insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

type insertModel struct {
IntField int64 `db:"int_field"`
OtherValue string
SkipInsert bool `goqux:"skip_insert"`
DbTag string `db:"another_col_name"`
IntField int64 `db:"int_field"`
OtherValue string
SkipInsert bool `goqux:"skip_insert"`
DbTag string `db:"another_col_name"`
DbTagOmitEmpty string `db:"another_col_name_omit,omitempty"`
}

func TestBuildInsert(t *testing.T) {
Expand All @@ -25,6 +26,13 @@ func TestBuildInsert(t *testing.T) {
}{
{
name: "simple_insert",
values: []any{insertModel{IntField: 5, DbTagOmitEmpty: "test"}},
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "another_col_name_omit", "int_field", "other_value") VALUES ($1, $2, $3, $4)`,
expectedArgs: []interface{}{"", "test", int64(5), ""},
expectedError: nil,
},
{
name: "simple_insert_ompitempty",
values: []any{insertModel{IntField: 5}},
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "int_field", "other_value") VALUES ($1, $2, $3)`,
expectedArgs: []interface{}{"", int64(5), ""},
Expand Down
8 changes: 8 additions & 0 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
defaultNow = "now"
// Same as default now but will inject time.Now().UTC()
defaultNowUtc = "now_utc"
// omitempty will skip the field if it is zero value
omitEmpty = ",omitempty"
)

func encodeValues(v any, skipType string, skipZeroValues bool) map[string]SQLValuer {
Expand All @@ -42,6 +44,12 @@ func encodeValues(v any, skipType string, skipZeroValues bool) map[string]SQLVal

columnName := strcase.ToSnake(f.Name)
if dbTag := f.Tag.Get(tagNameDb); dbTag != "" {
if strings.Contains(dbTag, omitEmpty) {
if value.IsZero() {
continue
}
dbTag = strings.ReplaceAll(dbTag, omitEmpty, "")
}
columnName = dbTag
}

Expand Down
12 changes: 10 additions & 2 deletions update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

type updateModel struct {
IntField int
DbTag string `db:"another_col_name"`
IntField int
DbTag string `db:"another_col_name"`
DbTagOmitEmpty string `db:"another_col_name_omit,omitempty"`
}

func TestBuildUpdate(t *testing.T) {
Expand All @@ -29,6 +30,13 @@ func TestBuildUpdate(t *testing.T) {
expectedArgs: []interface{}{"test", int64(5)},
expectedError: nil,
},
{
name: "simple_update",
dst: updateModel{IntField: 5, DbTagOmitEmpty: "test"},
expectedQuery: `UPDATE "update_models" SET "another_col_name_omit"=$1,"int_field"=$2`,
expectedArgs: []interface{}{"test", int64(5)},
expectedError: nil,
},
{
name: "update_with_filters",
dst: updateModel{IntField: 5},
Expand Down

0 comments on commit 4b5f08b

Please sign in to comment.