forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder_field.go.todo
195 lines (163 loc) · 4.27 KB
/
builder_field.go.todo
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
package uadmin
import (
"github.com/uadmin/uadmin/helper"
"strings"
)
type ReadOnly int
func (ReadOnly) Always() ReadOnly {
return 1
}
func (ReadOnly) New() ReadOnly {
return 2
}
func (ReadOnly) Edit() ReadOnly {
return 3
}
func (r *ReadOnly) GetTag() string {
if *r == r.Always() {
return "read_only"
}
if *r == r.Edit() {
return "read_only:true,edit"
}
if *r == r.New() {
return "read_only:true,new"
}
return ""
}
type BuilderField struct {
Model
Builder Builder `uadmin:"required;list_exclude"`
BuilderID uint
Name string `uadmin:"required;list_exclude"`
CodeName string `uadmin:"read_only"`
Type FieldType `uadmin:"required"`
Required bool
Help string `uadmin:"list_exclude"`
Search bool `uadmin:"list_exclude"`
ReadOnly ReadOnly
Max string `uadmin:"list_exclude"`
Min string `uadmin:"list_exclude"`
Hidden bool
ListExclude bool `uadmin:"list_exclude"`
Filter bool
CategoricalFilter bool `uadmin:"list_exclude"`
Encrypt bool `uadmin:"list_exclude"`
DefaultValue string `uadmin:"list_exclude"`
Pattern string `uadmin:"list_exclude"`
PatternMsg string `uadmin:"list_exclude"`
DisplayName string `uadmin:"list_exclude"`
}
func (b *BuilderField) GetCode() (code string, imports string) {
// Type map is a list field type to basic type
typeMap := map[FieldType]string{
b.Type.Boolean(): "bool",
b.Type.Code(): "string",
b.Type.DateTime(): "time.Time",
b.Type.DateTimePtr(): "*time.Time",
b.Type.Email(): "string",
b.Type.File(): "string",
b.Type.Float(): "float64",
b.Type.ForeignKey(): "{FKEY}",
b.Type.HTML(): "string",
b.Type.Image(): "string",
b.Type.Int(): "int",
b.Type.Link(): "string",
b.Type.M2M(): "{M2M}",
b.Type.Money(): "float64",
b.Type.Multilingual(): "string",
b.Type.Password(): "string",
b.Type.ProgressBar(): "float64",
b.Type.StaticList(): "{LIST}",
b.Type.String(): "string",
}
// required imports for field types
importMap := map[FieldType]string{
b.Type.DateTime(): "time",
b.Type.DateTimePtr(): "time",
}
// required tags for field types
tagMap := map[FieldType]string{
b.Type.Code(): "code",
b.Type.Email(): "email",
b.Type.File(): "file",
b.Type.HTML(): "html",
b.Type.Image(): "image",
b.Type.Link(): "link",
b.Type.Money(): "money",
b.Type.Multilingual(): "multilingual",
b.Type.Password(): "password",
b.Type.ProgressBar(): "progress_bar",
}
fName := b.CodeName
fType := typeMap[b.Type]
// check if there are any required imports
if val, ok := importMap[b.Type]; ok {
imports = val
}
// check if there are any required type tags
fTags := []string{}
if val, ok := tagMap[b.Type]; ok {
fTags = append(fTags, val)
}
// check for meta tags
if b.Required {
fTags = append(fTags, "required")
}
if b.Help != "" {
fTags = append(fTags, "help:"+b.Help)
}
if b.Search {
fTags = append(fTags, "search")
}
if b.ReadOnly != ReadOnly(0) {
fTags = append(fTags, b.ReadOnly.GetTag())
}
if b.Max != "" {
fTags = append(fTags, "max:"+b.Max)
}
if b.Min != "" {
fTags = append(fTags, "min:"+b.Min)
}
if b.Hidden {
fTags = append(fTags, "hidden")
}
if b.ListExclude {
fTags = append(fTags, "list_exclude")
}
if b.Filter {
fTags = append(fTags, "filter")
}
if b.CategoricalFilter {
fTags = append(fTags, "categorical_filter")
}
if b.Encrypt {
fTags = append(fTags, "encrypt")
}
if b.DefaultValue != "" {
fTags = append(fTags, "default_value:"+b.DefaultValue)
}
if b.Pattern != "" {
fTags = append(fTags, "pattern:"+b.Pattern)
}
if b.PatternMsg != "" {
fTags = append(fTags, "pattern_msg:"+b.PatternMsg)
}
if b.DisplayName != "" {
fTags = append(fTags, "display_name:"+b.DisplayName)
}
fTag := ""
if len(fTags) > 0 {
fTag += " `uadmin:\""
fTag += strings.Join(fTags, ";")
fTag += "\"`"
}
return fName + " " + fType + fTag, imports
}
func (b *BuilderField) Save() {
b.CodeName = helper.ToCamel(b.Name)
Save(b)
}
func (BuilderField) HideInDashboard() bool {
return true
}