Skip to content

Commit

Permalink
feat: add model parser to rapier
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Mar 15, 2024
1 parent bb355f3 commit 1719312
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 102 deletions.
27 changes: 15 additions & 12 deletions model_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
120 changes: 30 additions & 90 deletions well_known_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) }

0 comments on commit 1719312

Please sign in to comment.