Skip to content

Commit

Permalink
feat: add rapier
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed May 9, 2024
1 parent b54e83a commit cbf2b91
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
7 changes: 2 additions & 5 deletions cmd/ormat/command/rapier.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type rapierOpt struct {
ModelImportPath string // required, model导入路径
DisableDocComment bool // 禁用doc注释
EnableInt bool // 使能int8,uint8,int16,uint16,int32,uint32输出为int,uint
EnableIntegerInt bool // 使能int32,uint32输出为int,uint
EnableBoolInt bool // 使能bool输出int
}

Expand Down Expand Up @@ -107,7 +106,6 @@ func newRapierCmd() *rapierCmd {
ModelImportPath: root.ModelImportPath,
DisableDocComment: root.DisableDocComment,
EnableInt: root.EnableInt,
EnableIntegerInt: root.EnableIntegerInt,
EnableBoolInt: root.EnableBoolInt,
}

Expand Down Expand Up @@ -138,9 +136,8 @@ func newRapierCmd() *rapierCmd {

cmd.Flags().StringVar(&root.PackageName, "package", "repository", "proto package name")
cmd.Flags().StringVar(&root.ModelImportPath, "modelImportPath", "", "model导入路径")
cmd.Flags().BoolVar(&root.DisableDocComment, "enableInt", false, "禁用文档注释")
cmd.Flags().BoolVar(&root.EnableInt, "disableBool", false, "使能int8,uint8,int16,uint16,int32,uint32输出为int,uint")
cmd.Flags().BoolVar(&root.EnableIntegerInt, "enableIntegerInt", false, "使能int32,uint32输出为int,uint")
cmd.Flags().BoolVar(&root.DisableDocComment, "disableDocComment", false, "禁用文档注释")
cmd.Flags().BoolVar(&root.EnableInt, "enableInt", false, "使能int8,uint8,int16,uint16,int32,uint32输出为int,uint")
cmd.Flags().BoolVar(&root.EnableBoolInt, "enableBoolInt", false, "使能bool输出int")

cmd.MarkFlagsOneRequired("url", "input")
Expand Down
24 changes: 19 additions & 5 deletions rapier/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type CodeGen struct {
ModelImportPath string // required, model导入路径
DisableDocComment bool // 禁用doc注释
EnableInt bool // 使能int8,uint8,int16,uint16,int32,uint32输出为int,uint
EnableIntegerInt bool // 使能int32,uint32输出为int,uint
EnableBoolInt bool // 使能bool输出int
}

Expand Down Expand Up @@ -113,7 +112,7 @@ func (g *CodeGen) Gen() *CodeGen {
if field.Comment != "" {
comment = "// " + field.Comment
}
g.Printf("%s rapier.%s %s\n", field.GoName, field.Type.String(), comment)
g.Printf("%s rapier.%s %s\n", field.GoName, g.intoTypeName(field.Type), comment)
}
g.Println("}")
g.Println()
Expand All @@ -126,7 +125,7 @@ func (g *CodeGen) Gen() *CodeGen {
g.Println("refTableName: tableName,")
g.Println("ALL: rapier.NewAsterisk(alias),")
for _, field := range et.Fields {
g.Printf("%s: rapier.New%s(alias, \"%s\"),\n", field.GoName, field.Type.String(), field.ColumnName)
g.Printf("%s: rapier.New%s(alias, \"%s\"),\n", field.GoName, g.intoTypeName(field.Type), field.ColumnName)
}
g.Println("}")
g.Println("}")
Expand Down Expand Up @@ -243,19 +242,34 @@ func (g *CodeGen) escapeStructFields(et *Struct) {
break
}
goName = "X" + goName
// 和存在的重复
// 和当前字段存在的重复, 再追加一个
_, ok = existFieldName[goName]
if ok {
goName = "X" + goName
}
}
if field.GoName != goName {
field.GoName = goName
escapeNames[goName] = struct{}{}
escapeNames[goName] = struct{}{} // 添加为必须转义
}
}
}

func (g *CodeGen) intoTypeName(t Type) string {
if g.EnableInt {
switch t {
case Int8, Int16, Int32:
return Int.String()
case Uint8, Uint16, Uint32:
return Uint.String()
}
}
if g.EnableBoolInt && t == Bool {
return Int.String()
}
return t.String()
}

func genRapier_SelectVariantExprField(field *StructField, hasPrefix bool) string {
goName := field.GoName

Expand Down

0 comments on commit cbf2b91

Please sign in to comment.