element.go
5.1 KB
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
package elements
type LableInterface interface {
SetLable(label string)
GetLable() (label string)
SetLabelAttributes(labelattrs map[string]interface{})
GetLabelAttributes() (labelattrs map[string]interface{})
SetLabelOptions(labelOptions map[string]interface{})
GetLabelOptions() (labelOptions map[string]interface{})
ClearLabelOptions()
RemoveLabelOptions(keys []string)
SetLabelOption(key string, val interface{})
GetLabelOption(key string) (val interface{})
RemoveLabelOption(key string)
HasLabelOption(key string) bool
}
type ElementInterface interface {
SetName(name string)
GetName() (name string)
SetOptions(options map[string]interface{})
SetOption(key string, val interface{})
GetOptions() (options map[string]interface{})
GetOption(key string) (val interface{})
SetAttribute(key string, val interface{})
GetAttribute(key string) (val interface{})
RemoveAttribute(key string)
HasAttribute(key string) bool
SetAttributes(attributes map[string]interface{})
GetAttributes() (attributes map[string]interface{})
RemoveAttributes(keys []string)
ClearAttributes()
SetValue(val interface{})
GetValue() (val interface{})
SetMessages(msg string)
GetMessages() (msg string)
SetValidator(v ValidatorInterface)
Valid(val interface{}) bool
GetMessage() string
LableInterface
}
type ValidatorInterface interface {
IsValid(val interface{}) bool
GetMessages() (msg string)
}
type Element struct {
options map[string]interface{}
attributes map[string]interface{}
labelAttributes map[string]interface{}
labelOptions map[string]interface{}
value interface{}
lable string
messages string
validator ValidatorInterface
}
func (e *Element) SetName(name string) {
e.SetAttribute("name", name)
}
func (e *Element) GetName() (name string) {
return e.GetAttribute("name").(string)
}
func (e *Element) SetOptions(options map[string]interface{}) {
if val, ok := options["label"]; ok {
e.SetLable(val.(string))
}
if val, ok := options["label_attributes"]; ok {
e.SetLabelAttributes(val.(map[string]interface{}))
}
if val, ok := options["label_options"]; ok {
e.SetLabelOptions(val.(map[string]interface{}))
}
e.options = options
}
func (e *Element) SetOption(key string, val interface{}) {
e.options[key] = val
}
func (e *Element) GetOptions() (options map[string]interface{}) {
return e.options
}
func (e *Element) GetOption(key string) (val interface{}) {
if val, ok := e.options[key]; ok {
return val
}
return nil
}
func (e *Element) SetAttribute(key string, val interface{}) {
e.attributes[key] = val
}
func (e *Element) GetAttribute(key string) (val interface{}) {
if val, ok := e.attributes[key]; ok {
return val
}
return nil
}
func (e *Element) RemoveAttribute(key string) {
delete(e.attributes, key)
}
func (e *Element) HasAttribute(key string) bool {
if _, ok := e.attributes[key]; ok {
return true
}
return false
}
func (e *Element) SetAttributes(attributes map[string]interface{}) {
for key, val := range attributes {
e.SetAttribute(key, val)
}
}
func (e *Element) GetAttributes() (attributes map[string]interface{}) {
return e.attributes
}
func (e *Element) RemoveAttributes(keys []string) {
for _, key := range keys {
e.RemoveAttribute(key)
}
}
func (e *Element) ClearAttributes() {
e.attributes = make(map[string]interface{})
}
func (e *Element) SetValue(val interface{}) {
e.value = val
}
func (e *Element) GetValue() (val interface{}) {
return e.value
}
func (e *Element) SetLable(label string) {
e.lable = label
}
func (e *Element) GetLable() (label string) {
return e.lable
}
func (e *Element) SetLabelAttributes(labelattrs map[string]interface{}) {
e.labelAttributes = labelattrs
}
func (e *Element) GetLabelAttributes() (labelattrs map[string]interface{}) {
return e.labelAttributes
}
func (e *Element) SetLabelOptions(labelOptions map[string]interface{}) {
for key, val := range labelOptions {
e.SetLabelOption(key, val)
}
}
func (e *Element) GetLabelOptions() (labelOptions map[string]interface{}) {
return e.labelOptions
}
func (e *Element) ClearLabelOptions() {
e.labelOptions = make(map[string]interface{})
}
func (e *Element) RemoveLabelOptions(keys []string) {
for _, key := range keys {
e.RemoveLabelOption(key)
}
}
func (e *Element) SetLabelOption(key string, val interface{}) {
e.labelOptions[key] = val
}
func (e *Element) GetLabelOption(key string) (val interface{}) {
if v, ok := e.labelOptions[key]; ok {
return v
}
return nil
}
func (e *Element) RemoveLabelOption(key string) {
delete(e.labelOptions, key)
}
func (e *Element) HasLabelOption(key string) bool {
if _, ok := e.labelOptions[key]; ok {
return true
}
return false
}
func (e *Element) SetMessages(msg string) {
e.messages = msg
}
func (e *Element) GetMessages() (msg string) {
return e.messages
}
func (e *Element) SetValidator(v ValidatorInterface) {
e.validator = v
}
func (e *Element) Valid(val interface{}) bool {
if e.validator == nil {
return true
}
if e.validator.IsValid(val) {
return true
}
return false
}
func (e *Element) GetMessage() string {
if e.validator == nil {
return ""
}
return e.validator.GetMessages()
}