Skip to content

Commit

Permalink
feat(omitempty): swapping tag opts for the omitempty functionality (#73)
Browse files Browse the repository at this point in the history
* swapping tag opts for the omitempty functionality

* Updating const name
  • Loading branch information
Jacobbrewer1 authored Dec 20, 2024
1 parent d291b4d commit 54619b9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
4 changes: 2 additions & 2 deletions loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s *loadDiffSuite) TestLoadDiff_Success() {
func (s *loadDiffSuite) TestLoadDiff_Success_StructOpt_IncludeNilField() {
type testStruct struct {
Name string
Age *int `patcher:"nil"`
Age *int `patcher:"omitempty"`
}

old := testStruct{
Expand All @@ -73,7 +73,7 @@ func (s *loadDiffSuite) TestLoadDiff_Success_StructOpt_IncludeNilField() {
func (s *loadDiffSuite) TestLoadDiff_Success_StructOpt_IncludeZeroField() {
type testStruct struct {
Name string
Age int `patcher:"zero"`
Age int `patcher:"omitempty"`
}

old := testStruct{
Expand Down
17 changes: 7 additions & 10 deletions patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,21 @@ func (s *SQLPatch) shouldIncludeNil(tag string) bool {
return true
}

if tag != "" {
tags := strings.Split(tag, TagOptSeparator)
if slices.Contains(tags, TagOptAllowNil) {
return true
}
}

return false
return s.shouldOmitEmpty(tag)
}

func (s *SQLPatch) shouldIncludeZero(tag string) bool {
if s.includeZeroValues {
return true
}

return s.shouldOmitEmpty(tag)
}

func (s *SQLPatch) shouldOmitEmpty(tag string) bool {
if tag != "" {
tagOpts := strings.Split(tag, TagOptSeparator)
if slices.Contains(tagOpts, TagOptAllowZero) {
tags := strings.Split(tag, TagOptSeparator)
if slices.Contains(tags, TagOptOmitempty) {
return true
}
}
Expand Down
3 changes: 1 addition & 2 deletions patch_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const (
TagOptsName = "patcher"
TagOptSeparator = ","
TagOptSkip = "-"
TagOptAllowNil = "nil"
TagOptAllowZero = "zero"
TagOptOmitempty = "omitempty"
)

type PatchOpt func(*SQLPatch)
Expand Down
12 changes: 6 additions & 6 deletions sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *newSQLPatchSuite) TestNewSQLPatch_Success() {
func (s *newSQLPatchSuite) TestNewSQLPatch_Success_Struct_opt_IncludeNilFields() {
type testObj struct {
Id *int `db:"id_tag"`
Name *string `db:"name_tag" patcher:"nil"`
Name *string `db:"name_tag" patcher:"omitempty"`
}

obj := testObj{
Expand All @@ -53,7 +53,7 @@ func (s *newSQLPatchSuite) TestNewSQLPatch_Success_Struct_opt_IncludeNilFields()
func (s *newSQLPatchSuite) TestNewSQLPatch_Success_Struct_opt_IncludeZeroFields() {
type testObj struct {
Id int `db:"id_tag"`
Name string `db:"name_tag" patcher:"zero"`
Name string `db:"name_tag" patcher:"omitempty"`
}

obj := testObj{
Expand Down Expand Up @@ -385,7 +385,7 @@ func (s *generateSQLSuite) TestGenerateSQL_Success() {
func (s *generateSQLSuite) TestGenerateSQL_Success_Stuct_opt_IncludeNilFields() {
type testObj struct {
Id *int `db:"id"`
Name *string `db:"name" patcher:"nil"`
Name *string `db:"name" patcher:"omitempty"`
}

obj := testObj{
Expand All @@ -410,7 +410,7 @@ func (s *generateSQLSuite) TestGenerateSQL_Success_Stuct_opt_IncludeNilFields()
func (s *generateSQLSuite) TestGenerateSQL_Success_Struct_opt_IncludeZeroFields() {
type testObj struct {
Id int `db:"id"`
Name string `db:"name" patcher:"zero"`
Name string `db:"name" patcher:"omitempty"`
}

obj := testObj{
Expand Down Expand Up @@ -1003,7 +1003,7 @@ func (s *NewDiffSQLPatchSuite) TestNewDiffSQLPatch_Success() {
func (s *NewDiffSQLPatchSuite) TestNewDiffSQLPatch_Success_StructOpt_IncludeNilFields() {
type testObj struct {
Id *int `db:"id"`
Name *string `db:"name" patcher:"nil"`
Name *string `db:"name" patcher:"omitempty"`
}

obj := testObj{
Expand All @@ -1027,7 +1027,7 @@ func (s *NewDiffSQLPatchSuite) TestNewDiffSQLPatch_Success_StructOpt_IncludeNilF
func (s *NewDiffSQLPatchSuite) TestNewDiffSQLPatch_Success_StructOpt_IncludeZeroFields() {
type testObj struct {
Id int `db:"id"`
Name string `db:"name" patcher:"zero"`
Name string `db:"name" patcher:"omitempty"`
}

obj := testObj{
Expand Down

0 comments on commit 54619b9

Please sign in to comment.