mirror of
https://gitee.com/johng/gf
synced 2026-07-04 04:52:48 +08:00
fix: 调整目录细化功能
This commit is contained in:
13
cmd/gf/internal/cmd/gen/tpl/readme.md
Normal file
13
cmd/gf/internal/cmd/gen/tpl/readme.md
Normal file
@ -0,0 +1,13 @@
|
||||
## table
|
||||
|
||||
### feild
|
||||
|
||||
1. 字段名
|
||||
2. 类型
|
||||
3. 对应的 go 类型
|
||||
4. 是否主键
|
||||
5. 是否唯一键
|
||||
6. 备注
|
||||
7. 默认值
|
||||
8. 是否自增
|
||||
9. 默认值
|
||||
@ -2,10 +2,7 @@ package tpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
_ "github.com/gogf/gf/contrib/drivers/clickhouse/v2"
|
||||
_ "github.com/gogf/gf/contrib/drivers/mssql/v2"
|
||||
@ -16,8 +13,6 @@ import (
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gview"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
// init description
|
||||
@ -33,208 +28,34 @@ func init() {
|
||||
gdb.AddConfigNode("test", nodeDefault)
|
||||
}
|
||||
|
||||
var (
|
||||
defaultTypeMapping = map[DBFieldTypeName]CustomAttributeType{
|
||||
"decimal": {
|
||||
Type: "float64",
|
||||
},
|
||||
"money": {
|
||||
Type: "float64",
|
||||
},
|
||||
"numeric": {
|
||||
Type: "float64",
|
||||
},
|
||||
"smallmoney": {
|
||||
Type: "float64",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
type (
|
||||
DBFieldTypeName = string
|
||||
CustomAttributeType struct {
|
||||
Type string `brief:"custom attribute type name"`
|
||||
Import string `brief:"custom import for this type"`
|
||||
}
|
||||
)
|
||||
|
||||
// TableField description
|
||||
type TableField struct {
|
||||
gdb.TableField
|
||||
LocalType string
|
||||
JsonName string
|
||||
}
|
||||
|
||||
type TableFields []*TableField
|
||||
|
||||
func (s TableFields) Len() int { return len(s) }
|
||||
func (s TableFields) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s TableFields) Less(i, j int) bool {
|
||||
return strings.Compare(s[i].Name, s[j].Name) < 0
|
||||
}
|
||||
|
||||
// Tables description
|
||||
type Tables struct {
|
||||
Name string
|
||||
db gdb.DB
|
||||
Fields TableFields
|
||||
FieldsSource map[string]*gdb.TableField
|
||||
Imports map[string]struct{}
|
||||
}
|
||||
|
||||
// Name description
|
||||
// GetTables description
|
||||
//
|
||||
// createTime: 2023-10-23 16:17:30
|
||||
// createTime: 2023-12-11 16:21:43
|
||||
//
|
||||
// author: hailaz
|
||||
func (t *Tables) Show() string {
|
||||
return fmt.Sprintf("table name is %s", t.Name)
|
||||
}
|
||||
|
||||
// toTableFields description
|
||||
//
|
||||
// createTime: 2023-10-23 17:22:40
|
||||
//
|
||||
// author: hailaz
|
||||
func (t *Tables) toTableFields() {
|
||||
t.Fields = make(TableFields, len(t.FieldsSource))
|
||||
for _, v := range t.FieldsSource {
|
||||
field := &TableField{
|
||||
TableField: *v,
|
||||
}
|
||||
appendImport := field.GetLocalTypeName(context.Background(), t.db, Input{
|
||||
TypeMapping: defaultTypeMapping,
|
||||
StdTime: false,
|
||||
})
|
||||
if appendImport != "" {
|
||||
t.Imports[appendImport] = struct{}{}
|
||||
}
|
||||
t.Fields[v.Index] = field
|
||||
}
|
||||
}
|
||||
|
||||
// SortFields description
|
||||
//
|
||||
// createTime: 2023-10-23 17:18:22
|
||||
//
|
||||
// author: hailaz
|
||||
func (t *Tables) SortFields(isReverse bool) {
|
||||
if isReverse {
|
||||
sort.Sort(sort.Reverse(t.Fields))
|
||||
} else {
|
||||
sort.Sort(t.Fields)
|
||||
}
|
||||
}
|
||||
|
||||
// JsonStr description
|
||||
//
|
||||
// createTime: 2023-10-23 17:29:39
|
||||
//
|
||||
// author: hailaz
|
||||
func (t *Tables) FieldsJsonStr() string {
|
||||
mapStr := make(map[string]interface{}, len(t.Fields))
|
||||
for _, v := range t.Fields {
|
||||
mapStr[v.Name] = ""
|
||||
}
|
||||
b, _ := json.Marshal(mapStr)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// Input description
|
||||
type Input struct {
|
||||
StdTime bool
|
||||
GJsonSupport bool
|
||||
TypeMapping map[DBFieldTypeName]CustomAttributeType
|
||||
}
|
||||
|
||||
// GetLocalTypeName description
|
||||
//
|
||||
// createTime: 2023-10-25 15:43:06
|
||||
//
|
||||
// author: hailaz
|
||||
func (field *TableField) GetLocalTypeName(ctx context.Context, db gdb.DB, in Input) (appendImport string) {
|
||||
var (
|
||||
err error
|
||||
localTypeName gdb.LocalType
|
||||
localTypeNameStr string
|
||||
)
|
||||
if in.TypeMapping != nil && len(in.TypeMapping) > 0 {
|
||||
var (
|
||||
tryTypeName string
|
||||
)
|
||||
tryTypeMatch, _ := gregex.MatchString(`(.+?)\((.+)\)`, field.Type)
|
||||
if len(tryTypeMatch) == 3 {
|
||||
tryTypeName = gstr.Trim(tryTypeMatch[1])
|
||||
} else {
|
||||
tryTypeName = gstr.Split(field.Type, " ")[0]
|
||||
}
|
||||
if tryTypeName != "" {
|
||||
if typeMapping, ok := in.TypeMapping[strings.ToLower(tryTypeName)]; ok {
|
||||
localTypeNameStr = typeMapping.Type
|
||||
appendImport = typeMapping.Import
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if localTypeNameStr == "" {
|
||||
localTypeName, err = db.CheckLocalTypeForField(ctx, field.Type, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
localTypeNameStr = string(localTypeName)
|
||||
switch localTypeName {
|
||||
case gdb.LocalTypeDate, gdb.LocalTypeDatetime:
|
||||
if in.StdTime {
|
||||
localTypeNameStr = "time.Time"
|
||||
} else {
|
||||
localTypeNameStr = "*gtime.Time"
|
||||
appendImport = "github.com/gogf/gf/v2/os/gtime"
|
||||
}
|
||||
|
||||
case gdb.LocalTypeInt64Bytes:
|
||||
localTypeNameStr = "int64"
|
||||
|
||||
case gdb.LocalTypeUint64Bytes:
|
||||
localTypeNameStr = "uint64"
|
||||
|
||||
// Special type handle.
|
||||
case gdb.LocalTypeJson, gdb.LocalTypeJsonb:
|
||||
if in.GJsonSupport {
|
||||
localTypeNameStr = "*gjson.Json"
|
||||
appendImport = "github.com/gogf/gf/v2/encoding/gjson"
|
||||
} else {
|
||||
localTypeNameStr = "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
field.LocalType = localTypeNameStr
|
||||
field.JsonName = gstr.CaseConvert(field.Name, gstr.Camel)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func Tpl() {
|
||||
ctx := context.TODO()
|
||||
db, _ := gdb.Instance("test")
|
||||
func GetTables(ctx context.Context, db gdb.DB) Tables {
|
||||
tablesName, err := db.Tables(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(tablesName)
|
||||
tables := make([]Tables, 0)
|
||||
tables := make(Tables, 0)
|
||||
for _, v := range tablesName {
|
||||
f, _ := db.TableFields(ctx, v)
|
||||
t := Tables{
|
||||
Name: v,
|
||||
FieldsSource: f,
|
||||
db: db,
|
||||
Imports: make(map[string]struct{}),
|
||||
t, err := NewTable(ctx, db, v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
t.toTableFields()
|
||||
t.SortFields(true)
|
||||
tables = append(tables, t)
|
||||
}
|
||||
return tables
|
||||
}
|
||||
|
||||
func Tpl() {
|
||||
ctx := context.TODO()
|
||||
db, _ := gdb.Instance("test")
|
||||
|
||||
tables := GetTables(ctx, db)
|
||||
view := gview.New()
|
||||
res, err := view.ParseContent(ctx, `{{range $i,$v := .tables}}{{$i}} ---- {{$v}} {{$v.Name}}
|
||||
{{$v.FieldsJsonStr}}
|
||||
|
||||
98
cmd/gf/internal/cmd/gen/tpl/tpl_field.go
Normal file
98
cmd/gf/internal/cmd/gen/tpl/tpl_field.go
Normal file
@ -0,0 +1,98 @@
|
||||
package tpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
// TableField description
|
||||
type TableField struct {
|
||||
gdb.TableField
|
||||
LocalType string
|
||||
JsonName string
|
||||
}
|
||||
|
||||
type TableFields []*TableField
|
||||
|
||||
func (s TableFields) Len() int { return len(s) }
|
||||
func (s TableFields) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s TableFields) Less(i, j int) bool {
|
||||
return strings.Compare(s[i].Name, s[j].Name) < 0
|
||||
}
|
||||
|
||||
// Input description
|
||||
type Input struct {
|
||||
StdTime bool
|
||||
GJsonSupport bool
|
||||
TypeMapping map[DBFieldTypeName]CustomAttributeType
|
||||
}
|
||||
|
||||
// GetLocalTypeName description
|
||||
//
|
||||
// createTime: 2023-10-25 15:43:06
|
||||
//
|
||||
// author: hailaz
|
||||
func (field *TableField) GetLocalTypeName(ctx context.Context, db gdb.DB, in Input) (appendImport string) {
|
||||
var (
|
||||
err error
|
||||
localTypeName gdb.LocalType
|
||||
localTypeNameStr string
|
||||
)
|
||||
if in.TypeMapping != nil && len(in.TypeMapping) > 0 {
|
||||
var (
|
||||
tryTypeName string
|
||||
)
|
||||
tryTypeMatch, _ := gregex.MatchString(`(.+?)\((.+)\)`, field.Type)
|
||||
if len(tryTypeMatch) == 3 {
|
||||
tryTypeName = gstr.Trim(tryTypeMatch[1])
|
||||
} else {
|
||||
tryTypeName = gstr.Split(field.Type, " ")[0]
|
||||
}
|
||||
if tryTypeName != "" {
|
||||
if typeMapping, ok := in.TypeMapping[strings.ToLower(tryTypeName)]; ok {
|
||||
localTypeNameStr = typeMapping.Type
|
||||
appendImport = typeMapping.Import
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if localTypeNameStr == "" {
|
||||
localTypeName, err = db.CheckLocalTypeForField(ctx, field.Type, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
localTypeNameStr = string(localTypeName)
|
||||
switch localTypeName {
|
||||
case gdb.LocalTypeDate, gdb.LocalTypeDatetime:
|
||||
if in.StdTime {
|
||||
localTypeNameStr = "time.Time"
|
||||
} else {
|
||||
localTypeNameStr = "*gtime.Time"
|
||||
appendImport = "github.com/gogf/gf/v2/os/gtime"
|
||||
}
|
||||
|
||||
case gdb.LocalTypeInt64Bytes:
|
||||
localTypeNameStr = "int64"
|
||||
|
||||
case gdb.LocalTypeUint64Bytes:
|
||||
localTypeNameStr = "uint64"
|
||||
|
||||
// Special type handle.
|
||||
case gdb.LocalTypeJson, gdb.LocalTypeJsonb:
|
||||
if in.GJsonSupport {
|
||||
localTypeNameStr = "*gjson.Json"
|
||||
appendImport = "github.com/gogf/gf/v2/encoding/gjson"
|
||||
} else {
|
||||
localTypeNameStr = "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
field.LocalType = localTypeNameStr
|
||||
field.JsonName = gstr.CaseConvert(field.Name, gstr.Camel)
|
||||
|
||||
return
|
||||
}
|
||||
127
cmd/gf/internal/cmd/gen/tpl/tpl_table.go
Normal file
127
cmd/gf/internal/cmd/gen/tpl/tpl_table.go
Normal file
@ -0,0 +1,127 @@
|
||||
package tpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultTypeMapping = map[DBFieldTypeName]CustomAttributeType{
|
||||
"decimal": {
|
||||
Type: "float64",
|
||||
},
|
||||
"money": {
|
||||
Type: "float64",
|
||||
},
|
||||
"numeric": {
|
||||
Type: "float64",
|
||||
},
|
||||
"smallmoney": {
|
||||
Type: "float64",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
type (
|
||||
DBFieldTypeName = string
|
||||
CustomAttributeType struct {
|
||||
Type string `brief:"custom attribute type name"`
|
||||
Import string `brief:"custom import for this type"`
|
||||
}
|
||||
)
|
||||
|
||||
// Table description
|
||||
type Table struct {
|
||||
Name string
|
||||
db gdb.DB
|
||||
Fields TableFields
|
||||
FieldsSource map[string]*gdb.TableField
|
||||
Imports map[string]struct{}
|
||||
}
|
||||
|
||||
type Tables []*Table
|
||||
|
||||
// NewTable description
|
||||
//
|
||||
// createTime: 2023-12-11 16:17:33
|
||||
//
|
||||
// author: hailaz
|
||||
func NewTable(ctx context.Context, db gdb.DB, tableName string) (*Table, error) {
|
||||
fields, err := db.TableFields(ctx, tableName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
table := Table{
|
||||
Name: tableName,
|
||||
FieldsSource: fields,
|
||||
db: db,
|
||||
Imports: make(map[string]struct{}),
|
||||
}
|
||||
table.toTableFields()
|
||||
return &table, nil
|
||||
}
|
||||
|
||||
// Name description
|
||||
//
|
||||
// createTime: 2023-10-23 16:17:30
|
||||
//
|
||||
// author: hailaz
|
||||
func (t *Table) Show() string {
|
||||
return fmt.Sprintf("table name is %s", t.Name)
|
||||
}
|
||||
|
||||
// toTableFields description
|
||||
//
|
||||
// createTime: 2023-10-23 17:22:40
|
||||
//
|
||||
// author: hailaz
|
||||
func (t *Table) toTableFields() {
|
||||
t.Fields = make(TableFields, len(t.FieldsSource))
|
||||
for _, v := range t.FieldsSource {
|
||||
field := &TableField{
|
||||
TableField: *v,
|
||||
}
|
||||
appendImport := field.GetLocalTypeName(context.Background(), t.db, Input{
|
||||
TypeMapping: defaultTypeMapping,
|
||||
StdTime: false,
|
||||
})
|
||||
if appendImport != "" {
|
||||
t.Imports[appendImport] = struct{}{}
|
||||
}
|
||||
t.Fields[v.Index] = field
|
||||
}
|
||||
}
|
||||
|
||||
// SortFields description
|
||||
//
|
||||
// createTime: 2023-10-23 17:18:22
|
||||
//
|
||||
// author: hailaz
|
||||
func (t *Table) SortFields(isReverse bool) {
|
||||
if isReverse {
|
||||
sort.Sort(sort.Reverse(t.Fields))
|
||||
} else {
|
||||
sort.Sort(t.Fields)
|
||||
}
|
||||
}
|
||||
|
||||
// JsonStr description
|
||||
//
|
||||
// createTime: 2023-10-23 17:29:39
|
||||
//
|
||||
// author: hailaz
|
||||
func (t *Table) FieldsJsonStr() string {
|
||||
mapStr := make(map[string]interface{}, len(t.Fields))
|
||||
for _, v := range t.Fields {
|
||||
mapStr[v.Name] = v.Default
|
||||
}
|
||||
b, err := json.Marshal(mapStr)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
Reference in New Issue
Block a user