From 63f6d7b943cc4e49850d852ea376157628215cd1 Mon Sep 17 00:00:00 2001 From: Jacob Brewer Date: Sun, 3 Nov 2024 12:07:42 +0000 Subject: [PATCH] chore(inserter): initializing on an empty array by default (#63) * initializing the fields on default * initializing the fields on default * using default behaviour --- inserter/batch.go | 12 ++++++++++-- patch.go | 12 ++++++++++-- patch_opts.go | 4 ++-- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/inserter/batch.go b/inserter/batch.go index 3a22047..bc5390f 100644 --- a/inserter/batch.go +++ b/inserter/batch.go @@ -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: "", @@ -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 } diff --git a/patch.go b/patch.go index 9dffffe..d0c0d70 100644 --- a/patch.go +++ b/patch.go @@ -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: "", @@ -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 } diff --git a/patch_opts.go b/patch_opts.go index b050b81..1c671b4 100644 --- a/patch_opts.go +++ b/patch_opts.go @@ -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) @@ -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")