From 54619b9fa5cfe5f08d43909654cde26d2b97777d Mon Sep 17 00:00:00 2001 From: Jacob Brewer Date: Fri, 20 Dec 2024 13:20:04 +0000 Subject: [PATCH] feat(omitempty): swapping tag opts for the omitempty functionality (#73) * swapping tag opts for the omitempty functionality * Updating const name --- loader_test.go | 4 ++-- patch.go | 17 +++++++---------- patch_opts.go | 3 +-- sql_test.go | 12 ++++++------ 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/loader_test.go b/loader_test.go index 4cc35be..d0241f2 100644 --- a/loader_test.go +++ b/loader_test.go @@ -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{ @@ -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{ diff --git a/patch.go b/patch.go index d0c0d70..5d6a87a 100644 --- a/patch.go +++ b/patch.go @@ -147,14 +147,7 @@ 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 { @@ -162,9 +155,13 @@ func (s *SQLPatch) shouldIncludeZero(tag string) bool { 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 } } diff --git a/patch_opts.go b/patch_opts.go index 1c671b4..67b9e9c 100644 --- a/patch_opts.go +++ b/patch_opts.go @@ -9,8 +9,7 @@ const ( TagOptsName = "patcher" TagOptSeparator = "," TagOptSkip = "-" - TagOptAllowNil = "nil" - TagOptAllowZero = "zero" + TagOptOmitempty = "omitempty" ) type PatchOpt func(*SQLPatch) diff --git a/sql_test.go b/sql_test.go index c2ef63b..70f1c8c 100644 --- a/sql_test.go +++ b/sql_test.go @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{