forked from hootrhino/rulex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rulexui.go
297 lines (269 loc) · 6.57 KB
/
rulexui.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package core
import (
"fmt"
"reflect"
"rulex/typex"
"strings"
"github.com/iancoleman/strcase"
"github.com/ngaut/log"
)
//
// 空串
//
const EMPTY_STRING string = ""
/*
*
* 这里是一些前端渲染支持
*
*/
type viewType string
const (
_NUMBER viewType = "el-input-number"
_TEXT viewType = "el-input-text"
_INLINE viewType = "el-input-inline"
_SELECT viewType = "el-input-select"
_FILE viewType = "el-upload"
_MAP viewType = "el-map"
)
type view struct {
Order int `json:"order"` // 界面顺序
Type viewType `json:"type"` // 组件类型
Name string `json:"name"` // 表单字段名
Info string `json:"info"` // 表单提示
Label string `json:"label"` // 界面显示标签
Value interface{} `json:"value"` // 字段的值
Required bool `json:"required"` // 是否必填
Hidden bool `json:"hidden"` // 是否隐藏
Placeholder string `json:"placeholder"` // 占位文本
}
type numberInputView struct {
view
}
func NewNumberInputView() numberInputView {
v := numberInputView{}
v.Type = _NUMBER
return v
}
type textInputView struct {
view
}
func NewTextInputView() textInputView {
v := textInputView{}
v.Hidden = false
v.Required = true
v.Type = _TEXT
return v
}
type inLineView struct {
view
Children []view `json:"children"`
}
type fileView struct {
view
}
func NewFileView() fileView {
v := fileView{}
v.Type = _FILE
return v
}
func NewInlineView() inLineView {
v := inLineView{}
v.Type = _INLINE
return v
}
type selectView struct {
view
SelectOptions []selectOption `json:"selectOptions"`
}
type selectOption struct {
Label string `json:"label"` // 下拉选择框 UI
Value string `json:"value"` // 下拉选择框 值
}
func NewSelectView() selectView {
v := selectView{}
v.Type = _SELECT
return v
}
/*
*
* 渲染 select 组件, Select 在Config上用tag 表示
* 其格式如下: options:"label1,value1|label2,value2 ...."
*
*/
func renderSelect(s string) ([]selectOption, error) {
options := []selectOption{}
var err error
splits := strings.Split(s, "|")
if len(splits)%2 != 0 {
err = fmt.Errorf("expression error:%v", s)
goto END
}
for _, kv := range splits {
kv := strings.Split(kv, ",")
if len(splits) != 2 {
err = fmt.Errorf("syntax error:%v", kv)
goto END
}
options = append(options, selectOption{Label: kv[0], Value: kv[1]})
}
END:
return options, err
}
/*
*
* 渲染UI界面
*
*/
func GenInConfig(Type typex.InEndType, helpTip string, config interface{}) *typex.XConfig {
c, err := renderConfig(Type.String(), helpTip, config)
if err != nil {
log.Error(err)
return nil
}
return c
}
func GenOutConfig(Type typex.TargetType, helpTip string, config interface{}) *typex.XConfig {
c, err := renderConfig(Type.String(), helpTip, config)
if err != nil {
log.Error(err)
return nil
}
return c
}
func RenderInConfig(Type typex.InEndType, helpTip string, config interface{}) (*typex.XConfig, error) {
return renderConfig(Type.String(), helpTip, config)
}
func RenderOutConfig(Type typex.TargetType, helpTip string, config interface{}) (*typex.XConfig, error) {
return renderConfig(Type.String(), helpTip, config)
}
func renderConfig(Type string, helpTip string, config interface{}) (*typex.XConfig, error) {
var err error
typee := reflect.TypeOf(config)
views := make([]interface{}, 0)
for i := 0; i < typee.NumField(); i++ {
filedName := strcase.ToLowerCamel(typee.Field(i).Name)
filedType := typee.Field(i).Type.String()
tag := typee.Field(i).Tag
//
Info := tag.Get("info")
if Info == EMPTY_STRING {
Info = "***" // 当看到这个文本的时候就需要去增加这个Tag字段了
}
Label := tag.Get("label")
if Label == EMPTY_STRING {
Label = "***" // 当看到这个文本的时候就需要去增加这个Tag字段了
}
Required := tag.Get("required")
Hidden := tag.Get("hidden")
Placeholder := tag.Get("placeholder")
//
xcfg := typex.XConfig{}
// 数字输入
if (filedType == "int") ||
(filedType == "int64") ||
(filedType == "int32") ||
(filedType == "float32") {
xcfg.Type = string(_NUMBER)
nv := NewNumberInputView()
//
nv.Order = i
nv.Name = filedName
nv.Label = Label
nv.Info = Info
nv.Placeholder = Placeholder
if Required == "false" {
nv.Required = false
}
if Hidden == "true" {
nv.Hidden = true
}
views = append(views, nv)
}
// 文本输入
if filedType == "string" || filedType == "*string" {
// 文件框
fileTag := tag.Get("file") // file:"uploadfile"
if fileTag != EMPTY_STRING {
xcfg.Type = string(_FILE)
fv := NewFileView()
//
fv.Order = i
fv.Name = fileTag
fv.Label = Label
fv.Info = Info
fv.Placeholder = Placeholder
if Required == "false" {
fv.Required = false
}
if Hidden == "true" {
fv.Hidden = true
}
views = append(views, fv)
} else {
xcfg.Type = string(_TEXT)
tv := NewTextInputView()
//
tv.Order = i
tv.Name = filedName
tv.Label = Label
tv.Info = Info
tv.Placeholder = Placeholder
if Required == "false" {
tv.Required = false
}
if Hidden == "true" {
tv.Hidden = true
}
views = append(views, tv)
}
}
// 动态数组
if (filedType == "[]string") ||
(filedType == "[]int") ||
(filedType == "[]int32") ||
(filedType == "[]int64") {
xcfg.Type = string(_INLINE)
iv := NewInlineView()
//
iv.Order = i
iv.Name = filedName
iv.Label = Label
iv.Info = Info
iv.Placeholder = Placeholder
if Required == "false" {
iv.Required = false
}
if Hidden == "true" {
iv.Hidden = true
}
views = append(views, iv)
}
optionsTag := tag.Get("options")
// 下拉框输入
if optionsTag != EMPTY_STRING {
xcfg.Type = string(_SELECT)
sv := NewSelectView()
//
sv.Order = i
sv.Name = filedName
sv.Label = Label
sv.Info = Info
sv.Placeholder = Placeholder
if Required == "false" {
sv.Required = false
}
if Hidden == "true" {
sv.Hidden = true
}
selectOptions, err1 := renderSelect(optionsTag)
if err1 != nil {
err = err1
goto END
}
sv.SelectOptions = selectOptions
views = append(views, sv)
}
}
END:
return &typex.XConfig{Type: Type, Views: views, HelpTip: helpTip}, err
}