Skip to content

Commit

Permalink
chore(inserter): initializing on an empty array by default (#63)
Browse files Browse the repository at this point in the history
* initializing the fields on default

* initializing the fields on default

* using default behaviour
  • Loading branch information
Jacobbrewer1 authored Nov 3, 2024
1 parent 4cf97e3 commit 63f6d7b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
12 changes: 10 additions & 2 deletions inserter/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ type SQLBatch struct {
// newBatchDefaults returns a new SQLBatch with default values
func newBatchDefaults(opts ...BatchOpt) *SQLBatch {
b := &SQLBatch{
fields: nil,
args: nil,
fields: make([]string, 0),
args: make([]any, 0),
db: nil,
tagName: patcher.DefaultDbTagName,
table: "",
Expand All @@ -64,10 +64,18 @@ func newBatchDefaults(opts ...BatchOpt) *SQLBatch {
}

func (b *SQLBatch) Fields() []string {
if len(b.fields) == 0 {
// Default behaviour to return nil if no fields are set
return nil
}
return b.fields
}

func (b *SQLBatch) Args() []any {
if len(b.args) == 0 {
// Default behaviour to return nil if no args are set
return nil
}
return b.args
}

Expand Down
12 changes: 10 additions & 2 deletions patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ type SQLPatch struct {
func newPatchDefaults(opts ...PatchOpt) *SQLPatch {
// Default options
p := &SQLPatch{
fields: nil,
args: nil,
fields: make([]string, 0),
args: make([]any, 0),
db: nil,
tagName: DefaultDbTagName,
table: "",
Expand All @@ -97,10 +97,18 @@ func newPatchDefaults(opts ...PatchOpt) *SQLPatch {
}

func (s *SQLPatch) Fields() []string {
if len(s.fields) == 0 {
// Default behaviour is to return nil if there are no fields
return nil
}
return s.fields
}

func (s *SQLPatch) Args() []any {
if len(s.args) == 0 {
// Default behaviour is to return nil if there are no args
return nil
}
return s.args
}

Expand Down
4 changes: 2 additions & 2 deletions patch_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func WithWhere(where Wherer) PatchOpt {
}
fwSQL, fwArgs := where.Where()
if fwArgs == nil {
fwArgs = []any{}
fwArgs = make([]any, 0)
}
wtStr := WhereTypeAnd // default to AND
wt, ok := where.(WhereTyper)
Expand All @@ -59,7 +59,7 @@ func WithJoin(join Joiner) PatchOpt {
}
fjSQL, fjArgs := join.Join()
if fjArgs == nil {
fjArgs = []any{}
fjArgs = make([]any, 0)
}
s.joinSql.WriteString(strings.TrimSpace(fjSQL))
s.joinSql.WriteString("\n")
Expand Down

0 comments on commit 63f6d7b

Please sign in to comment.