From 3f44f0335c730790e1af8b1cb77db67d8503b49e Mon Sep 17 00:00:00 2001 From: what Date: Mon, 31 Jul 2023 21:30:23 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20ToValue=20=E6=95=B0=E6=8D=AE=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/res_field.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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()" {