From 8fbe9e00c366a9e53aea4f574c923a3cb420c27d Mon Sep 17 00:00:00 2001 From: thinkgos Date: Fri, 10 May 2024 22:18:47 +0800 Subject: [PATCH] fix: fix ens --- cmd/ormat/command/model.go | 16 +++--- codegen.go | 2 + def.go | 26 --------- driver/driver.go | 2 +- driver/mysql/def_atlas.go | 50 +++++++++++++++++ driver/mysql/def_utils.go | 29 ---------- driver/mysql/mysql.go | 8 +-- driver/mysql/sql.go | 6 +- driver/mysql/sql_test.go | 3 +- driver/mysql/sql_tidb.go | 8 +-- entity_builder.go | 81 --------------------------- field.go | 10 ++-- field_builder.go | 111 ------------------------------------- foreign_key_builder.go | 68 ----------------------- index_builder.go | 41 -------------- model_parse.go | 29 +++++++--- schema.go | 17 ------ 17 files changed, 98 insertions(+), 409 deletions(-) delete mode 100644 entity_builder.go delete mode 100644 field_builder.go delete mode 100644 foreign_key_builder.go delete mode 100644 index_builder.go diff --git a/cmd/ormat/command/model.go b/cmd/ormat/command/model.go index 25134d7..b644e05 100644 --- a/cmd/ormat/command/model.go +++ b/cmd/ormat/command/model.go @@ -44,7 +44,7 @@ type modelCmd struct { func newModelCmd() *modelCmd { root := &modelCmd{} - getSchema := func() (ens.Schemaer, error) { + getSchema := func() (*ens.Schema, error) { if root.URL != "" { d, err := LoadDriver(root.URL) if err != nil { @@ -64,12 +64,12 @@ func newModelCmd() *modelCmd { if err != nil { return nil, err } - mixin := &ens.MixinSchema{ + mixin := &ens.Schema{ Name: "", - Entities: make([]ens.MixinEntity, 0, 128), + Entities: make([]*ens.EntityDescriptor, 0, 128), } for _, filename := range root.InputFile { - sc, err := func() (ens.Schemaer, error) { + sc, err := func() (*ens.Schema, error) { content, err := os.ReadFile(filename) if err != nil { return nil, err @@ -84,7 +84,7 @@ func newModelCmd() *modelCmd { slog.Warn("🧐 parse failed !!!", slog.String("file", filename), slog.Any("error", err)) continue } - mixin.Entities = append(mixin.Entities, sc.(*ens.MixinSchema).Entities...) + mixin.Entities = append(mixin.Entities, sc.Entities...) } return mixin, nil } @@ -101,14 +101,14 @@ func newModelCmd() *modelCmd { if err != nil { return err } - sc := schemaes.Build(&root.Option) if root.Merge { g := ens.CodeGen{ - Entities: sc.Entities, + Entities: schemaes.Entities, ByName: "ormat", Version: version, PackageName: cmp.Or(root.PackageName, utils.GetPkgName(root.OutputDir)), DisableDocComment: root.DisableDocComment, + Option: root.Option, } data, err := g.Gen().FormatSource() if err != nil { @@ -121,7 +121,7 @@ func newModelCmd() *modelCmd { } slog.Info("👉 " + filename) } else { - for _, entity := range sc.Entities { + for _, entity := range schemaes.Entities { g := &ens.CodeGen{ Entities: []*ens.EntityDescriptor{entity}, ByName: "ormat", diff --git a/codegen.go b/codegen.go index 4b3e6bc..de74294 100644 --- a/codegen.go +++ b/codegen.go @@ -16,6 +16,7 @@ type CodeGen struct { Version string PackageName string DisableDocComment bool + Option } // Bytes returns the CodeBuf's buffer. @@ -106,6 +107,7 @@ func (g *CodeGen) Gen() *CodeGen { } func (g *CodeGen) genModelStructField(field *FieldDescriptor) string { + field.build(&g.Option) b := strings.Builder{} b.Grow(256) ident := field.Type.Ident diff --git a/def.go b/def.go index 5b7fc4d..8da9ef1 100644 --- a/def.go +++ b/def.go @@ -23,29 +23,3 @@ type ForeignKeyDef interface { ForeignKey() *schema.ForeignKey Definition() string } - -type Indexer interface { - Build() *IndexDescriptor -} - -type Fielder interface { - Build(*Option) *FieldDescriptor -} - -type ForeignKeyer interface { - Build() *ForeignKeyDescriptor -} - -type MixinEntity interface { - Metadata() (string, string) - Table() TableDef - Fields() []Fielder - Indexes() []Indexer - ForeignKeys() []ForeignKeyer - - Build(*Option) *EntityDescriptor -} - -type Schemaer interface { - Build(opt *Option) *Schema -} diff --git a/driver/driver.go b/driver/driver.go index 055e408..973d99f 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -22,7 +22,7 @@ const ( var drivers sync.Map type Driver interface { - InspectSchema(context.Context, *InspectOption) (*ens.MixinSchema, error) + InspectSchema(context.Context, *InspectOption) (*ens.Schema, error) InspectProto(context.Context, *InspectOption) (*proto.Schema, error) InspectRapier(ctx context.Context, arg *InspectOption) (*rapier.Schema, error) InspectSql(ctx context.Context, arg *InspectOption) (*sqlx.Schema, error) diff --git a/driver/mysql/def_atlas.go b/driver/mysql/def_atlas.go index 6d4daa6..513ba3b 100644 --- a/driver/mysql/def_atlas.go +++ b/driver/mysql/def_atlas.go @@ -8,6 +8,7 @@ import ( "ariga.io/atlas/sql/schema" "google.golang.org/protobuf/reflect/protoreflect" + "github.com/things-go/ens" "github.com/things-go/ens/internal/insql" "github.com/things-go/ens/proto" "github.com/things-go/ens/rapier" @@ -261,3 +262,52 @@ func intoSql(tb *schema.Table) *sqlx.Table { Comment: insql.MustComment(tb.Attrs), } } + +func intoSchema(tb *schema.Table) *ens.EntityDescriptor { + // * columns + fielders := make([]*ens.FieldDescriptor, 0, len(tb.Columns)) + for _, col := range tb.Columns { + fielders = append(fielders, &ens.FieldDescriptor{ + Name: col.Name, + Comment: insql.MustComment(col.Attrs), + Nullable: col.Type.Null, + Column: NewColumnDef(col), + Type: intoGoType(col.Type.Raw), + Optional: col.Type.Null, + Tags: []string{intoGormTag(tb, col)}, + }) + } + // * indexes + indexers := make([]*ens.IndexDescriptor, 0, len(tb.Indexes)) + for _, index := range tb.Indexes { + indexers = append(indexers, &ens.IndexDescriptor{ + Name: index.Name, + Fields: insql.IndexPartColumnNames(index.Parts), + Index: NewIndexDef(index), + }) + } + //* foreignKeys + fks := make([]*ens.ForeignKeyDescriptor, 0, len(tb.ForeignKeys)) + for _, fk := range tb.ForeignKeys { + fks = append(fks, &ens.ForeignKeyDescriptor{ + Symbol: fk.Symbol, + Table: fk.Table.Name, + Columns: insql.ColumnNames(fk.Columns), + RefTable: fk.RefTable.Name, + RefColumns: insql.ColumnNames(fk.RefColumns), + OnUpdate: fk.OnUpdate, + OnDelete: fk.OnDelete, + ForeignKey: NewForeignKey(fk), + }) + } + + // * table + return &ens.EntityDescriptor{ + Name: tb.Name, + Comment: insql.MustComment(tb.Attrs), + Table: NewTableDef(tb), + Fields: fielders, + Indexes: indexers, + ForeignKeys: fks, + } +} diff --git a/driver/mysql/def_utils.go b/driver/mysql/def_utils.go index 00cd40b..45ef625 100644 --- a/driver/mysql/def_utils.go +++ b/driver/mysql/def_utils.go @@ -4,7 +4,6 @@ import ( "ariga.io/atlas/sql/mysql" "ariga.io/atlas/sql/schema" - "github.com/things-go/ens" "github.com/things-go/ens/internal/insql" ) @@ -20,31 +19,3 @@ func findIndexType(attrs []schema.Attr) string { return "BTREE" } } - -func IntoMixinEntity(tb *schema.Table) ens.MixinEntity { - // * columns - fielders := make([]ens.Fielder, 0, len(tb.Columns)) - for _, col := range tb.Columns { - colDef := NewColumnDef(col) - fielders = append(fielders, - ens.FieldFromDef(intoGoType(col.Type.Raw), colDef). - Tags(colDef.GormTag(tb)), - ) - } - // * indexes - indexers := make([]ens.Indexer, 0, len(tb.Indexes)) - for _, index := range tb.Indexes { - indexers = append(indexers, ens.IndexFromDef(NewIndexDef(index))) - } - //* foreignKeys - fkers := make([]ens.ForeignKeyer, 0, len(tb.ForeignKeys)) - for _, fk := range tb.ForeignKeys { - fkers = append(fkers, ens.ForeignKeyFromDef(NewForeignKey(fk))) - } - - // * table - return ens.EntityFromDef(NewTableDef(tb)). - SetFields(fielders...). - SetIndexes(indexers...). - SetForeignKeys(fkers...) -} diff --git a/driver/mysql/mysql.go b/driver/mysql/mysql.go index 062688b..1957d62 100644 --- a/driver/mysql/mysql.go +++ b/driver/mysql/mysql.go @@ -20,16 +20,16 @@ var _ driver.Driver = (*MySQL)(nil) type MySQL struct{} -func (self *MySQL) InspectSchema(ctx context.Context, arg *driver.InspectOption) (*ens.MixinSchema, error) { +func (self *MySQL) InspectSchema(ctx context.Context, arg *driver.InspectOption) (*ens.Schema, error) { schemaes, err := self.inspectSchema(ctx, arg) if err != nil { return nil, err } - entities := make([]ens.MixinEntity, 0, len(schemaes.Tables)) + entities := make([]*ens.EntityDescriptor, 0, len(schemaes.Tables)) for _, tb := range schemaes.Tables { - entities = append(entities, IntoMixinEntity(tb)) + entities = append(entities, intoSchema(tb)) } - return &ens.MixinSchema{ + return &ens.Schema{ Name: schemaes.Name, Entities: entities, }, nil diff --git a/driver/mysql/sql.go b/driver/mysql/sql.go index 0b55756..6b1606f 100644 --- a/driver/mysql/sql.go +++ b/driver/mysql/sql.go @@ -23,14 +23,14 @@ var _ driver.Driver = (*SQL)(nil) type SQL struct{} // InspectSchema implements driver.Driver. -func (self *SQL) InspectSchema(ctx context.Context, arg *driver.InspectOption) (*ens.MixinSchema, error) { +func (self *SQL) InspectSchema(ctx context.Context, arg *driver.InspectOption) (*ens.Schema, error) { table, err := self.inspectSchema(ctx, arg) if err != nil { return nil, err } - return &ens.MixinSchema{ + return &ens.Schema{ Name: "", - Entities: []ens.MixinEntity{IntoMixinEntity(table)}, + Entities: []*ens.EntityDescriptor{intoSchema(table)}, }, nil } diff --git a/driver/mysql/sql_test.go b/driver/mysql/sql_test.go index da65ca6..1d03fba 100644 --- a/driver/mysql/sql_test.go +++ b/driver/mysql/sql_test.go @@ -5,7 +5,6 @@ import ( "fmt" "testing" - "github.com/things-go/ens" "github.com/things-go/ens/driver" ) @@ -57,5 +56,5 @@ func Test_SQL_Parse(t *testing.T) { if err != nil { t.Fatal(err) } - fmt.Println(value.Entities[0].(*ens.EntityBuilder)) + fmt.Println(value.Entities[0]) } diff --git a/driver/mysql/sql_tidb.go b/driver/mysql/sql_tidb.go index aa92411..0d059d3 100644 --- a/driver/mysql/sql_tidb.go +++ b/driver/mysql/sql_tidb.go @@ -25,14 +25,14 @@ import ( type SQLTidb struct{} // InspectSchema implements driver.Driver. -func (self *SQLTidb) InspectSchema(_ context.Context, arg *driver.InspectOption) (*ens.MixinSchema, error) { +func (self *SQLTidb) InspectSchema(_ context.Context, arg *driver.InspectOption) (*ens.Schema, error) { pr := parser.New() stmts, _, err := pr.ParseSQL(arg.Data) if err != nil { return nil, err } - entities := make([]ens.MixinEntity, 0, len(stmts)) + entities := make([]*ens.EntityDescriptor, 0, len(stmts)) for _, stmt := range stmts { createStmt, ok := stmt.(*ast.CreateTableStmt) if !ok { @@ -42,9 +42,9 @@ func (self *SQLTidb) InspectSchema(_ context.Context, arg *driver.InspectOption) if err != nil { return nil, err } - entities = append(entities, IntoMixinEntity(table)) + entities = append(entities, intoSchema(table)) } - return &ens.MixinSchema{ + return &ens.Schema{ Name: "", Entities: entities, }, nil diff --git a/entity_builder.go b/entity_builder.go deleted file mode 100644 index ee878dd..0000000 --- a/entity_builder.go +++ /dev/null @@ -1,81 +0,0 @@ -package ens - -import "github.com/things-go/ens/internal/insql" - -var _ MixinEntity = (*EntityBuilder)(nil) - -type EntityBuilder struct { - name string // schema entity name - comment string // schema entity comment - table TableDef // entity table define - fields []Fielder // field information - indexes []Indexer // index information - foreignKeys []ForeignKeyer // foreign key information -} - -// EntityFromDef returns a new entity with the TableDef. -// auto set name, comment, table. -func EntityFromDef(def TableDef) *EntityBuilder { - tb := def.Table() - return &EntityBuilder{ - name: tb.Name, - comment: insql.MustComment(tb.Attrs), - table: def, - fields: nil, - indexes: nil, - } -} - -func (self *EntityBuilder) SetMetadata(name, comment string) *EntityBuilder { - self.name = name - self.comment = comment - return self -} -func (self *EntityBuilder) SetTable(tb TableDef) *EntityBuilder { - self.table = tb - return self -} -func (self *EntityBuilder) SetFields(fields ...Fielder) *EntityBuilder { - self.fields = fields - return self -} -func (self *EntityBuilder) SetIndexes(indexes ...Indexer) *EntityBuilder { - self.indexes = indexes - return self -} -func (self *EntityBuilder) SetForeignKeys(fks ...ForeignKeyer) *EntityBuilder { - self.foreignKeys = fks - return self -} -func (self *EntityBuilder) Metadata() (name, comment string) { return self.name, self.comment } -func (self *EntityBuilder) Table() TableDef { return self.table } -func (self *EntityBuilder) Fields() []Fielder { return self.fields } -func (self *EntityBuilder) Indexes() []Indexer { return self.indexes } -func (self *EntityBuilder) ForeignKeys() []ForeignKeyer { return self.foreignKeys } -func (self *EntityBuilder) Build(opt *Option) *EntityDescriptor { - fielders := self.Fields() - fields := make([]*FieldDescriptor, 0, len(fielders)) - for _, fb := range fielders { - field := fb.Build(opt) - fields = append(fields, field) - } - indexers := self.Indexes() - indexes := make([]*IndexDescriptor, 0, len(indexers)) - for _, v := range indexers { - indexes = append(indexes, v.Build()) - } - fkers := self.ForeignKeys() - fks := make([]*ForeignKeyDescriptor, 0, len(fkers)) - for _, v := range fkers { - fks = append(fks, v.Build()) - } - name, comment := self.Metadata() - return &EntityDescriptor{ - Name: name, - Comment: comment, - Table: self.Table(), - Fields: fields, - Indexes: indexes, - ForeignKeys: fks, - } -} diff --git a/field.go b/field.go index 0c8530b..28ce141 100644 --- a/field.go +++ b/field.go @@ -19,14 +19,14 @@ type FieldDescriptor struct { Tags []string // Tags struct tag } -func (field *FieldDescriptor) goType(typ any) { +func (field *FieldDescriptor) GoType(typ any) { field.Type = NewGoType(field.Type.Type, typ) } func (field *FieldDescriptor) build(opt *Option) { if field.Name == "deleted_at" && field.Type.IsInteger() { field.Optional = false - field.goType(soft_delete.DeletedAt(0)) + field.GoType(soft_delete.DeletedAt(0)) } if opt == nil { return @@ -35,13 +35,13 @@ func (field *FieldDescriptor) build(opt *Option) { if opt.EnableInt { switch field.Type.Type { case TypeInt8, TypeInt16, TypeInt32: - field.goType(int(0)) + field.GoType(int(0)) case TypeUint8, TypeUint16, TypeUint32: - field.goType(uint(0)) + field.GoType(uint(0)) } } if opt.EnableBoolInt && field.Type.IsBool() { - field.goType(int(0)) + field.GoType(int(0)) } if field.Nullable && opt.DisableNullToPoint { gt, ok := sqlNullValueGoType[field.Type.Type] diff --git a/field_builder.go b/field_builder.go deleted file mode 100644 index bc92349..0000000 --- a/field_builder.go +++ /dev/null @@ -1,111 +0,0 @@ -package ens - -import ( - "database/sql/driver" - - "github.com/things-go/ens/internal/insql" -) - -func Bool(name string) *FieldBuilder { return Field(BoolType(), name) } -func Int(name string) *FieldBuilder { return Field(IntType(), name) } -func Int8(name string) *FieldBuilder { return Field(Int8Type(), name) } -func Int16(name string) *FieldBuilder { return Field(Int16Type(), name) } -func Int32(name string) *FieldBuilder { return Field(Int32Type(), name) } -func Int64(name string) *FieldBuilder { return Field(Int64Type(), name) } -func Uint(name string) *FieldBuilder { return Field(UintType(), name) } -func Uint8(name string) *FieldBuilder { return Field(Uint8Type(), name) } -func Uint16(name string) *FieldBuilder { return Field(Uint16Type(), name) } -func Uint32(name string) *FieldBuilder { return Field(Uint32Type(), name) } -func Uint64(name string) *FieldBuilder { return Field(Uint64Type(), name) } -func Float32(name string) *FieldBuilder { return Field(Float32Type(), name) } -func Float64(name string) *FieldBuilder { return Field(Float64Type(), name) } -func String(name string) *FieldBuilder { return Field(StringType(), name) } -func Bytes(name string) *FieldBuilder { return Field(BytesType(), name) } -func JSON(name string) *FieldBuilder { return Field(JSONRawMessageType(), name) } -func Enum(name string) *FieldBuilder { return Field(EnumType(), name) } -func Time(name string) *FieldBuilder { return Field(TimeType(), name) } -func UUID(name string, typ driver.Valuer) *FieldBuilder { return Field(NewGoType(TypeUUID, typ), name) } - -var _ Fielder = (*FieldBuilder)(nil) - -// FieldBuilder is the builder for field. -type FieldBuilder struct { - inner *FieldDescriptor -} - -// Field returns a new Field with the type. -func Field(t *GoType, name string) *FieldBuilder { - return &FieldBuilder{ - inner: &FieldDescriptor{ - Name: name, - Type: t, - }, - } -} - -// FieldFromDef returns a new Field with the type and ColumnDef. -// auto set name, comment, nullable, column and optional. -func FieldFromDef(t *GoType, def ColumnDef) *FieldBuilder { - col := def.Column() - return &FieldBuilder{ - inner: &FieldDescriptor{ - Name: col.Name, - Comment: insql.MustComment(col.Attrs), - Nullable: col.Type.Null, - Column: def, - Type: t, - Optional: col.Type.Null, - }, - } -} - -// Column the column expression of the field. -func (b *FieldBuilder) Column(e ColumnDef) *FieldBuilder { - b.inner.Column = e - return b -} - -// Comment sets the comment of the field. -func (b *FieldBuilder) Comment(c string) *FieldBuilder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *FieldBuilder) Nullable() *FieldBuilder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Bool("deleted"). -// GoType(sql.NullBool{}) -// field.Bytes("ip"). -// GoType(net.IP("127.0.0.1")) -// field.String("dir"). -// GoType(http.Dir("dir")) -func (b *FieldBuilder) GoType(typ any) *FieldBuilder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *FieldBuilder) Optional() *FieldBuilder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -func (b *FieldBuilder) Tags(tags ...string) *FieldBuilder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *FieldBuilder) Build(opt *Option) *FieldDescriptor { - b.inner.build(opt) - return b.inner -} diff --git a/foreign_key_builder.go b/foreign_key_builder.go deleted file mode 100644 index 5eb6f7d..0000000 --- a/foreign_key_builder.go +++ /dev/null @@ -1,68 +0,0 @@ -package ens - -import ( - "ariga.io/atlas/sql/schema" - "github.com/things-go/ens/internal/insql" -) - -// ForeignKeyFromDef returns a new ForeignKey with the ForeignKeyDef. -func ForeignKey(symbol string) *foreignKeyBuilder { - return &foreignKeyBuilder{ - inner: &ForeignKeyDescriptor{ - Symbol: symbol, - Table: "", - Columns: nil, - RefTable: "", - RefColumns: nil, - OnUpdate: schema.Restrict, - OnDelete: schema.Restrict, - ForeignKey: nil, - }, - } -} - -// ForeignKeyFromDef returns a new ForeignKey with the ForeignKeyDef. -func ForeignKeyFromDef(def ForeignKeyDef) *foreignKeyBuilder { - fk := def.ForeignKey() - return &foreignKeyBuilder{ - inner: &ForeignKeyDescriptor{ - Symbol: fk.Symbol, - Table: fk.Table.Name, - Columns: insql.ColumnNames(fk.Columns), - RefTable: fk.RefTable.Name, - RefColumns: insql.ColumnNames(fk.RefColumns), - OnUpdate: fk.OnUpdate, - OnDelete: fk.OnDelete, - ForeignKey: def, - }, - } -} - -// foreignKeyBuilder is the builder for ForeignKey. -type foreignKeyBuilder struct { - inner *ForeignKeyDescriptor -} - -func (b *foreignKeyBuilder) Table(tbName string, columns []string) *foreignKeyBuilder { - b.inner.Table = tbName - b.inner.Columns = columns - return b -} -func (b *foreignKeyBuilder) RefTable(tbName string, columns []string) *foreignKeyBuilder { - b.inner.RefTable = tbName - b.inner.RefColumns = columns - return b -} -func (b *foreignKeyBuilder) OnDelete(v schema.ReferenceOption) *foreignKeyBuilder { - b.inner.OnDelete = v - return b -} -func (b *foreignKeyBuilder) OnUpdate(v schema.ReferenceOption) *foreignKeyBuilder { - b.inner.OnUpdate = v - return b -} - -// Build implements the ForeignKeyer interface by returning its descriptor. -func (b *foreignKeyBuilder) Build() *ForeignKeyDescriptor { - return b.inner -} diff --git a/index_builder.go b/index_builder.go deleted file mode 100644 index 0662e89..0000000 --- a/index_builder.go +++ /dev/null @@ -1,41 +0,0 @@ -package ens - -import "github.com/things-go/ens/internal/insql" - -type indexBuilder struct { - inner *IndexDescriptor -} - -// Index returns a new Index with the name. -func Index(name string) *indexBuilder { - return &indexBuilder{ - inner: &IndexDescriptor{ - Name: name, - Fields: []string{}, - }, - } -} - -// IndexFromDef returns a new Index with the IndexDef. -// auto set name, fields, index. -func IndexFromDef(e IndexDef) *indexBuilder { - index := e.Index() - return &indexBuilder{ - inner: &IndexDescriptor{ - Name: index.Name, - Fields: insql.IndexPartColumnNames(index.Parts), - Index: e, - }, - } -} -func (b *indexBuilder) Fields(fields ...string) *indexBuilder { - b.inner.Fields = append(b.inner.Fields, fields...) - return b -} -func (b *indexBuilder) Index(i IndexDef) *indexBuilder { - b.inner.Index = i - return b -} -func (b *indexBuilder) Build() *IndexDescriptor { - return b.inner -} diff --git a/model_parse.go b/model_parse.go index 7a20097..12fc8ba 100644 --- a/model_parse.go +++ b/model_parse.go @@ -14,7 +14,7 @@ import ( var rowScanner = reflect.TypeOf((*sql.Scanner)(nil)).Elem() var rowValuer = reflect.TypeOf((*driver.Valuer)(nil)).Elem() -func ParseModel(v any) (MixinEntity, error) { +func ParseModel(v any) (*EntityDescriptor, error) { value := reflect.ValueOf(v) if value.Kind() == reflect.Pointer && value.IsNil() { return nil, nil @@ -26,15 +26,18 @@ func ParseModel(v any) (MixinEntity, error) { if vt.Kind() != reflect.Struct { return nil, fmt.Errorf("%s is not a struct", vt.String()) } - entityBuilder := &EntityBuilder{} - fields := structToFielders(vt) - return entityBuilder. - SetMetadata(utils.SnakeCase(vt.Name()), ""). - SetFields(fields...), nil + return &EntityDescriptor{ + Name: utils.SnakeCase(vt.Name()), + Comment: "", + Table: nil, + Fields: structToFielders(vt), + Indexes: nil, + ForeignKeys: nil, + }, nil } -func structToFielders(vt reflect.Type) []Fielder { - fields := make([]Fielder, 0, vt.NumField()) +func structToFielders(vt reflect.Type) []*FieldDescriptor { + fields := make([]*FieldDescriptor, 0, vt.NumField()) for i := 0; i < vt.NumField(); i++ { fv := vt.Field(i) if !fv.IsExported() { // ignore unexported field @@ -63,7 +66,15 @@ func structToFielders(vt reflect.Type) []Fielder { } fields = append( fields, - Field(newGoType(t, fv.Type), utils.SnakeCase(fv.Name)), + &FieldDescriptor{ + Name: utils.SnakeCase(fv.Name), + Comment: "", + Nullable: false, + Column: nil, + Type: newGoType(t, fv.Type), + Optional: false, + Tags: []string{}, + }, ) } } diff --git a/schema.go b/schema.go index 7454227..32ce01b 100644 --- a/schema.go +++ b/schema.go @@ -5,20 +5,3 @@ type Schema struct { Name string // schema name Entities []*EntityDescriptor // schema entity. } - -// MixinSchema information -type MixinSchema struct { - Name string // schema name - Entities []MixinEntity // schema entity. -} - -func (self *MixinSchema) Build(opt *Option) *Schema { - entities := make([]*EntityDescriptor, 0, len(self.Entities)) - for _, mixin := range self.Entities { - entities = append(entities, mixin.Build(opt)) - } - return &Schema{ - Name: self.Name, - Entities: entities, - } -}