[fix] ToValue 数据类型转换

This commit is contained in:
what 2023-07-31 21:30:23 +08:00
parent cd4e1b4789
commit 3f44f0335c

View File

@ -1,6 +1,7 @@
package base package base
import ( import (
"encoding/json"
"fmt" "fmt"
"reflect" "reflect"
"strconv" "strconv"
@ -126,17 +127,30 @@ func (this ResField) ToValue(v any) any {
return this.Default 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), " ") return strings.Trim(cast.ToString(v), " ")
} }
func (this ResField) GetRawDefault(driver string) db.Expression { 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("'{}'") 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:" { if len(this.Default) > 4 && strings.ToLower(this.Default[0:4]) == "sql:" {
sql := strings.ToLower(this.Default[4:]) sql := strings.ToLower(this.Default[4:])
if sql == "uuid()" { if sql == "uuid()" {