From e077ad19678f1f902e9d819cbbdff9c89099cb37 Mon Sep 17 00:00:00 2001 From: thinkgos Date: Thu, 9 May 2024 22:30:12 +0800 Subject: [PATCH] fix: drop dead code --- cmd/ormat/command/build.go | 14 +---- cmd/ormat/command/gen.go | 16 ------ cmd/ormat/command/helper_config.go | 13 +---- cmd/ormat/command/helper_gen_file.go | 40 -------------- codegen/codegen.go | 28 ---------- codegen/mapper.go | 82 ---------------------------- codegen/model.go | 10 ---- entity.go | 13 ++--- entity_builder.go | 15 ++--- field.go | 21 +------ field_protobuf.go | 40 -------------- go_type.go | 50 ----------------- option.go | 1 - 13 files changed, 19 insertions(+), 324 deletions(-) delete mode 100644 codegen/mapper.go delete mode 100644 field_protobuf.go diff --git a/cmd/ormat/command/build.go b/cmd/ormat/command/build.go index b981b80..bcb5ef3 100644 --- a/cmd/ormat/command/build.go +++ b/cmd/ormat/command/build.go @@ -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") @@ -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 } diff --git a/cmd/ormat/command/gen.go b/cmd/ormat/command/gen.go index ec7eac8..09ecd62 100644 --- a/cmd/ormat/command/gen.go +++ b/cmd/ormat/command/gen.go @@ -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:123456@127.0.0.1:3306/test") cmd.PersistentFlags().StringSliceVarP(&root.Tables, "table", "t", nil, "only out custom table") cmd.PersistentFlags().StringSliceVar(&root.Exclude, "exclude", nil, "exclude table pattern") @@ -79,9 +66,6 @@ func newGenCmd() *genCmd { cmd.MarkPersistentFlagRequired("url") // nolint - cmd.AddCommand( - cmdMapper, - ) root.cmd = cmd return root } diff --git a/cmd/ormat/command/helper_config.go b/cmd/ormat/command/helper_config.go index 0fc9978..76990e5 100644 --- a/cmd/ormat/command/helper_config.go +++ b/cmd/ormat/command/helper_config.go @@ -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, "禁用文档注释") } diff --git a/cmd/ormat/command/helper_gen_file.go b/cmd/ormat/command/helper_gen_file.go index 4ef2889..0e17575 100644 --- a/cmd/ormat/command/helper_gen_file.go +++ b/cmd/ormat/command/helper_gen_file.go @@ -15,8 +15,6 @@ type genFileOpt struct { Merge bool MergeFilename string Template string - // assist命令 model 导入路径 - ModelImportPath string } func (self *genFileOpt) build(mixin ens.Schemaer) *ens.Schema { @@ -24,18 +22,10 @@ func (self *genFileOpt) build(mixin ens.Schemaer) *ens.Schema { } 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) @@ -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 -} diff --git a/codegen/codegen.go b/codegen/codegen.go index 7c0500e..f72cb21 100644 --- a/codegen/codegen.go +++ b/codegen/codegen.go @@ -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 } @@ -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 @@ -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) diff --git a/codegen/mapper.go b/codegen/mapper.go deleted file mode 100644 index 6e7305a..0000000 --- a/codegen/mapper.go +++ /dev/null @@ -1,82 +0,0 @@ -package codegen - -import ( - "fmt" - "strings" - - "github.com/things-go/ens" - "github.com/things-go/ens/utils" -) - -func (g *CodeGen) GenMapper() *CodeGen { - 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.Println(`syntax = "proto3";`) - g.Println() - g.Printf("package %s;\n", g.packageName) - g.Println() - if len(g.options) > 0 { - for k, v := range g.options { - g.Printf("option %s = \"%s\";\n", k, v) - } - g.Println() - } - - if needOpenapiv2Annotation(g.entities) { - g.Println(`import "protoc-gen-openapiv2/options/annotations.proto";`) - g.Println() - } - - for _, et := range g.entities { - structName := utils.CamelCase(et.Name) - - g.Printf("// %s %s\n", structName, trimStructComment(et.Comment, "\n", "\n// ")) - - g.Printf("// #[seaql]\n") - if et.Table != nil && et.Table.PrimaryKey() != nil { - g.Printf("// #[seaql(index=\"%s\")]\n", et.Table.PrimaryKey().Definition()) - } - for _, index := range et.Indexes { - if et.Table != nil && - et.Table.PrimaryKey() != nil && - et.Table.PrimaryKey().Index().Name == index.Name { - continue - } - g.Printf("// #[seaql(index=\"%s\")]\n", index.Index.Definition()) - } - for _, fk := range et.ForeignKeys { - g.Printf("// #[seaql(foreign_key=\"%s\")]\n", fk.ForeignKey.Definition()) - } - g.Printf("message %s {\n", structName) - for i, m := range et.ProtoMessage { - if m.Comment != "" { - g.Printf("%s\n", m.Comment) - } - g.Println(genMapperMessageField(i+1, m)) - } - g.Println("}") - } - return g -} - -func genMapperMessageField(seq int, m *ens.ProtoMessage) string { - annotation := "" - if len(m.Annotations) > 0 { - annotation = fmt.Sprintf(" [%s]", strings.Join(m.Annotations, ", ")) - } - return fmt.Sprintf("%s %s = %d%s;", m.DataType, m.Name, seq, annotation) -} - -func needOpenapiv2Annotation(entities []*ens.EntityDescriptor) bool { - for _, entity := range entities { - for _, v := range entity.ProtoMessage { - if v.DataType == "int64" { - return true - } - } - } - return false -} diff --git a/codegen/model.go b/codegen/model.go index 4ddedd9..0840d24 100644 --- a/codegen/model.go +++ b/codegen/model.go @@ -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 } diff --git a/entity.go b/entity.go index d5c7e85..cf3086c 100644 --- a/entity.go +++ b/entity.go @@ -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 diff --git a/entity_builder.go b/entity_builder.go index 584a470..acd607c 100644 --- a/entity_builder.go +++ b/entity_builder.go @@ -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)) @@ -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, } } diff --git a/field.go b/field.go index bbb12bd..0c8530b 100644 --- a/field.go +++ b/field.go @@ -14,10 +14,9 @@ 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) { @@ -25,7 +24,6 @@ func (field *FieldDescriptor) goType(typ any) { } 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)) @@ -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] diff --git a/field_protobuf.go b/field_protobuf.go deleted file mode 100644 index 7d61796..0000000 --- a/field_protobuf.go +++ /dev/null @@ -1,40 +0,0 @@ -package ens - -import ( - "strings" - - "github.com/things-go/ens/matcher" -) - -type ProtoMessage struct { - DataType string // 数据类型 - Name string // 名称 - Optional bool // 是否可选 - Comment string // 注释 - Annotations []string // 注解 -} - -func (field *FieldDescriptor) buildProtoMessage() *ProtoMessage { - dataType := field.Type.Type.IntoProtoDataType() - annotations := make([]string, 0, 16) - if field.Type.Type == TypeInt64 || - field.Type.Type == TypeUint64 { - annotations = append(annotations, `(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = { type: [ INTEGER ] }`) - } else if field.Type.IsTime() { - dataType = "int64" - annotations = append(annotations, `(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = { type: [ INTEGER ] }`) - } - - comment := strings.TrimSuffix(matcher.TrimEnumValue(field.Comment), ",") - if comment != "" { - comment = "// " + comment - } - - return &ProtoMessage{ - DataType: dataType, - Name: field.Name, - Optional: field.Optional, - Comment: comment, - Annotations: annotations, - } -} diff --git a/go_type.go b/go_type.go index ce7e7cf..959a5af 100644 --- a/go_type.go +++ b/go_type.go @@ -3,7 +3,6 @@ package ens import ( "reflect" "slices" - "strings" "github.com/things-go/ens/rapier" "github.com/things-go/ens/utils" @@ -191,55 +190,6 @@ func (t Type) IntoRapierType() rapier.Type { } } -func (t Type) IntoProtoDataType() string { - dataType := "" - switch t { - case TypeBool: - dataType = "bool" - case TypeInt8, TypeInt16, TypeInt32, TypeInt: - dataType = "int32" - case TypeInt64: - dataType = "int64" - case TypeUint8, TypeUint16, TypeUint32, TypeUint: - dataType = "uint32" - case TypeUint64: - dataType = "uint64" - case TypeFloat32: - dataType = "float" - case TypeFloat64: - dataType = "double" - case TypeDecimal, TypeString, TypeEnum, TypeJSON, TypeUUID, TypeOther: - dataType = "string" - case TypeBytes: - dataType = "bytes" - case TypeTime: - dataType = "google.protobuf.Timestamp" - default: - dataType = "string" - } - return dataType -} - -func (t Type) IntoRapierDataType() string { - switch t { - case TypeBool, - TypeInt8, TypeInt16, TypeInt32, TypeInt64, TypeInt, - TypeUint8, TypeUint16, TypeUint32, TypeUint64, TypeUint, - TypeFloat32, TypeFloat64, TypeString, TypeEnum: - return strings.Title(t.String()) - case TypeDecimal: - return "Decimal" - case TypeBytes: - return "Bytes" - case TypeTime: - return "Time" - case TypeJSON, TypeUUID, TypeOther: - fallthrough - default: - return "Field" - } -} - type GoType struct { Type Type // Type enum. Ident string // Type identifier, e.g. int, time.Time, sql.NullInt64. diff --git a/option.go b/option.go index d4618a2..b478cd1 100644 --- a/option.go +++ b/option.go @@ -2,7 +2,6 @@ package ens type Option struct { EnableInt bool `yaml:"enableInt" json:"enableInt"` // 使能int8,uint8,int16,uint16,int32,uint32输出为int,uint - EnableIntegerInt bool `yaml:"enableIntegerInt" json:"enableIntegerInt"` // 使能int32,uint32输出为int,uint EnableBoolInt bool `yaml:"enableBoolInt" json:"enableBoolInt"` // 使能bool输出int DisableNullToPoint bool `yaml:"disableNullToPoint" json:"disableNullToPoint"` // 禁用字段为null时输出指针类型,将输出为sql.Nullxx EnableForeignKey bool `yaml:"enableForeignKey" json:"enableForeignKey"` // 输出外键