Skip to content

Commit

Permalink
fix: fix ens
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed May 10, 2024
1 parent ddde021 commit 8fbe9e0
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 409 deletions.
16 changes: 8 additions & 8 deletions cmd/ormat/command/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type CodeGen struct {
Version string
PackageName string
DisableDocComment bool
Option
}

// Bytes returns the CodeBuf's buffer.
Expand Down Expand Up @@ -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
Expand Down
26 changes: 0 additions & 26 deletions def.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 50 additions & 0 deletions driver/mysql/def_atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}
}
29 changes: 0 additions & 29 deletions driver/mysql/def_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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...)
}
8 changes: 4 additions & 4 deletions driver/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions driver/mysql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions driver/mysql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"testing"

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

Expand Down Expand Up @@ -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])
}
8 changes: 4 additions & 4 deletions driver/mysql/sql_tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
81 changes: 0 additions & 81 deletions entity_builder.go

This file was deleted.

Loading

0 comments on commit 8fbe9e0

Please sign in to comment.