-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.go
80 lines (74 loc) · 1.36 KB
/
builtin.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
package applicator
import (
"reflect"
"strings"
)
func trim(i interface{}, _ string) (interface{}, error) {
t := func(v reflect.Value) string {
return strings.TrimSpace(v.String())
}
v := reflect.ValueOf(i)
var err error
switch v.Kind() {
case reflect.String:
i = t(v)
case reflect.Ptr:
if !v.IsNil() {
v = v.Elem()
if v.Kind() != reflect.String {
err = ErrUnsupported
} else {
str := t(v)
i = &str
}
}
default:
err = ErrUnsupported
}
return i, err
}
func lower(i interface{}, _ string) (interface{}, error) {
l := func(v reflect.Value) string {
return strings.ToLower(v.String())
}
v := reflect.ValueOf(i)
var err error
switch v.Kind() {
case reflect.String:
i = l(v)
case reflect.Ptr:
if !v.IsNil() {
v = v.Elem()
if v.Kind() != reflect.String {
err = ErrUnsupported
} else {
str := l(v)
i = &str
}
}
default:
err = ErrUnsupported
}
return i, err
}
func fillNil(i interface{}, _ string) (interface{}, error) {
v := reflect.ValueOf(i)
var err error
switch v.Kind() {
case reflect.Ptr:
if v.IsNil() {
i = reflect.New(v.Type().Elem()).Interface()
}
case reflect.Map:
if v.IsNil() {
i = reflect.MakeMap(v.Type()).Interface()
}
case reflect.Slice:
if v.IsNil() {
i = reflect.MakeSlice(v.Type(), 0, 1).Interface()
}
default:
err = ErrUnsupported
}
return i, err
}