-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribute.go
62 lines (48 loc) · 1.14 KB
/
attribute.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
package utils
import (
"regexp"
"strings"
)
type Attribute map[string]interface{}
func (attrs Attribute) Item(name string, value ...interface{}) (attr interface{}) {
attr = attrs[name]
if value != nil && len(value) > 0 {
attr = value[0]
if attr == nil {
delete(attrs, name)
} else {
attrs[name] = attr
}
}
return
}
func (attrs Attribute) Set(key string, value interface{}) interface{} {
attrs[key] = value
return value
}
func (attrs Attribute) Get(key string) interface{} {
if value, ok := attrs[key]; ok {
return value
}
return nil
}
func (attrs Attribute) GetString(key string) string {
return ToString(attrs.Get(key))
}
func (props Attribute) SetProperty(key string, value string, keepToken ...bool) string {
if len(keepToken) <= 0 || !keepToken[0] {
reg := regexp.MustCompile(`\{\s*([\w\._\-]+)\s*\}`)
value = reg.ReplaceAllStringFunc(value, func(s string) string {
skey := strings.Trim(s, "{")
skey = strings.Trim(skey, "}")
skey = strings.Trim(skey, " ")
if v := props.Get(skey); v != nil {
return ToString(v)
}
return s
})
}
// props.Set(key, value)
props[key] = value
return value
}