Skip to content

Commit

Permalink
fix: drop dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed May 9, 2024
1 parent fabe1d2 commit e077ad1
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 324 deletions.
14 changes: 1 addition & 13 deletions cmd/ormat/command/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@ func newBuildCmd() *buildCmd {
},
}

cmdMapper := &cobra.Command{
Use: "mapper",
Short: "model mapper from database",
Example: "ormat gen mapper",
RunE: func(*cobra.Command, []string) error {
sc := getSchema()
return root.genFileOpt.GenMapper(sc)
},
}

cmd.PersistentFlags().StringSliceVarP(&root.InputFile, "input", "i", nil, "input file")
cmd.PersistentFlags().StringVarP(&root.Schema, "schema", "s", "file+mysql", "parser driver, [file+mysql,file+tidb]")
cmd.PersistentFlags().StringVarP(&root.OutputDir, "out", "o", "./model", "out directory")
Expand All @@ -89,9 +79,7 @@ func newBuildCmd() *buildCmd {
cmd.PersistentFlags().StringVar(&root.Template, "template", "", "use custom template")

cmd.MarkPersistentFlagRequired("input") // nolint
cmd.AddCommand(
cmdMapper,
)

root.cmd = cmd
return root
}
16 changes: 0 additions & 16 deletions cmd/ormat/command/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,6 @@ func newGenCmd() *genCmd {
},
}

cmdMapper := &cobra.Command{
Use: "mapper",
Short: "model mapper from database",
Example: "ormat gen mapper",
RunE: func(*cobra.Command, []string) error {
sc, err := getSchema()
if err != nil {
return err
}
return root.genFileOpt.GenMapper(sc)
},
}

cmd.PersistentFlags().StringVar(&root.URL, "url", "", "mysql://root:[email protected]:3306/test")
cmd.PersistentFlags().StringSliceVarP(&root.Tables, "table", "t", nil, "only out custom table")
cmd.PersistentFlags().StringSliceVar(&root.Exclude, "exclude", nil, "exclude table pattern")
Expand All @@ -79,9 +66,6 @@ func newGenCmd() *genCmd {

cmd.MarkPersistentFlagRequired("url") // nolint

cmd.AddCommand(
cmdMapper,
)
root.cmd = cmd
return root
}
13 changes: 3 additions & 10 deletions cmd/ormat/command/helper_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,18 @@ import (

type Config struct {
ens.Option
DisableCommentTag bool `yaml:"disableCommentTag" json:"disableCommentTag"` // 禁用注释放入tag标签中
HasColumn bool `yaml:"hasColumn" json:"hasColumn"` // 是否输出字段
SkipColumns []string `yaml:"skipColumns" json:"skipColumns"` // 忽略输出字段, 格式 table.column
Package string `yaml:"package" json:"package"` // 包名
Options map[string]string `yaml:"options" json:"options"` // 选项
DisableDocComment bool `yaml:"disable_doc_comment" json:"disable_doc_comment"` // 禁用文档注释
DisableCommentTag bool `yaml:"disableCommentTag" json:"disableCommentTag"` // 禁用注释放入tag标签中
Package string `yaml:"package" json:"package"` // 包名
DisableDocComment bool `yaml:"disable_doc_comment" json:"disable_doc_comment"` // 禁用文档注释
}

func InitFlagSetForConfig(s *pflag.FlagSet, cc *Config) {
s.StringToStringVarP(&cc.Tags, "tags", "K", map[string]string{"json": utils.StyleSnakeCase}, "tags标签,类型支持[smallCamelCase,camelCase,snakeCase,kebab]")
s.BoolVarP(&cc.EnableInt, "enableInt", "e", false, "使能int8,uint8,int16,uint16,int32,uint32输出为int,uint")
s.BoolVarP(&cc.EnableIntegerInt, "enableIntegerInt", "E", false, "使能int32,uint32输出为int,uint")
s.BoolVarP(&cc.EnableBoolInt, "enableBoolInt", "b", false, "使能bool输出int")
s.BoolVarP(&cc.DisableNullToPoint, "disableNullToPoint", "B", false, "禁用字段为null时输出指针类型,将输出为sql.Nullxx")
s.BoolVarP(&cc.DisableCommentTag, "disableCommentTag", "j", false, "禁用注释放入tag标签中")
s.BoolVarP(&cc.EnableForeignKey, "enableForeignKey", "J", false, "使用外键")
s.BoolVar(&cc.HasColumn, "hasColumn", false, "是否输出字段")
s.StringSliceVar(&cc.SkipColumns, "skipColumns", nil, "忽略输出字段(仅 hasColumn = true 有效), 格式 table.column(只作用于指定表字段) 或 column(作用于所有表)")
s.StringVar(&cc.Package, "package", "", "package name")
s.StringToStringVar(&cc.Options, "options", nil, "options key value")
s.BoolVarP(&cc.DisableDocComment, "disableDocComment", "d", false, "禁用文档注释")
}
40 changes: 0 additions & 40 deletions cmd/ormat/command/helper_gen_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,17 @@ type genFileOpt struct {
Merge bool
MergeFilename string
Template string
// assist命令 model 导入路径
ModelImportPath string
}

func (self *genFileOpt) build(mixin ens.Schemaer) *ens.Schema {
return mixin.Build(&self.View.Option)
}

func (self *genFileOpt) GenModel(mixin ens.Schemaer) error {
skipColumns := make(map[string]struct{})
for _, v := range self.View.SkipColumns {
skipColumns[v] = struct{}{}
}

codegenOption := []codegen.Option{
codegen.WithByName("ormat"),
codegen.WithVersion(version),
codegen.WithPackageName(utils.GetPkgName(self.OutputDir)),
codegen.WithOptions(self.View.Options),
codegen.WithSkipColumns(skipColumns),
codegen.WithHasColumn(self.View.HasColumn),
codegen.WithDisableDocComment(self.View.DisableDocComment),
}
sc := self.build(mixin)
Expand Down Expand Up @@ -67,33 +57,3 @@ func (self *genFileOpt) GenModel(mixin ens.Schemaer) error {
slog.Info("😄 generate success !!!")
return nil
}

func (self *genFileOpt) GenMapper(mixin ens.Schemaer) error {
skipColumns := make(map[string]struct{})
for _, v := range self.View.SkipColumns {
skipColumns[v] = struct{}{}
}
codegenOption := []codegen.Option{
codegen.WithByName("ormat"),
codegen.WithVersion(version),
codegen.WithPackageName(utils.GetPkgName(self.OutputDir)),
codegen.WithOptions(self.View.Options),
codegen.WithSkipColumns(skipColumns),
codegen.WithHasColumn(self.View.HasColumn),
codegen.WithDisableDocComment(self.View.DisableDocComment),
}
sc := self.build(mixin)
for _, entity := range sc.Entities {
data := codegen.New([]*ens.EntityDescriptor{entity}, codegenOption...).
GenMapper().
Bytes()
filename := joinFilename(self.OutputDir, entity.Name, ".proto")
err := WriteFile(filename, data)
if err != nil {
return fmt.Errorf("%v: %v", entity.Name, err)
}
slog.Info("👉 " + filename)
}
slog.Info("😄 generate success !!!")
return nil
}
28 changes: 0 additions & 28 deletions codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ type CodeGen struct {
byName string
version string
packageName string
options map[string]string
skipColumns map[string]struct{}
hasColumn bool
disableDocComment bool
}

Expand All @@ -41,28 +38,6 @@ func WithPackageName(s string) Option {
}
}

func WithOptions(mp map[string]string) Option {
return func(g *CodeGen) {
for k, v := range mp {
g.options[k] = v
}
}
}

func WithSkipColumns(mp map[string]struct{}) Option {
return func(g *CodeGen) {
if mp != nil {
g.skipColumns = mp
}
}
}

func WithHasColumn(b bool) Option {
return func(g *CodeGen) {
g.hasColumn = b
}
}

func WithDisableDocComment(b bool) Option {
return func(g *CodeGen) {
g.disableDocComment = b
Expand All @@ -75,9 +50,6 @@ func New(md []*ens.EntityDescriptor, opts ...Option) *CodeGen {
byName: "codegen",
version: "unknown",
packageName: "codegen",
options: make(map[string]string),
skipColumns: make(map[string]struct{}),
hasColumn: false,
}
for _, opt := range opts {
opt(g)
Expand Down
82 changes: 0 additions & 82 deletions codegen/mapper.go

This file was deleted.

10 changes: 0 additions & 10 deletions codegen/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,6 @@ func (g *CodeGen) GenModel() *CodeGen {
g.Printf("return \"%s\"\n", tableName)
g.Println("}")
g.Println()

if g.hasColumn {
g.Printf("// Select%s database column name.\n", structName)
g.Printf("var Select%s = []string {\n", structName)
for _, field := range et.Fields {
_, skip := g.skipColumns[field.Name]
g.Println(genModelStructVarField(tableName, field, skip))
}
g.Println("}")
}
}
return g
}
Expand Down
13 changes: 6 additions & 7 deletions entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package ens

// EntityDescriptor Each table corresponds to an EntityDescriptor
type EntityDescriptor struct {
Name string // entity name
Comment string // entity comment
Table TableDef // entity table define
Fields []*FieldDescriptor // field information
Indexes []*IndexDescriptor // index information
ForeignKeys []*ForeignKeyDescriptor // foreign key information
ProtoMessage []*ProtoMessage // protobuf message information.
Name string // entity name
Comment string // entity comment
Table TableDef // entity table define
Fields []*FieldDescriptor // field information
Indexes []*IndexDescriptor // index information
ForeignKeys []*ForeignKeyDescriptor // foreign key information
}

type EntityDescriptorSlice []*EntityDescriptor
Expand Down
15 changes: 6 additions & 9 deletions entity_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ func (self *EntityBuilder) ForeignKeys() []ForeignKeyer { return self.forei
func (self *EntityBuilder) Build(opt *Option) *EntityDescriptor {
fielders := self.Fields()
fields := make([]*FieldDescriptor, 0, len(fielders))
protoMessages := make([]*ProtoMessage, 0, len(fielders))
for _, fb := range fielders {
field := fb.Build(opt)
fields = append(fields, field)
protoMessages = append(protoMessages, field.buildProtoMessage())
}
indexers := self.Indexes()
indexes := make([]*IndexDescriptor, 0, len(indexers))
Expand All @@ -73,12 +71,11 @@ func (self *EntityBuilder) Build(opt *Option) *EntityDescriptor {
}
name, comment := self.Metadata()
return &EntityDescriptor{
Name: name,
Comment: comment,
Table: self.Table(),
Fields: fields,
Indexes: indexes,
ForeignKeys: fks,
ProtoMessage: protoMessages,
Name: name,
Comment: comment,
Table: self.Table(),
Fields: fields,
Indexes: indexes,
ForeignKeys: fks,
}
}
21 changes: 3 additions & 18 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ type FieldDescriptor struct {
Nullable bool // Nullable reports whether the column may be null.
Column ColumnDef
// for go
Type *GoType // go type information.
Optional bool // nullable struct field.
Tags []string // Tags struct tag
RapierDataType string // rapier data type
Type *GoType // go type information.
Optional bool // nullable struct field.
Tags []string // Tags struct tag
}

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

func (field *FieldDescriptor) build(opt *Option) {
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 @@ -38,25 +36,12 @@ func (field *FieldDescriptor) build(opt *Option) {
switch field.Type.Type {
case TypeInt8, TypeInt16, TypeInt32:
field.goType(int(0))
field.RapierDataType = TypeInt.IntoRapierDataType()
case TypeUint8, TypeUint16, TypeUint32:
field.goType(uint(0))
field.RapierDataType = TypeUint.IntoRapierDataType()
}
}
if opt.EnableIntegerInt {
switch field.Type.Type {
case TypeInt32:
field.goType(int(0))
field.RapierDataType = TypeInt.IntoRapierDataType()
case TypeUint32:
field.goType(uint(0))
field.RapierDataType = TypeUint.IntoRapierDataType()
}
}
if opt.EnableBoolInt && field.Type.IsBool() {
field.goType(int(0))
field.RapierDataType = TypeInt.IntoRapierDataType()
}
if field.Nullable && opt.DisableNullToPoint {
gt, ok := sqlNullValueGoType[field.Type.Type]
Expand Down
Loading

0 comments on commit e077ad1

Please sign in to comment.