diff --git a/base/res_field.go b/base/res_field.go index 4e223fe..e33d6b3 100644 --- a/base/res_field.go +++ b/base/res_field.go @@ -1,6 +1,7 @@ package base import ( + "encoding/json" "fmt" "reflect" "strconv" @@ -126,17 +127,30 @@ func (this ResField) ToValue(v any) any { return this.Default } - return v + if str, ok := v.(string); ok { + return str + } else if b, err := json.Marshal(v); err == nil { + return string(b) + } else { + panic(fmt.Sprintf("%s, 类型转换错误, %s", this.Code, err)) + } } return strings.Trim(cast.ToString(v), " ") } func (this ResField) GetRawDefault(driver string) db.Expression { - if this.DataType == "json" && this.Default == "" { + if this.DataType == contracts.ResDataType_Json && this.Default == "" { return db.Raw("'{}'") } + if this.DataType == contracts.ResDataType_Boolean { + if v, _ := strconv.ParseBool(this.Default); v { + return db.Raw("'1'") + } + return db.Raw("'0'") + } + if len(this.Default) > 4 && strings.ToLower(this.Default[0:4]) == "sql:" { sql := strings.ToLower(this.Default[4:]) if sql == "uuid()" {