mirror of
https://github.com/zhufuyi/sponge.git
synced 2025-09-26 20:51:14 +08:00
fix: update slice bug
This commit is contained in:
@@ -61,10 +61,10 @@ func (d *userExampleDao) Create(ctx context.Context, record *model.UserExample)
|
||||
if record.ID.IsZero() {
|
||||
record.ID = primitive.NewObjectID()
|
||||
}
|
||||
if record.CreatedAt.IsZero() {
|
||||
record.CreatedAt = time.Now()
|
||||
record.UpdatedAt = time.Now()
|
||||
}
|
||||
now := time.Now()
|
||||
record.CreatedAt = &now
|
||||
record.UpdatedAt = &now
|
||||
|
||||
_, err := d.collection.InsertOne(ctx, record)
|
||||
|
||||
_ = d.deleteCache(ctx, record.ID.Hex())
|
||||
|
@@ -66,10 +66,10 @@ func (d *userExampleDao) Create(ctx context.Context, record *model.UserExample)
|
||||
if record.ID.IsZero() {
|
||||
record.ID = primitive.NewObjectID()
|
||||
}
|
||||
if record.CreatedAt.IsZero() {
|
||||
record.CreatedAt = time.Now()
|
||||
record.UpdatedAt = time.Now()
|
||||
}
|
||||
now := time.Now()
|
||||
record.CreatedAt = &now
|
||||
record.UpdatedAt = &now
|
||||
|
||||
_, err := d.collection.InsertOne(ctx, record)
|
||||
|
||||
_ = d.deleteCache(ctx, record.ID.Hex())
|
||||
|
@@ -10,22 +10,20 @@ import (
|
||||
// Model embedded structs, add `bson: ",inline"` when defining table structs
|
||||
type Model struct {
|
||||
ID primitive.ObjectID `bson:"_id" json:"id"`
|
||||
CreatedAt time.Time `bson:"created_at" json:"createdAt"`
|
||||
UpdatedAt time.Time `bson:"updated_at" json:"updatedAt"`
|
||||
CreatedAt *time.Time `bson:"created_at" json:"createdAt"`
|
||||
UpdatedAt *time.Time `bson:"updated_at" json:"updatedAt"`
|
||||
DeletedAt *time.Time `bson:"deleted_at,omitempty" json:"deletedAt,omitempty"`
|
||||
}
|
||||
|
||||
// SetModelValue set model fields
|
||||
func (p *Model) SetModelValue() {
|
||||
now := time.Now()
|
||||
if !p.ID.IsZero() {
|
||||
p.ID = primitive.NewObjectID()
|
||||
}
|
||||
|
||||
if p.CreatedAt.IsZero() {
|
||||
p.CreatedAt = now
|
||||
p.UpdatedAt = now
|
||||
}
|
||||
now := time.Now()
|
||||
p.CreatedAt = &now
|
||||
p.UpdatedAt = &now
|
||||
}
|
||||
|
||||
// ExcludeDeleted exclude soft deleted records
|
||||
|
@@ -212,7 +212,7 @@ func (t tmplField) ConditionZero() string {
|
||||
return ` != ""`
|
||||
case "time.Time", "*time.Time", "sql.NullTime": //nolint
|
||||
return ` != nil && table.` + t.Name + `.IsZero() == false`
|
||||
case "[]byte", "[]string", "[]int", "interface{}": //nolint
|
||||
case "interface{}": //nolint
|
||||
return ` != nil` //nolint
|
||||
case "bool": //nolint
|
||||
return ` != false`
|
||||
@@ -222,12 +222,13 @@ func (t tmplField) ConditionZero() string {
|
||||
if t.GoType == goTypeOID {
|
||||
return ` != primitive.NilObjectID`
|
||||
}
|
||||
}
|
||||
|
||||
if t.GoType == "*"+t.Name {
|
||||
return ` != nil` //nolint
|
||||
}
|
||||
if strings.Contains(t.GoType, "[]") {
|
||||
return ` != nil` //nolint
|
||||
}
|
||||
return ` != nil && len(table.` + t.Name + `) > 0` //nolint
|
||||
}
|
||||
|
||||
if t.GoType == "" {
|
||||
@@ -686,9 +687,6 @@ func getModelStructCode(data tmplData, importPaths []string, isEmbed bool, jsonN
|
||||
}
|
||||
if field.rewriterField != nil {
|
||||
switch field.rewriterField.goType {
|
||||
//case jsonTypeName, decimalTypeName:
|
||||
// field.GoType = field.rewriterField.goType
|
||||
// importPaths = append(importPaths, field.rewriterField.path)
|
||||
case jsonTypeName, decimalTypeName, boolTypeName, boolTypeTinyName:
|
||||
field.GoType = "*" + field.rewriterField.goType
|
||||
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")
|
||||
} else {
|
||||
for _, field := range data.Fields {
|
||||
if strings.Contains(field.GoType, "time.Time") {
|
||||
field.GoType = "*time.Time"
|
||||
}
|
||||
switch field.DBDriver {
|
||||
case DBDriverMongodb:
|
||||
if field.Name == "ID" {
|
||||
@@ -724,9 +725,6 @@ func getModelStructCode(data tmplData, importPaths []string, isEmbed bool, jsonN
|
||||
}
|
||||
|
||||
default:
|
||||
if strings.Contains(field.GoType, "time.Time") {
|
||||
field.GoType = "*time.Time"
|
||||
}
|
||||
// force conversion of ID field to uint64 type
|
||||
if field.Name == "ID" {
|
||||
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.rewriterField != nil {
|
||||
switch field.rewriterField.goType {
|
||||
//case jsonTypeName, decimalTypeName:
|
||||
// field.GoType = field.rewriterField.goType
|
||||
// importPaths = append(importPaths, field.rewriterField.path)
|
||||
case jsonTypeName, decimalTypeName, boolTypeName, boolTypeTinyName:
|
||||
field.GoType = "*" + field.rewriterField.goType
|
||||
importPaths = append(importPaths, field.rewriterField.path)
|
||||
|
Reference in New Issue
Block a user