Skip to content

Commit 63f6d7b

Browse files
authored
chore(inserter): initializing on an empty array by default (#63)
* initializing the fields on default * initializing the fields on default * using default behaviour
1 parent 4cf97e3 commit 63f6d7b

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

inserter/batch.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ type SQLBatch struct {
4949
// newBatchDefaults returns a new SQLBatch with default values
5050
func newBatchDefaults(opts ...BatchOpt) *SQLBatch {
5151
b := &SQLBatch{
52-
fields: nil,
53-
args: nil,
52+
fields: make([]string, 0),
53+
args: make([]any, 0),
5454
db: nil,
5555
tagName: patcher.DefaultDbTagName,
5656
table: "",
@@ -64,10 +64,18 @@ func newBatchDefaults(opts ...BatchOpt) *SQLBatch {
6464
}
6565

6666
func (b *SQLBatch) Fields() []string {
67+
if len(b.fields) == 0 {
68+
// Default behaviour to return nil if no fields are set
69+
return nil
70+
}
6771
return b.fields
6872
}
6973

7074
func (b *SQLBatch) Args() []any {
75+
if len(b.args) == 0 {
76+
// Default behaviour to return nil if no args are set
77+
return nil
78+
}
7179
return b.args
7280
}
7381

patch.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ type SQLPatch struct {
7474
func newPatchDefaults(opts ...PatchOpt) *SQLPatch {
7575
// Default options
7676
p := &SQLPatch{
77-
fields: nil,
78-
args: nil,
77+
fields: make([]string, 0),
78+
args: make([]any, 0),
7979
db: nil,
8080
tagName: DefaultDbTagName,
8181
table: "",
@@ -97,10 +97,18 @@ func newPatchDefaults(opts ...PatchOpt) *SQLPatch {
9797
}
9898

9999
func (s *SQLPatch) Fields() []string {
100+
if len(s.fields) == 0 {
101+
// Default behaviour is to return nil if there are no fields
102+
return nil
103+
}
100104
return s.fields
101105
}
102106

103107
func (s *SQLPatch) Args() []any {
108+
if len(s.args) == 0 {
109+
// Default behaviour is to return nil if there are no args
110+
return nil
111+
}
104112
return s.args
105113
}
106114

patch_opts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func WithWhere(where Wherer) PatchOpt {
3737
}
3838
fwSQL, fwArgs := where.Where()
3939
if fwArgs == nil {
40-
fwArgs = []any{}
40+
fwArgs = make([]any, 0)
4141
}
4242
wtStr := WhereTypeAnd // default to AND
4343
wt, ok := where.(WhereTyper)
@@ -59,7 +59,7 @@ func WithJoin(join Joiner) PatchOpt {
5959
}
6060
fjSQL, fjArgs := join.Join()
6161
if fjArgs == nil {
62-
fjArgs = []any{}
62+
fjArgs = make([]any, 0)
6363
}
6464
s.joinSql.WriteString(strings.TrimSpace(fjSQL))
6565
s.joinSql.WriteString("\n")

0 commit comments

Comments
 (0)