fix: update slice bug

This commit is contained in:
zhuyasen
2025-08-24 20:25:07 +08:00
parent bee4eeabe1
commit 6707e646da
4 changed files with 24 additions and 31 deletions

View File

@@ -61,10 +61,10 @@ func (d *userExampleDao) Create(ctx context.Context, record *model.UserExample)
if record.ID.IsZero() { if record.ID.IsZero() {
record.ID = primitive.NewObjectID() record.ID = primitive.NewObjectID()
} }
if record.CreatedAt.IsZero() { now := time.Now()
record.CreatedAt = time.Now() record.CreatedAt = &now
record.UpdatedAt = time.Now() record.UpdatedAt = &now
}
_, err := d.collection.InsertOne(ctx, record) _, err := d.collection.InsertOne(ctx, record)
_ = d.deleteCache(ctx, record.ID.Hex()) _ = d.deleteCache(ctx, record.ID.Hex())

View File

@@ -66,10 +66,10 @@ func (d *userExampleDao) Create(ctx context.Context, record *model.UserExample)
if record.ID.IsZero() { if record.ID.IsZero() {
record.ID = primitive.NewObjectID() record.ID = primitive.NewObjectID()
} }
if record.CreatedAt.IsZero() { now := time.Now()
record.CreatedAt = time.Now() record.CreatedAt = &now
record.UpdatedAt = time.Now() record.UpdatedAt = &now
}
_, err := d.collection.InsertOne(ctx, record) _, err := d.collection.InsertOne(ctx, record)
_ = d.deleteCache(ctx, record.ID.Hex()) _ = d.deleteCache(ctx, record.ID.Hex())

View File

@@ -10,22 +10,20 @@ import (
// Model embedded structs, add `bson: ",inline"` when defining table structs // Model embedded structs, add `bson: ",inline"` when defining table structs
type Model struct { type Model struct {
ID primitive.ObjectID `bson:"_id" json:"id"` ID primitive.ObjectID `bson:"_id" json:"id"`
CreatedAt time.Time `bson:"created_at" json:"createdAt"` CreatedAt *time.Time `bson:"created_at" json:"createdAt"`
UpdatedAt time.Time `bson:"updated_at" json:"updatedAt"` UpdatedAt *time.Time `bson:"updated_at" json:"updatedAt"`
DeletedAt *time.Time `bson:"deleted_at,omitempty" json:"deletedAt,omitempty"` DeletedAt *time.Time `bson:"deleted_at,omitempty" json:"deletedAt,omitempty"`
} }
// SetModelValue set model fields // SetModelValue set model fields
func (p *Model) SetModelValue() { func (p *Model) SetModelValue() {
now := time.Now()
if !p.ID.IsZero() { if !p.ID.IsZero() {
p.ID = primitive.NewObjectID() p.ID = primitive.NewObjectID()
} }
if p.CreatedAt.IsZero() { now := time.Now()
p.CreatedAt = now p.CreatedAt = &now
p.UpdatedAt = now p.UpdatedAt = &now
}
} }
// ExcludeDeleted exclude soft deleted records // ExcludeDeleted exclude soft deleted records

View File

@@ -212,7 +212,7 @@ func (t tmplField) ConditionZero() string {
return ` != ""` return ` != ""`
case "time.Time", "*time.Time", "sql.NullTime": //nolint case "time.Time", "*time.Time", "sql.NullTime": //nolint
return ` != nil && table.` + t.Name + `.IsZero() == false` return ` != nil && table.` + t.Name + `.IsZero() == false`
case "[]byte", "[]string", "[]int", "interface{}": //nolint case "interface{}": //nolint
return ` != nil` //nolint return ` != nil` //nolint
case "bool": //nolint case "bool": //nolint
return ` != false` return ` != false`
@@ -222,12 +222,13 @@ func (t tmplField) ConditionZero() string {
if t.GoType == goTypeOID { if t.GoType == goTypeOID {
return ` != primitive.NilObjectID` return ` != primitive.NilObjectID`
} }
if t.GoType == "*"+t.Name { }
return ` != nil` //nolint
} if t.GoType == "*"+t.Name {
if strings.Contains(t.GoType, "[]") { return ` != nil` //nolint
return ` != nil` //nolint }
} if strings.Contains(t.GoType, "[]") {
return ` != nil && len(table.` + t.Name + `) > 0` //nolint
} }
if t.GoType == "" { if t.GoType == "" {
@@ -686,9 +687,6 @@ func getModelStructCode(data tmplData, importPaths []string, isEmbed bool, jsonN
} }
if field.rewriterField != nil { if field.rewriterField != nil {
switch field.rewriterField.goType { switch field.rewriterField.goType {
//case jsonTypeName, decimalTypeName:
// field.GoType = field.rewriterField.goType
// importPaths = append(importPaths, field.rewriterField.path)
case jsonTypeName, decimalTypeName, boolTypeName, boolTypeTinyName: case jsonTypeName, decimalTypeName, boolTypeName, boolTypeTinyName:
field.GoType = "*" + field.rewriterField.goType field.GoType = "*" + field.rewriterField.goType
importPaths = append(importPaths, field.rewriterField.path) importPaths = append(importPaths, field.rewriterField.path)
@@ -716,6 +714,9 @@ func getModelStructCode(data tmplData, importPaths []string, isEmbed bool, jsonN
newImportPaths = append(newImportPaths, "github.com/go-dev-frame/sponge/pkg/sgorm") newImportPaths = append(newImportPaths, "github.com/go-dev-frame/sponge/pkg/sgorm")
} else { } else {
for _, field := range data.Fields { for _, field := range data.Fields {
if strings.Contains(field.GoType, "time.Time") {
field.GoType = "*time.Time"
}
switch field.DBDriver { switch field.DBDriver {
case DBDriverMongodb: case DBDriverMongodb:
if field.Name == "ID" { if field.Name == "ID" {
@@ -724,9 +725,6 @@ func getModelStructCode(data tmplData, importPaths []string, isEmbed bool, jsonN
} }
default: default:
if strings.Contains(field.GoType, "time.Time") {
field.GoType = "*time.Time"
}
// force conversion of ID field to uint64 type // force conversion of ID field to uint64 type
if field.Name == "ID" { if field.Name == "ID" {
field.GoType = "uint64" field.GoType = "uint64"
@@ -737,9 +735,6 @@ func getModelStructCode(data tmplData, importPaths []string, isEmbed bool, jsonN
if field.DBDriver == DBDriverMysql || field.DBDriver == DBDriverPostgresql || field.DBDriver == DBDriverTidb { if field.DBDriver == DBDriverMysql || field.DBDriver == DBDriverPostgresql || field.DBDriver == DBDriverTidb {
if field.rewriterField != nil { if field.rewriterField != nil {
switch field.rewriterField.goType { switch field.rewriterField.goType {
//case jsonTypeName, decimalTypeName:
// field.GoType = field.rewriterField.goType
// importPaths = append(importPaths, field.rewriterField.path)
case jsonTypeName, decimalTypeName, boolTypeName, boolTypeTinyName: case jsonTypeName, decimalTypeName, boolTypeName, boolTypeTinyName:
field.GoType = "*" + field.rewriterField.goType field.GoType = "*" + field.rewriterField.goType
importPaths = append(importPaths, field.rewriterField.path) importPaths = append(importPaths, field.rewriterField.path)