-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmeta.go
270 lines (252 loc) · 6.45 KB
/
meta.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package kuu
import (
"path"
"reflect"
"strings"
"sync"
"time"
)
var (
metadataMap = make(map[string]*Metadata)
metadataList = make([]*Metadata, 0)
modelStructsMap sync.Map
)
// Metadata
type Metadata struct {
ModCode string
Name string
NativeName string
DisplayName string
LocaleKey string
FullName string
Fields []MetadataField
RestDesc *RestDesc `json:"-"`
reflectType reflect.Type
SubDocIDNames []string `json:"-" gorm:"-"`
UIDNames []string `json:"-" gorm:"-"`
OrgIDNames []string `json:"-" gorm:"-"`
TagSettings map[string]string `json:"-" gorm:"-"`
}
// MetadataField
type MetadataField struct {
Code string
Name string
NativeName string
DBType string
IsBland bool
IsPrimaryKey bool
LocaleKey string
Kind string
Type string
Enum string
IsRef bool
IsPassword bool
IsArray bool
Value interface{} `json:"-" gorm:"-"`
Tag reflect.StructTag `json:"-" gorm:"-"`
}
// NewValue
func (m *Metadata) NewValue() interface{} {
return reflect.New(m.reflectType).Interface()
}
// OmitPassword
func (m *Metadata) OmitPassword(data interface{}) interface{} {
if m == nil {
return data
}
var passwordKeys []string
for _, field := range m.Fields {
if field.IsPassword {
passwordKeys = append(passwordKeys, field.Code)
}
}
if len(passwordKeys) == 0 {
return data
}
execOmit := func(indirectValue reflect.Value) {
var val interface{}
if indirectValue.CanAddr() {
val = indirectValue.Addr().Interface()
} else {
val = indirectValue.Interface()
}
scope := DB().NewScope(val)
for _, key := range passwordKeys {
if _, ok := scope.FieldByName(key); ok {
if err := scope.SetColumn(key, ""); err != nil {
ERROR(err)
}
}
}
}
if indirectValue := indirectValue(data); indirectValue.Kind() == reflect.Slice {
for i := 0; i < indirectValue.Len(); i++ {
execOmit(indirectValue.Index(i))
}
} else {
execOmit(indirectValue)
}
return data
}
func parseMetadata(value interface{}) (m *Metadata) {
reflectType := reflect.ValueOf(value).Type()
for reflectType.Kind() == reflect.Slice || reflectType.Kind() == reflect.Ptr {
reflectType = reflectType.Elem()
}
// Scope value need to be a struct
if reflectType.Kind() != reflect.Struct {
return
}
hashKey := reflectType
if value, ok := modelStructsMap.Load(hashKey); ok && value != nil {
return value.(*Metadata)
}
reflectTypeName := reflectType.Name()
m = &Metadata{
Name: reflectTypeName,
FullName: path.Join(reflectType.PkgPath(), reflectTypeName),
reflectType: reflectType,
}
modelScope := DB().NewScope(value)
m.NativeName = modelScope.TableName()
for i := 0; i < reflectType.NumField(); i++ {
fieldStruct := reflectType.Field(i)
displayName := fieldStruct.Tag.Get("displayName")
if m.DisplayName == "" && displayName != "" {
m.DisplayName = displayName
if v := fieldStruct.Tag.Get("locale"); m.LocaleKey == "" && v != "" {
m.LocaleKey = v
}
}
indirectType := fieldStruct.Type
for indirectType.Kind() == reflect.Ptr {
indirectType = indirectType.Elem()
}
fieldValue := reflect.New(indirectType).Interface()
field := MetadataField{
Code: fieldStruct.Name,
Kind: fieldStruct.Type.String(),
Enum: fieldStruct.Tag.Get("enum"),
LocaleKey: fieldStruct.Tag.Get("locale"),
Tag: fieldStruct.Tag,
}
if modelField, hasModelField := modelScope.FieldByName(field.Code); hasModelField {
field.NativeName = modelField.DBName
field.IsBland = modelField.IsBlank
field.IsPrimaryKey = modelField.IsPrimaryKey
if modelField.IsNormal {
field.DBType = DB().Dialect().DataTypeOf(modelField.StructField)
}
}
switch field.Kind {
case "bool", "null.Bool":
field.Type = "boolean"
case "int", "int8", "int16", "int32", "int64",
"uint", "uint8", "uint16", "uint32", "uint64", "null.Int":
field.Type = "integer"
case "float32", "float64":
field.Type = "number"
case "slice", "struct", "ptr":
field.Type = "object"
case "null.String", "string":
field.Type = "string"
case "gorm.Model", "kuu.Model", "kuu.ModelExOrg":
temp := parseMetadata(fieldValue)
for _, metadataField := range temp.Fields {
m.Fields = append(m.Fields, metadataField)
}
default:
field.Type = field.Kind
}
if _, ok := fieldValue.(*time.Time); ok {
field.Type = "string"
}
ref := fieldStruct.Tag.Get("ref")
if ref != "" {
fieldMeta := Meta(ref)
if fieldMeta != nil {
field.Type = fieldMeta.Name
field.IsRef = true
field.Value = fieldValue
if indirectType.Kind() == reflect.Slice {
field.IsArray = true
}
}
}
tagSettings := parseTagSetting(fieldStruct.Tag, "kuu")
if len(tagSettings) > 0 {
m.TagSettings = tagSettings
if _, exists := tagSettings["PASSWORD"]; exists {
field.IsPassword = true
}
if v, exists := tagSettings["UIDS"]; exists {
m.UIDNames = strings.Split(v, ",")
}
if v, exists := tagSettings["SUB_IDS"]; exists {
m.SubDocIDNames = strings.Split(v, ",")
}
if v, exists := tagSettings["ORG_IDS"]; exists {
m.OrgIDNames = strings.Split(v, ",")
}
}
name := fieldStruct.Tag.Get("name")
if name != "" {
field.Name = name
}
if field.DBType != "" {
m.Fields = append(m.Fields, field)
}
}
modelStructsMap.Store(hashKey, m)
metadataMap[m.Name] = m
metadataList = append(metadataList, m)
return
}
// Meta
func Meta(valueOrName interface{}) (m *Metadata) {
if v, ok := valueOrName.(string); ok {
return metadataMap[v]
} else {
return parseMetadata(valueOrName)
}
}
// Metalist
func Metalist() []*Metadata {
return metadataList
}
// RegisterMeta
func RegisterMeta() {
tx := DB().Begin()
tx = tx.Unscoped().Where(&Metadata{}).Delete(&Metadata{})
for _, meta := range metadataList {
tx = tx.Create(meta)
}
if errs := tx.GetErrors(); len(errs) > 0 {
ERROR(errs)
if err := tx.Rollback(); err != nil {
ERROR(err)
}
} else {
if err := tx.Commit().Error; err != nil {
ERROR(err)
}
}
}
func parseTagSetting(tags reflect.StructTag, tagKey string) map[string]string {
setting := map[string]string{}
str := tags.Get(tagKey)
split := strings.Split(str, ";")
for _, value := range split {
if value == "" {
continue
}
v := strings.Split(value, ":")
k := strings.TrimSpace(strings.ToUpper(v[0]))
if len(v) >= 2 {
setting[k] = strings.Join(v[1:], ":")
} else {
setting[k] = k
}
}
return setting
}