feat: add transformer for model
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"go.ipao.vip/atomctl/pkg/ast/model"
|
||||||
astModel "go.ipao.vip/atomctl/pkg/ast/model"
|
astModel "go.ipao.vip/atomctl/pkg/ast/model"
|
||||||
pgDatabase "go.ipao.vip/atomctl/pkg/postgres"
|
pgDatabase "go.ipao.vip/atomctl/pkg/postgres"
|
||||||
"go.ipao.vip/atomctl/pkg/utils/gomod"
|
"go.ipao.vip/atomctl/pkg/utils/gomod"
|
||||||
@@ -43,11 +44,6 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
|||||||
return errors.Wrap(err, "get db")
|
return errors.Wrap(err, "get db")
|
||||||
}
|
}
|
||||||
|
|
||||||
type Transformer struct {
|
|
||||||
Ignores []string `mapstructure:"ignores"`
|
|
||||||
Types map[string]map[string]string `mapstructure:"types"`
|
|
||||||
}
|
|
||||||
|
|
||||||
v := viper.New()
|
v := viper.New()
|
||||||
v.SetConfigType("yaml")
|
v.SetConfigType("yaml")
|
||||||
v.SetConfigFile("database/transform.yaml")
|
v.SetConfigFile("database/transform.yaml")
|
||||||
@@ -56,8 +52,8 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var conf Transformer
|
var transformer model.Transformer
|
||||||
if err := v.Unmarshal(&conf); err != nil {
|
if err := v.Unmarshal(&transformer); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +81,7 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
|||||||
UseTable(func(table metadata.Table) template.TableSQLBuilder {
|
UseTable(func(table metadata.Table) template.TableSQLBuilder {
|
||||||
tbl := template.DefaultTableSQLBuilder(table)
|
tbl := template.DefaultTableSQLBuilder(table)
|
||||||
|
|
||||||
if lo.Contains(conf.Ignores, table.Name) {
|
if lo.Contains(transformer.Ignores.Jet, table.Name) {
|
||||||
tbl.Skip = true
|
tbl.Skip = true
|
||||||
log.Infof("Skip table %s", table.Name)
|
log.Infof("Skip table %s", table.Name)
|
||||||
}
|
}
|
||||||
@@ -93,7 +89,7 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
|||||||
}).
|
}).
|
||||||
UseEnum(func(meta metadata.Enum) template.EnumSQLBuilder {
|
UseEnum(func(meta metadata.Enum) template.EnumSQLBuilder {
|
||||||
enum := template.DefaultEnumSQLBuilder(meta)
|
enum := template.DefaultEnumSQLBuilder(meta)
|
||||||
if lo.Contains(conf.Ignores, meta.Name) {
|
if lo.Contains(transformer.Ignores.Jet, meta.Name) {
|
||||||
enum.Skip = true
|
enum.Skip = true
|
||||||
log.Infof("Skip enum %s", meta.Name)
|
log.Infof("Skip enum %s", meta.Name)
|
||||||
}
|
}
|
||||||
@@ -105,7 +101,7 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
|||||||
DefaultModel().
|
DefaultModel().
|
||||||
UseTable(func(table metadata.Table) template.TableModel {
|
UseTable(func(table metadata.Table) template.TableModel {
|
||||||
tbl := template.DefaultTableModel(table)
|
tbl := template.DefaultTableModel(table)
|
||||||
if lo.Contains(conf.Ignores, table.Name) {
|
if lo.Contains(transformer.Ignores.Jet, table.Name) {
|
||||||
tbl.Skip = true
|
tbl.Skip = true
|
||||||
log.Infof("Skip table %s", table.Name)
|
log.Infof("Skip table %s", table.Name)
|
||||||
return tbl
|
return tbl
|
||||||
@@ -121,7 +117,7 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
|||||||
return defaultTableModelField
|
return defaultTableModelField
|
||||||
}
|
}
|
||||||
|
|
||||||
fields, ok := conf.Types[table.Name]
|
fields, ok := transformer.Types[table.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return defaultTableModelField
|
return defaultTableModelField
|
||||||
}
|
}
|
||||||
@@ -169,5 +165,5 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
|||||||
if err := os.Rename(dataPath, "database/schemas"); err != nil {
|
if err := os.Rename(dataPath, "database/schemas"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return astModel.Generate(generatedTables)
|
return astModel.Generate(generatedTables, transformer)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -22,7 +23,7 @@ type TableModelParam struct {
|
|||||||
PascalTable string // User
|
PascalTable string // User
|
||||||
}
|
}
|
||||||
|
|
||||||
func Generate(tables []string) error {
|
func Generate(tables []string, transformer Transformer) error {
|
||||||
baseDir := "app/models"
|
baseDir := "app/models"
|
||||||
|
|
||||||
tableTpl := template.Must(template.New("model").Parse(string(tableTpl)))
|
tableTpl := template.Must(template.New("model").Parse(string(tableTpl)))
|
||||||
@@ -30,6 +31,11 @@ func Generate(tables []string) error {
|
|||||||
|
|
||||||
items := []TableModelParam{}
|
items := []TableModelParam{}
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
|
if lo.Contains(transformer.Ignores.Model, table) {
|
||||||
|
log.Printf("[WARN] skip model %s\n", table)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
items = append(items, TableModelParam{
|
items = append(items, TableModelParam{
|
||||||
CamelTable: lo.CamelCase(table),
|
CamelTable: lo.CamelCase(table),
|
||||||
PascalTable: lo.PascalCase(table),
|
PascalTable: lo.PascalCase(table),
|
||||||
@@ -68,7 +74,16 @@ func Generate(tables []string) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.HasSuffix(name, "_test.go") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
baseName := strings.TrimSuffix(name, filepath.Ext(name))
|
baseName := strings.TrimSuffix(name, filepath.Ext(name))
|
||||||
|
if lo.Contains(transformer.Ignores.Model, baseName) {
|
||||||
|
log.Printf("[WARN] skip model %s\n", baseName)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if !lo.Contains(tables, baseName) {
|
if !lo.Contains(tables, baseName) {
|
||||||
items = append(items, TableModelParam{
|
items = append(items, TableModelParam{
|
||||||
CamelTable: lo.CamelCase(baseName),
|
CamelTable: lo.CamelCase(baseName),
|
||||||
|
|||||||
9
pkg/ast/model/transformer.go
Normal file
9
pkg/ast/model/transformer.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
type Transformer struct {
|
||||||
|
Ignores struct {
|
||||||
|
Jet []string `mapstructure:"jet"`
|
||||||
|
Model []string `mapstructure:"model"`
|
||||||
|
} `mapstructure:"ignores"`
|
||||||
|
Types map[string]map[string]string `mapstructure:"types"`
|
||||||
|
}
|
||||||
@@ -4,8 +4,8 @@ import "github.com/samber/lo"
|
|||||||
|
|
||||||
type Pager struct {
|
type Pager struct {
|
||||||
Pagination `json:",inline"`
|
Pagination `json:",inline"`
|
||||||
Total int64 `json:"total"`
|
Total int64 `json:"total"`
|
||||||
Items interface{} `json:"items"`
|
Items any `json:"items"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Pagination struct {
|
type Pagination struct {
|
||||||
|
|||||||
@@ -11,11 +11,11 @@ plugins:
|
|||||||
- local: protoc-gen-go
|
- local: protoc-gen-go
|
||||||
out: pkg/proto
|
out: pkg/proto
|
||||||
opt: paths=source_relative
|
opt: paths=source_relative
|
||||||
- local: protoc-gen-grpc-gateway
|
#- local: protoc-gen-grpc-gateway
|
||||||
out: pkg/proto
|
# out: pkg/proto
|
||||||
opt:
|
# opt:
|
||||||
- paths=source_relative
|
# - paths=source_relative
|
||||||
- generate_unbound_methods=true
|
# - generate_unbound_methods=true
|
||||||
- local: protoc-gen-go-grpc
|
- local: protoc-gen-go-grpc
|
||||||
out: pkg/proto
|
out: pkg/proto
|
||||||
opt: paths=source_relative
|
opt: paths=source_relative
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
ignores:
|
ignores:
|
||||||
- migrations
|
jet:
|
||||||
- river_leader
|
- migrations
|
||||||
- river_job
|
- river_leader
|
||||||
- river_client
|
- river_job
|
||||||
- river_client_queue
|
- river_client
|
||||||
- river_queue
|
- river_client_queue
|
||||||
# types:
|
- river_queue
|
||||||
|
model:
|
||||||
|
types:
|
||||||
# users: # table name
|
# users: # table name
|
||||||
# meta: UserMeta
|
# meta: UserMeta
|
||||||
# meta: Json[UserMeta]
|
# meta: Json[UserMeta]
|
||||||
|
|||||||
Reference in New Issue
Block a user