Skip to content

Commit 6707e64

Browse files
committed
fix: update slice bug
1 parent bee4eea commit 6707e64

File tree

4 files changed

+24
-31
lines changed

4 files changed

+24
-31
lines changed

internal/dao/userExample.go.mgo

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ func (d *userExampleDao) Create(ctx context.Context, record *model.UserExample)
6161
if record.ID.IsZero() {
6262
record.ID = primitive.NewObjectID()
6363
}
64-
if record.CreatedAt.IsZero() {
65-
record.CreatedAt = time.Now()
66-
record.UpdatedAt = time.Now()
67-
}
64+
now := time.Now()
65+
record.CreatedAt = &now
66+
record.UpdatedAt = &now
67+
6868
_, err := d.collection.InsertOne(ctx, record)
6969

7070
_ = d.deleteCache(ctx, record.ID.Hex())

internal/dao/userExample.go.mgo.exp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ func (d *userExampleDao) Create(ctx context.Context, record *model.UserExample)
6666
if record.ID.IsZero() {
6767
record.ID = primitive.NewObjectID()
6868
}
69-
if record.CreatedAt.IsZero() {
70-
record.CreatedAt = time.Now()
71-
record.UpdatedAt = time.Now()
72-
}
69+
now := time.Now()
70+
record.CreatedAt = &now
71+
record.UpdatedAt = &now
72+
7373
_, err := d.collection.InsertOne(ctx, record)
7474

7575
_ = d.deleteCache(ctx, record.ID.Hex())

pkg/mgo/model.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@ import (
1010
// Model embedded structs, add `bson: ",inline"` when defining table structs
1111
type Model struct {
1212
ID primitive.ObjectID `bson:"_id" json:"id"`
13-
CreatedAt time.Time `bson:"created_at" json:"createdAt"`
14-
UpdatedAt time.Time `bson:"updated_at" json:"updatedAt"`
13+
CreatedAt *time.Time `bson:"created_at" json:"createdAt"`
14+
UpdatedAt *time.Time `bson:"updated_at" json:"updatedAt"`
1515
DeletedAt *time.Time `bson:"deleted_at,omitempty" json:"deletedAt,omitempty"`
1616
}
1717

1818
// SetModelValue set model fields
1919
func (p *Model) SetModelValue() {
20-
now := time.Now()
2120
if !p.ID.IsZero() {
2221
p.ID = primitive.NewObjectID()
2322
}
2423

25-
if p.CreatedAt.IsZero() {
26-
p.CreatedAt = now
27-
p.UpdatedAt = now
28-
}
24+
now := time.Now()
25+
p.CreatedAt = &now
26+
p.UpdatedAt = &now
2927
}
3028

3129
// ExcludeDeleted exclude soft deleted records

pkg/sql2code/parser/parser.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (t tmplField) ConditionZero() string {
212212
return ` != ""`
213213
case "time.Time", "*time.Time", "sql.NullTime": //nolint
214214
return ` != nil && table.` + t.Name + `.IsZero() == false`
215-
case "[]byte", "[]string", "[]int", "interface{}": //nolint
215+
case "interface{}": //nolint
216216
return ` != nil` //nolint
217217
case "bool": //nolint
218218
return ` != false`
@@ -222,12 +222,13 @@ func (t tmplField) ConditionZero() string {
222222
if t.GoType == goTypeOID {
223223
return ` != primitive.NilObjectID`
224224
}
225-
if t.GoType == "*"+t.Name {
226-
return ` != nil` //nolint
227-
}
228-
if strings.Contains(t.GoType, "[]") {
229-
return ` != nil` //nolint
230-
}
225+
}
226+
227+
if t.GoType == "*"+t.Name {
228+
return ` != nil` //nolint
229+
}
230+
if strings.Contains(t.GoType, "[]") {
231+
return ` != nil && len(table.` + t.Name + `) > 0` //nolint
231232
}
232233

233234
if t.GoType == "" {
@@ -686,9 +687,6 @@ func getModelStructCode(data tmplData, importPaths []string, isEmbed bool, jsonN
686687
}
687688
if field.rewriterField != nil {
688689
switch field.rewriterField.goType {
689-
//case jsonTypeName, decimalTypeName:
690-
// field.GoType = field.rewriterField.goType
691-
// importPaths = append(importPaths, field.rewriterField.path)
692690
case jsonTypeName, decimalTypeName, boolTypeName, boolTypeTinyName:
693691
field.GoType = "*" + field.rewriterField.goType
694692
importPaths = append(importPaths, field.rewriterField.path)
@@ -716,6 +714,9 @@ func getModelStructCode(data tmplData, importPaths []string, isEmbed bool, jsonN
716714
newImportPaths = append(newImportPaths, "github.com/go-dev-frame/sponge/pkg/sgorm")
717715
} else {
718716
for _, field := range data.Fields {
717+
if strings.Contains(field.GoType, "time.Time") {
718+
field.GoType = "*time.Time"
719+
}
719720
switch field.DBDriver {
720721
case DBDriverMongodb:
721722
if field.Name == "ID" {
@@ -724,9 +725,6 @@ func getModelStructCode(data tmplData, importPaths []string, isEmbed bool, jsonN
724725
}
725726

726727
default:
727-
if strings.Contains(field.GoType, "time.Time") {
728-
field.GoType = "*time.Time"
729-
}
730728
// force conversion of ID field to uint64 type
731729
if field.Name == "ID" {
732730
field.GoType = "uint64"
@@ -737,9 +735,6 @@ func getModelStructCode(data tmplData, importPaths []string, isEmbed bool, jsonN
737735
if field.DBDriver == DBDriverMysql || field.DBDriver == DBDriverPostgresql || field.DBDriver == DBDriverTidb {
738736
if field.rewriterField != nil {
739737
switch field.rewriterField.goType {
740-
//case jsonTypeName, decimalTypeName:
741-
// field.GoType = field.rewriterField.goType
742-
// importPaths = append(importPaths, field.rewriterField.path)
743738
case jsonTypeName, decimalTypeName, boolTypeName, boolTypeTinyName:
744739
field.GoType = "*" + field.rewriterField.goType
745740
importPaths = append(importPaths, field.rewriterField.path)

0 commit comments

Comments
 (0)