Skip to content

Commit 95d30dd

Browse files
pr comments
1 parent ef6a395 commit 95d30dd

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

table.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ func (t *Table[T, PT, IDT]) Save(ctx context.Context, records ...PT) error {
4747
}
4848

4949
func (t *Table[T, PT, IDT]) saveOne(ctx context.Context, record PT) error {
50+
if record == nil {
51+
return nil
52+
}
53+
5054
if err := record.Validate(); err != nil {
5155
return fmt.Errorf("validate record: %w", err)
5256
}
@@ -56,7 +60,8 @@ func (t *Table[T, PT, IDT]) saveOne(ctx context.Context, record PT) error {
5660
}
5761

5862
// Insert
59-
if record.GetID() == *new(IDT) {
63+
var zero IDT
64+
if record.GetID() == zero {
6065
q := t.SQL.
6166
InsertRecord(record).
6267
Into(t.Name).
@@ -84,11 +89,15 @@ func (t *Table[T, PT, IDT]) saveAll(ctx context.Context, records []PT) error {
8489
now := time.Now().UTC()
8590

8691
insertRecords := make([]PT, 0)
87-
insertIndices := make([]int, 0) // keep track of original indices
92+
insertIndices := make([]int, 0) // keep track of original indices, so we can update the records with IDs in passed slice
8893

8994
updateQueries := make(Queries, 0)
9095

9196
for i, r := range records {
97+
if r == nil {
98+
continue
99+
}
100+
92101
if err := r.Validate(); err != nil {
93102
return fmt.Errorf("validate record: %w", err)
94103
}
@@ -97,7 +106,8 @@ func (t *Table[T, PT, IDT]) saveAll(ctx context.Context, records []PT) error {
97106
row.SetUpdatedAt(now)
98107
}
99108

100-
if r.GetID() == *new(IDT) {
109+
var zero IDT
110+
if r.GetID() == zero {
101111
if row, ok := any(r).(hasSetCreatedAt); ok {
102112
row.SetCreatedAt(now)
103113
}

0 commit comments

Comments
 (0)