From 1719312be7c8cf17d83caa035505d5c6b6e0cb18 Mon Sep 17 00:00:00 2001 From: thinkgos Date: Fri, 15 Mar 2024 09:26:37 +0000 Subject: [PATCH] feat: add model parser to rapier --- model_parse.go | 27 +++++----- well_known_type.go | 120 ++++++++++++--------------------------------- 2 files changed, 45 insertions(+), 102 deletions(-) diff --git a/model_parse.go b/model_parse.go index ad1a79d..e522f4d 100644 --- a/model_parse.go +++ b/model_parse.go @@ -3,8 +3,10 @@ package ens import ( "fmt" "reflect" + "strings" "github.com/things-go/ens/utils" + "gorm.io/gorm/schema" ) func ParseModel(v any) (MixinEntity, error) { @@ -59,15 +61,10 @@ func structFieldToFielder(fv reflect.StructField) Fielder { } fieldName := utils.SnakeCase(fv.Name) - // setting := schema.ParseTagSetting(fv.Tag.Get("gorm"), ";") - // if v, ok := setting["COLUMN"]; ok && v != "" { - // fieldName = v - // } - ident := fvt.String() return Field( &GoType{ - Type: intoGoTypeType(fvt.Kind(), ident), + Type: intoGoTypeType(fvt, fv.Tag), Ident: ident, PkgPath: fvt.PkgPath(), PkgQualifier: PkgQualifier(ident), @@ -77,8 +74,9 @@ func structFieldToFielder(fv reflect.StructField) Fielder { ) } -func intoGoTypeType(kind reflect.Kind, ident string) Type { - switch kind { +func intoGoTypeType(t reflect.Type, tag reflect.StructTag) Type { + ident := t.String() + switch t.Kind() { case reflect.Bool: return TypeBool case reflect.Int: @@ -106,10 +104,14 @@ func intoGoTypeType(kind reflect.Kind, ident string) Type { case reflect.Float64: return TypeFloat64 case reflect.String: + typeValue := schema.ParseTagSetting(tag.Get("gorm"), ";")["TYPE"] + if strings.Contains(strings.ToUpper(typeValue), "DECIMAL") { + return TypeDecimal + } return TypeString case reflect.Struct: switch ident { - case "time.Time", "sql.NullTime": + case "time.Time", "sql.NullTime", "datatypes.Date": return TypeTime case "sql.NullBool": return TypeBool @@ -129,11 +131,12 @@ func intoGoTypeType(kind reflect.Kind, ident string) Type { return TypeOther } case reflect.Slice: - if ident == "json.RawMessage" { + if ident == "json.RawMessage" || ident == "datatypes.JSON" { return TypeJSON } - fallthrough - // case reflect.Array: + return TypeBytes + case reflect.Array: + return TypeBytes default: return TypeOther } diff --git a/well_known_type.go b/well_known_type.go index d54c4ed..f99b09f 100644 --- a/well_known_type.go +++ b/well_known_type.go @@ -20,93 +20,33 @@ var sqlNullValueGoType = map[Type]*GoType{ TypeTime: SQLNullTimeType(), } -func BoolType() *GoType { - return NewGoType(TypeBool, false) -} -func IntType() *GoType { - return NewGoType(TypeInt, int(0)) -} -func Int8Type() *GoType { - return NewGoType(TypeInt8, int8(0)) -} -func Int16Type() *GoType { - return NewGoType(TypeInt16, int16(0)) -} -func Int32Type() *GoType { - return NewGoType(TypeInt32, int32(0)) -} -func Int64Type() *GoType { - return NewGoType(TypeInt64, int64(0)) -} -func UintType() *GoType { - return NewGoType(TypeUint, uint(0)) -} -func Uint8Type() *GoType { - return NewGoType(TypeUint8, uint8(0)) -} -func Uint16Type() *GoType { - return NewGoType(TypeUint16, uint16(0)) -} -func Uint32Type() *GoType { - return NewGoType(TypeUint32, uint32(0)) -} -func Uint64Type() *GoType { - return NewGoType(TypeUint64, uint64(0)) -} -func Float32Type() *GoType { - return NewGoType(TypeFloat32, float32(0)) -} -func Float64Type() *GoType { - return NewGoType(TypeFloat64, float64(0)) -} -func StringType() *GoType { - return NewGoType(TypeString, "") -} -func DecimalType() *GoType { - return NewGoType(TypeDecimal, "") -} -func EnumType() *GoType { - return NewGoType(TypeEnum, "") -} -func TimeType() *GoType { - return NewGoType(TypeTime, time.Time{}) -} -func BytesType() *GoType { - return NewGoType(TypeBytes, []byte(nil)) -} -func SQLNullBoolType() *GoType { - return NewGoType(TypeBool, sql.NullBool{}) -} -func SQLNullByteType() *GoType { - return NewGoType(TypeUint8, sql.NullByte{}) -} -func SQLNullFloat64Type() *GoType { - return NewGoType(TypeFloat64, sql.NullFloat64{}) -} -func SQLNullInt16Type() *GoType { - return NewGoType(TypeInt16, sql.NullInt16{}) -} -func SQLNullInt32Type() *GoType { - return NewGoType(TypeInt32, sql.NullInt32{}) -} -func SQLNullInt64Type() *GoType { - return NewGoType(TypeInt64, sql.NullInt64{}) -} -func SQLNullStringType() *GoType { - return NewGoType(TypeString, sql.NullString{}) -} -func SQLNullTimeType() *GoType { - return NewGoType(TypeTime, sql.NullTime{}) -} -func JSONRawMessageType() *GoType { - return NewGoType(TypeJSON, json.RawMessage{}) -} -func SoftDeleteType() *GoType { - return NewGoType(TypeInt64, soft_delete.DeletedAt(0)) -} -func DatatypesDateType() *GoType { - return NewGoType(TypeTime, datatypes.Date{}) -} -func DatatypesJSONType() *GoType { - return NewGoType(TypeJSON, datatypes.JSON{}) -} +func BoolType() *GoType { return NewGoType(TypeBool, false) } +func IntType() *GoType { return NewGoType(TypeInt, int(0)) } +func Int8Type() *GoType { return NewGoType(TypeInt8, int8(0)) } +func Int16Type() *GoType { return NewGoType(TypeInt16, int16(0)) } +func Int32Type() *GoType { return NewGoType(TypeInt32, int32(0)) } +func Int64Type() *GoType { return NewGoType(TypeInt64, int64(0)) } +func UintType() *GoType { return NewGoType(TypeUint, uint(0)) } +func Uint8Type() *GoType { return NewGoType(TypeUint8, uint8(0)) } +func Uint16Type() *GoType { return NewGoType(TypeUint16, uint16(0)) } +func Uint32Type() *GoType { return NewGoType(TypeUint32, uint32(0)) } +func Uint64Type() *GoType { return NewGoType(TypeUint64, uint64(0)) } +func Float32Type() *GoType { return NewGoType(TypeFloat32, float32(0)) } +func Float64Type() *GoType { return NewGoType(TypeFloat64, float64(0)) } +func StringType() *GoType { return NewGoType(TypeString, "") } +func DecimalType() *GoType { return NewGoType(TypeDecimal, "") } +func EnumType() *GoType { return NewGoType(TypeEnum, "") } +func TimeType() *GoType { return NewGoType(TypeTime, time.Time{}) } +func BytesType() *GoType { return NewGoType(TypeBytes, []byte(nil)) } +func SQLNullBoolType() *GoType { return NewGoType(TypeBool, sql.NullBool{}) } +func SQLNullByteType() *GoType { return NewGoType(TypeUint8, sql.NullByte{}) } +func SQLNullFloat64Type() *GoType { return NewGoType(TypeFloat64, sql.NullFloat64{}) } +func SQLNullInt16Type() *GoType { return NewGoType(TypeInt16, sql.NullInt16{}) } +func SQLNullInt32Type() *GoType { return NewGoType(TypeInt32, sql.NullInt32{}) } +func SQLNullInt64Type() *GoType { return NewGoType(TypeInt64, sql.NullInt64{}) } +func SQLNullStringType() *GoType { return NewGoType(TypeString, sql.NullString{}) } +func SQLNullTimeType() *GoType { return NewGoType(TypeTime, sql.NullTime{}) } +func JSONRawMessageType() *GoType { return NewGoType(TypeJSON, json.RawMessage{}) } +func SoftDeleteType() *GoType { return NewGoType(TypeInt64, soft_delete.DeletedAt(0)) } +func DatatypesDateType() *GoType { return NewGoType(TypeTime, datatypes.Date{}) } +func DatatypesJSONType() *GoType { return NewGoType(TypeJSON, datatypes.JSON{}) }