-
Notifications
You must be signed in to change notification settings - Fork 6
/
parser.go
executable file
·197 lines (189 loc) · 4.3 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package tto
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/ixre/gof/db/db"
"github.com/ixre/gof/log"
"github.com/ixre/gof/util"
)
// 获取表结构,userMeta 是否使用用户的元数据
func parseTable(ordinal int, tb *db.Table, shortUpper bool, userMeta bool) *Table {
n := &Table{
Ordinal: ordinal,
Name: tb.Name,
Prefix: prefix(tb.Name),
Title: title(tb.Name, shortUpper),
ShortTitle: shortTitle(tb.Name),
Comment: tb.Comment,
Engine: tb.Engine,
Schema: tb.Schema,
Charset: tb.Charset,
Raw: tb,
Pk: "",
PkProp: "",
PkType: db.TypeInt32,
Columns: make([]*Column, len(tb.Columns)),
}
if len(n.Comment) == 0 {
n.Comment = n.Title
}
for i, v := range tb.Columns {
if v.IsPk && n.Pk != "" {
n.Pk = v.Name
n.PkProp = title(v.Name, shortUpper)
n.PkType = v.Type
}
c := &Column{
Ordinal: i,
Name: v.Name,
Prop: title(v.Name, shortUpper),
IsPk: v.IsPk,
IsAuto: v.IsAuto,
NotNull: v.NotNull,
DbType: v.DbType,
Comment: v.Comment,
Length: v.Length,
Type: v.Type,
}
if len(c.Comment) == 0 {
c.Comment = c.Prop
}
// 兼容JAVA项目int主键
if CompactMode && c.DbType == "int(11)" {
c.Type = db.TypeInt32
}
n.Columns[i] = c
}
// 如果未设置主键,则默认第一个列
if n.Pk == "" {
fc := tb.Columns[0]
n.Pk = fc.Name
n.PkProp = title(fc.Name, shortUpper)
n.PkType = fc.Type
}
return loadUserMeta(n, userMeta)
}
// 读取并更新元数据
func loadUserMeta(t *Table, userMeta bool) *Table {
cfg := &TableConfig{}
dstPath := fmt.Sprintf("./meta-settings/%s.json", t.Name)
if userMeta {
f, err := os.Open(dstPath)
// 如果不存在
if os.IsNotExist(err) {
cfg = defaultMetaInfo(t)
if userMeta {
flushCfgFile(cfg, dstPath)
}
} else {
bytes, _ := io.ReadAll(f)
if err = json.Unmarshal(bytes, cfg); err != nil {
log.Fatalf("[ tto][ fatal]: read user meta file %s failed, reason: %s",
dstPath, err.Error())
}
}
} else {
cfg = defaultMetaInfo(t)
}
needMerge := false
// 赋值Render,如果未包含新的字段,则加入
for _, c := range t.Columns {
f, b := cfg.Fields[c.Name]
if !b {
f = smartField(c)
cfg.Fields[c.Name] = f
needMerge = true
}
c.Render = f.Render
}
// 去掉旧的字段
if len(t.Columns) != len(cfg.Fields) {
needMerge = true
arr := make([]string, 0)
for k := range cfg.Fields {
exist := false
for _, c := range t.Columns {
if c.Name == k {
exist = true
break
}
}
if !exist {
arr = append(arr, k)
}
}
for _, v := range arr {
delete(cfg.Fields, v)
}
}
if needMerge {
flushCfgFile(cfg, dstPath)
}
return t
}
func flushCfgFile(cfg *TableConfig, dstFile string) {
bytes, err := json.MarshalIndent(cfg, "", " ")
if err == nil {
err = util.BytesToFile(bytes, dstFile)
}
if err != nil {
log.Fatalf("[ tto][ fatal]: save user meta file %s failed, reason: %s",
dstFile, err.Error())
}
}
func defaultMetaInfo(t *Table) *TableConfig {
cfg := &TableConfig{
Struct: &TableMeta{},
Fields: make(map[string]*ColumnMeta, 0),
}
for _, c := range t.Columns {
f := smartField(c)
f.Title = c.Comment
cfg.Fields[c.Name] = f
}
return cfg
}
// 返回自动生成的字段
func smartField(c *Column) *ColumnMeta {
ele, options := smartElement(c)
fd := &ColumnMeta{
Render: &PropRenderOptions{
Visible: true,
Element: ele,
Options: options,
},
}
return fd
}
func smartElement(c *Column) (string, map[string]string) {
name := c.Name
len := c.Length
if strings.HasPrefix(name, "is_") {
return "checkbox", map[string]string{"是": "1", "否": "0"}
}
if strings.HasSuffix(name, "_time") {
return "time", map[string]string{}
}
if strings.HasSuffix(name, "state") ||
strings.HasSuffix(name, "status") ||
strings.HasSuffix(name, "enabled") {
return "radio", map[string]string{}
}
if strings.HasSuffix(name, "_date") {
return "date", map[string]string{}
}
if strings.HasPrefix(name, "upload_") ||
strings.HasPrefix(name, "file_") ||
strings.HasSuffix(name, "_image") ||
strings.HasSuffix(name, "_img") ||
strings.HasPrefix(name, "attachment") {
return "upload", map[string]string{}
}
if len >= 64 || len == 0 {
return "textarea", map[string]string{}
}
return "input", map[string]string{}
}