Skip to content

Commit 4b5f08b

Browse files
authored
Feature: Support omit empty on dbtag (#12)
* Feature: Support omit empty on dbtag * lint
1 parent f11b0c7 commit 4b5f08b

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

insert_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import (
88
)
99

1010
type insertModel struct {
11-
IntField int64 `db:"int_field"`
12-
OtherValue string
13-
SkipInsert bool `goqux:"skip_insert"`
14-
DbTag string `db:"another_col_name"`
11+
IntField int64 `db:"int_field"`
12+
OtherValue string
13+
SkipInsert bool `goqux:"skip_insert"`
14+
DbTag string `db:"another_col_name"`
15+
DbTagOmitEmpty string `db:"another_col_name_omit,omitempty"`
1516
}
1617

1718
func TestBuildInsert(t *testing.T) {
@@ -25,6 +26,13 @@ func TestBuildInsert(t *testing.T) {
2526
}{
2627
{
2728
name: "simple_insert",
29+
values: []any{insertModel{IntField: 5, DbTagOmitEmpty: "test"}},
30+
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "another_col_name_omit", "int_field", "other_value") VALUES ($1, $2, $3, $4)`,
31+
expectedArgs: []interface{}{"", "test", int64(5), ""},
32+
expectedError: nil,
33+
},
34+
{
35+
name: "simple_insert_ompitempty",
2836
values: []any{insertModel{IntField: 5}},
2937
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "int_field", "other_value") VALUES ($1, $2, $3)`,
3038
expectedArgs: []interface{}{"", int64(5), ""},

struct.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const (
2424
defaultNow = "now"
2525
// Same as default now but will inject time.Now().UTC()
2626
defaultNowUtc = "now_utc"
27+
// omitempty will skip the field if it is zero value
28+
omitEmpty = ",omitempty"
2729
)
2830

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

4345
columnName := strcase.ToSnake(f.Name)
4446
if dbTag := f.Tag.Get(tagNameDb); dbTag != "" {
47+
if strings.Contains(dbTag, omitEmpty) {
48+
if value.IsZero() {
49+
continue
50+
}
51+
dbTag = strings.ReplaceAll(dbTag, omitEmpty, "")
52+
}
4553
columnName = dbTag
4654
}
4755

update_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99
)
1010

1111
type updateModel struct {
12-
IntField int
13-
DbTag string `db:"another_col_name"`
12+
IntField int
13+
DbTag string `db:"another_col_name"`
14+
DbTagOmitEmpty string `db:"another_col_name_omit,omitempty"`
1415
}
1516

1617
func TestBuildUpdate(t *testing.T) {
@@ -29,6 +30,13 @@ func TestBuildUpdate(t *testing.T) {
2930
expectedArgs: []interface{}{"test", int64(5)},
3031
expectedError: nil,
3132
},
33+
{
34+
name: "simple_update",
35+
dst: updateModel{IntField: 5, DbTagOmitEmpty: "test"},
36+
expectedQuery: `UPDATE "update_models" SET "another_col_name_omit"=$1,"int_field"=$2`,
37+
expectedArgs: []interface{}{"test", int64(5)},
38+
expectedError: nil,
39+
},
3240
{
3341
name: "update_with_filters",
3442
dst: updateModel{IntField: 5},

0 commit comments

Comments
 (0)