Skip to content

Commit

Permalink
fix: add rapier
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Sep 13, 2023
1 parent 4d69637 commit 2bf29d3
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 10 deletions.
5 changes: 3 additions & 2 deletions codegen/assist.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/things-go/ens/utils"
)

// Deprecated: Don't use this, deprecated next major version.
func (g *CodeGen) GenAssist(modelImportPath string) *CodeGen {
pkgQualifierPrefix := ""
if p := ens.PkgName(modelImportPath); p != "" {
Expand Down Expand Up @@ -66,7 +67,7 @@ func (g *CodeGen) GenAssist(modelImportPath string) *CodeGen {
g.Println("xAlias string")
g.Println("ALL assist.Asterisk")
for _, field := range et.Fields {
g.Printf("%s assist.%s\n", utils.CamelCase(field.Name), field.AssistDataType)
g.Printf("%s assist.%s\n", utils.CamelCase(field.Name), field.RapierDataType)
}
g.Println("}")
g.Println()
Expand All @@ -87,7 +88,7 @@ func (g *CodeGen) GenAssist(modelImportPath string) *CodeGen {
g.Println("ALL: assist.NewAsterisk(xAlias),")
for _, field := range et.Fields {
fieldName := utils.CamelCase(field.Name)
g.Printf("%s: assist.New%s(xAlias, %s),\n", fieldName, field.AssistDataType, constField(structName, fieldName))
g.Printf("%s: assist.New%s(xAlias, %s),\n", fieldName, field.RapierDataType, constField(structName, fieldName))
}
g.Println("}")
g.Println("}")
Expand Down
201 changes: 201 additions & 0 deletions codegen/rapier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package codegen

import (
"fmt"
"strings"

"github.com/things-go/ens"
"github.com/things-go/ens/utils"
)

func (g *CodeGen) GenRapier(modelImportPath string) *CodeGen {
pkgQualifierPrefix := ""
if p := ens.PkgName(modelImportPath); p != "" {
pkgQualifierPrefix = p + "."
}
if !g.disableDocComment {
g.Printf("// Code generated by %s. DO NOT EDIT.\n", g.byName)
g.Printf("// version: %s\n", g.version)
g.Println()
}
g.Printf("package %s\n", g.packageName)
g.Println()

//* import
g.Println("import (")
if pkgQualifierPrefix != "" {
g.Printf("\"%s\"\n", modelImportPath)
g.Println()
}
g.Println(`rapier "github.com/thinkgos/gorm-rapier"`)
g.Println(`"gorm.io/gorm"`)
g.Println(")")

constField := func(structName, fieldName string) string {
return fmt.Sprintf(`xx_%s_%s`, structName, fieldName)
}
//* struct
for _, et := range g.entities {
structName := utils.CamelCase(et.Name)
tableName := et.Name

constTableName := fmt.Sprintf("xx_%s_TableName", structName)
{ //* const field
g.Println("const (")
g.Printf("// hold model `%s` table name\n", structName)
g.Printf("%s = \"%s\"\n", constTableName, tableName)
g.Printf("// hold model `%s` column name\n", structName)
for _, field := range et.Fields {
g.Printf("%s = \"%s\"\n", constField(structName, utils.CamelCase(field.Name)), field.Name)
}
g.Println(")")
g.Println()
}

varModel := fmt.Sprintf(`xxx_%s_Model`, structName)
funcInnerNew := fmt.Sprintf(`new_%s`, structName)
{ //* var field
g.Printf("var %s = %s (%s)\n", varModel, funcInnerNew, constTableName)
g.Println()
}

typeNative := fmt.Sprintf("%s_Native", structName)
//* type
{
g.Printf("type %s struct {\n", typeNative)
g.Println("xAlias string")
g.Println("ALL rapier.Asterisk")
for _, field := range et.Fields {
g.Printf("%s rapier.%s\n", utils.CamelCase(field.Name), field.RapierDataType)
}
g.Println("}")
g.Println()
}
//* function X_xxx
{
g.Printf("// X_%s model with TableName `%s`.\n", structName, tableName)
g.Printf("func X_%s() %s {\n", structName, typeNative)
g.Printf("return %s\n", varModel)
g.Println("}")
g.Println()
}
//* function new_xxx
{
g.Printf("func %s(xAlias string) %s {\n", funcInnerNew, typeNative)
g.Printf("return %s {\n", typeNative)
g.Println("xAlias: xAlias,")
g.Println("ALL: rapier.NewAsterisk(xAlias),")
for _, field := range et.Fields {
fieldName := utils.CamelCase(field.Name)
g.Printf("%s: rapier.New%s(xAlias, %s),\n", fieldName, field.RapierDataType, constField(structName, fieldName))
}
g.Println("}")
g.Println("}")
g.Println()
}
//* function New_xxxx
{
g.Printf("// New_%s new instance.\n", structName)
g.Printf("func New_%s(xAlias string) %s {\n", structName, typeNative)
g.Printf("if xAlias == %s {\n", constTableName)
g.Printf("return %s\n", varModel)
g.Println("} else {")
g.Printf("return %s(xAlias)\n", funcInnerNew)
g.Println("}")
g.Println("}")
g.Println()
}
//* method As
{
g.Println("// As alias")
g.Printf("func (*%[1]s) As(alias string) %[1]s {\n", typeNative)
g.Printf("return New_%s(alias)\n", structName)
g.Println("}")
g.Println()
}
//* method X_Alias
{
g.Printf("// X_Alias hold table name when call New_%[1]s or %[1]s_Native.As that you defined.\n", structName)
g.Printf("func (x *%s) X_Alias() string {\n", typeNative)
g.Println("return x.xAlias")
g.Println("}")
g.Println()
}
// impl TableName interface
{
//* method TableName
g.Printf("// TableName hold model `%s` table name returns `%s`.\n", structName, tableName)
g.Printf("func (*%s) TableName() string {\n", typeNative)
g.Printf("return %s\n", constTableName)
g.Println("}")
g.Println()
}

//* method New_Executor
{
modelName := pkgQualifierPrefix + structName
g.Println("// New_Executor new entity executor which suggest use only once.")
g.Printf("func (*%s) New_Executor(db *gorm.DB) *rapier.Executor[%s] {\n", typeNative, modelName)
g.Printf("return rapier.NewExecutor[%s](db)\n", modelName)
g.Println("}")
g.Println()
}
//* method Select_Expr
{
g.Println("// Select_Expr select model fields")
g.Printf("func (x *%s) Select_Expr() []rapier.Expr {\n", typeNative)
g.Println("return []rapier.Expr{")
for _, field := range et.Fields {
g.Printf("x.%s,\n", utils.CamelCase(field.Name))
}
g.Println("}")
g.Println("}")
g.Println()
}

//* method Select_VariantExpr
{
g.Println("// Select_VariantExpr select model fields, but time.Time field convert to timestamp(int64).")
g.Printf("func (x *%s) Select_VariantExpr(prefixes ...string) []rapier.Expr {\n", typeNative)
g.Println("if len(prefixes) > 0 && prefixes[0] != \"\" {")
g.Println("return []rapier.Expr{")
for _, field := range et.Fields {
g.Println(genRapier_SelectVariantExprField(structName, field, true))
}
g.Println("}")
g.Println("} else {")
g.Println("return []rapier.Expr{")
for _, field := range et.Fields {
g.Println(genRapier_SelectVariantExprField(structName, field, false))
}
g.Println("}")
g.Println("}")
g.Println("}")
g.Println()
}
}
return g
}

func genRapier_SelectVariantExprField(structName string, field *ens.FieldDescriptor, hasPrefix bool) string {
fieldName := utils.CamelCase(field.Name)

b := &strings.Builder{}
b.Grow(64)
b.WriteString("x.")
b.WriteString(fieldName)
if field.Type.IsTime() {
b.WriteString(".UnixTimestamp()")
if field.Nullable {
b.WriteString(".IfNull(0)")
}
if !hasPrefix {
fmt.Fprintf(b, ".As(x.%s.ColumnName())", fieldName)
}
}
if hasPrefix {
fmt.Fprintf(b, ".As(x.%s.FieldName(prefixes...))", fieldName)
}
b.WriteString(",")
return b.String()
}
14 changes: 7 additions & 7 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ type FieldDescriptor struct {
Type *GoType // go type information.
Optional bool // nullable struct field.
Tags []string // Tags struct tag
AssistDataType string // assist data type
RapierDataType string // rapier data type
}

func (f *FieldDescriptor) goType(typ any) {
f.Type = NewGoType(f.Type.Type, typ)
}

func (field *FieldDescriptor) build(opt *Option) {
field.AssistDataType = field.Type.Type.IntoAssistDataType()
field.RapierDataType = field.Type.Type.IntoRapierDataType()
if field.Name == "deleted_at" && field.Type.IsInteger() {
field.Optional = false
field.goType(soft_delete.DeletedAt(0))
Expand All @@ -40,25 +40,25 @@ func (field *FieldDescriptor) build(opt *Option) {
switch field.Type.Type {
case TypeInt8, TypeInt16, TypeInt32:
field.goType(int(0))
field.AssistDataType = TypeInt.IntoAssistDataType()
field.RapierDataType = TypeInt.IntoRapierDataType()
case TypeUint8, TypeUint16, TypeUint32:
field.goType(uint(0))
field.AssistDataType = TypeUint.IntoAssistDataType()
field.RapierDataType = TypeUint.IntoRapierDataType()
}
}
if opt.EnableIntegerInt {
switch field.Type.Type {
case TypeInt32:
field.goType(int(0))
field.AssistDataType = TypeInt.IntoAssistDataType()
field.RapierDataType = TypeInt.IntoRapierDataType()
case TypeUint32:
field.goType(uint(0))
field.AssistDataType = TypeUint.IntoAssistDataType()
field.RapierDataType = TypeUint.IntoRapierDataType()
}
}
if opt.EnableBoolInt && field.Type.IsBool() {
field.goType(int(0))
field.AssistDataType = TypeInt.IntoAssistDataType()
field.RapierDataType = TypeInt.IntoRapierDataType()
}
if field.Nullable && opt.DisableNullToPoint {
gt, ok := sqlNullValueGoType[field.Type.Type]
Expand Down
2 changes: 1 addition & 1 deletion go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (t Type) IntoProtoDataType() string {
return dataType
}

func (t Type) IntoAssistDataType() string {
func (t Type) IntoRapierDataType() string {
switch t {
case TypeBool,
TypeInt8, TypeInt16, TypeInt32, TypeInt64, TypeInt,
Expand Down

0 comments on commit 2bf29d3

Please sign in to comment.