-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.go
67 lines (53 loc) · 963 Bytes
/
field.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
package main
type Field struct {
Name string
Value string
Data map[string]interface{}
}
func NewField() *Field {
return &Field{}
}
func NewFieldWithMap(m map[string]interface{}) *Field {
f := NewField()
for k, v := range m {
f.Set(k, v)
}
return f
}
func (f *Field) SetName(name string) {
f.Name = name
}
func (f *Field) GetName() string {
return f.Name
}
func (f *Field) SetValue(v string) {
f.Value = v
}
func (f *Field) Set(k string, v interface{}) {
if k == "" {
return
}
switch k {
case "value":
f.Value = v.(string)
case "name":
f.Name = v.(string)
default:
if f.Data == nil {
f.Data = make(map[string]interface{})
}
f.Data[k] = v
}
}
func (f *Field) IsEmpty() bool {
return f.Name == "" && f.Value == "" && f.Data == nil
}
func (f *Field) ToMap() map[string]interface{} {
res := f.Data
if res == nil {
res = make(map[string]interface{})
}
res["name"] = f.Name
res["value"] = f.Value
return res
}