forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
269 lines (259 loc) · 6.98 KB
/
schema.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
package uadmin
import (
"encoding/json"
"fmt"
"reflect"
"runtime"
"strings"
"time"
)
// ModelSchema for a form
type ModelSchema struct {
Name string // Name of the Model e.g. OrderItem
DisplayName string // Display Name of the model e.g. Order Items
ModelName string // URL e.g. orderitem
TableName string // DB table name e.g. order_items
Category string
ModelID uint
Inlines []*ModelSchema
InlinesData []listData
Fields []F
IncludeFormJS []string
IncludeListJS []string
FormModifier func(*ModelSchema, interface{}, *User) `json:"-"`
ListModifier func(*ModelSchema, *User) (string, []interface{}) `json:"-"`
FormTheme string
ListTheme string
}
// FieldByName returns a field from a ModelSchema by name or nil if
// it doesn't exist
func (s ModelSchema) FieldByName(a string) *F {
for i := range s.Fields {
if strings.EqualFold(s.Fields[i].Name, a) {
return &s.Fields[i]
}
}
return &F{}
}
// FieldByName returns a field from a ModelSchema by name or nil if
// it doesn't exist
func (s ModelSchema) FieldByColumnName(a string) *F {
for i := range s.Fields {
if strings.EqualFold(s.Fields[i].ColumnName, a) {
return &s.Fields[i]
}
}
return nil
}
// GetFormTheme returns the theme for this model or the
// global theme if there is no assigned theme for the model
func (s *ModelSchema) GetFormTheme() string {
if s.FormTheme == "" {
return Theme
}
return s.FormTheme
}
// GetListTheme returns the theme for this model or the
// global theme if there is no assigned theme for the model
func (s *ModelSchema) GetListTheme() string {
if s.ListTheme == "" {
return Theme
}
return s.ListTheme
}
// MarshalJSON customizes json export for models schema
func (s ModelSchema) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Name string // Name of the Model
DisplayName string // Display Name of the model
ModelName string // URL
TableName string
ModelID uint
Inlines []*ModelSchema
InlinesData []listData
Fields []F
IncludeFormJS []string
IncludeListJS []string
FormModifier *string
ListModifier *string
FormTheme string
ListTheme string
}{
Name: s.Name,
DisplayName: s.DisplayName,
ModelName: s.ModelName,
TableName: s.TableName,
ModelID: s.ModelID,
Inlines: s.Inlines,
InlinesData: s.InlinesData,
Fields: s.Fields,
IncludeFormJS: s.IncludeFormJS,
IncludeListJS: s.IncludeListJS,
FormModifier: func() *string {
if s.FormModifier == nil {
return nil
}
v := runtime.FuncForPC(reflect.ValueOf(s.FormModifier).Pointer()).Name()
return &v
}(),
ListModifier: func() *string {
if s.ListModifier == nil {
return nil
}
v := runtime.FuncForPC(reflect.ValueOf(s.ListModifier).Pointer()).Name()
return &v
}(),
FormTheme: s.FormTheme,
ListTheme: s.ListTheme,
})
}
// F is a field
type F struct {
Name string
DisplayName string
ColumnName string
Type string
TypeName string
Value interface{}
Help string
Max interface{}
Min interface{}
Format string
DefaultValue string
Required bool
Pattern string
PatternMsg string
Hidden bool
ReadOnly string
Searchable bool
Filter bool
ListDisplay bool
FormDisplay bool
CategoricalFilter bool
Translations []translation
Choices []Choice
IsMethod bool
ErrMsg string
ProgressBar map[float64]string `json:"-"`
LimitChoicesTo func(interface{}, *User) []Choice `json:"-"`
UploadTo string
Encrypt bool
Approval bool
NewValue interface{}
OldValue interface{}
ChangedBy string
ChangeDate *time.Time
ApprovalAction ApprovalAction
ApprovalDate *time.Time
ApprovalBy string
ApprovalID uint
WebCam bool
Stringer bool
Deprecated bool
}
// MarshalJSON customizes F json export
func (f F) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Name string
DisplayName string
ColumnName string
Type string
TypeName string
Value interface{}
Help string
Max interface{}
Min interface{}
Format string
DefaultValue string
Required bool
Pattern string
PatternMsg string
Hidden bool
ReadOnly string
Searchable bool
Filter bool
ListDisplay bool
FormDisplay bool
CategoricalFilter bool
Translations []translation
Choices []Choice
IsMethod bool
ErrMsg string
ProgressBar map[string]string
LimitChoicesTo *string
UploadTo string
Encrypt bool
Approval bool
NewValue interface{}
OldValue interface{}
ChangedBy string
ChangeDate *time.Time
ApprovalAction ApprovalAction
ApprovalDate *time.Time
ApprovalBy string
ApprovalID uint
WebCam bool
Stringer bool
Deprecated bool
}{
Name: f.Name,
DisplayName: f.DisplayName,
ColumnName: f.ColumnName,
Type: f.Type,
TypeName: f.TypeName,
Value: f.Value,
Help: f.Help,
Max: f.Max,
Min: f.Min,
Format: f.Format,
DefaultValue: f.DefaultValue,
Required: f.Required,
Pattern: f.Pattern,
PatternMsg: f.PatternMsg,
Hidden: f.Hidden,
ReadOnly: f.ReadOnly,
Searchable: f.Searchable,
Filter: f.Filter,
ListDisplay: f.ListDisplay,
FormDisplay: f.FormDisplay,
CategoricalFilter: f.CategoricalFilter,
Translations: f.Translations,
Choices: f.Choices,
IsMethod: f.IsMethod,
ErrMsg: f.ErrMsg,
ProgressBar: func() map[string]string {
tempMap := map[string]string{}
for k, v := range f.ProgressBar {
tempMap[fmt.Sprint(k)] = v
}
return tempMap
}(),
LimitChoicesTo: func() *string {
if f.LimitChoicesTo == nil {
return nil
}
v := runtime.FuncForPC(reflect.ValueOf(f.LimitChoicesTo).Pointer()).Name()
return &v
}(),
UploadTo: f.UploadTo,
Encrypt: f.Encrypt,
Approval: f.Approval,
NewValue: f.NewValue,
OldValue: f.OldValue,
ChangedBy: f.ChangedBy,
ChangeDate: f.ChangeDate,
ApprovalAction: f.ApprovalAction,
ApprovalDate: f.ApprovalDate,
ApprovalBy: f.ApprovalBy,
ApprovalID: f.ApprovalID,
WebCam: f.WebCam,
Stringer: f.Stringer,
Deprecated: f.Deprecated,
})
}
// Choice is a struct for list choices
type Choice struct {
V string
K uint
Selected bool
}