-
Notifications
You must be signed in to change notification settings - Fork 2
/
general.go
189 lines (178 loc) · 4.8 KB
/
general.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
package functions
import (
"fmt"
"reflect"
"strings"
)
var FuncDefault = Function{
Description: "If <given> is empty it will return <defaultValue>.",
Parameters: Parameters{{
Name: "defaultValue",
}, {
Name: "given",
}},
}.MustWithFunc(func(defaultValue interface{}, given interface{}) interface{} {
if empty(given) {
return defaultValue
}
return given
})
var FuncEmpty = Function{
Description: "Checks the given <argument> if it is empty or not.",
Parameters: Parameters{{
Name: "argument",
}},
}.MustWithFunc(empty)
var FuncIsNotEmpty = Function{
Description: "Checks the given <argument> if it is not empty.",
Parameters: Parameters{{
Name: "argument",
}},
}.MustWithFunc(func(given interface{}) bool {
return !empty(given)
})
var FuncOptional = Function{
Description: "Asks the given <holder> if a property of given <name> exists and returns it. Otherwise the result is <nil>.",
Parameters: Parameters{{
Name: "name",
}, {
Name: "holder",
}},
}.MustWithFunc(func(name string, holder interface{}) (interface{}, error) {
v := reflect.ValueOf(holder)
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.IsNil() {
return nil, nil
}
for v.Kind() == reflect.Interface {
v = v.Elem()
}
t := v.Type()
switch v.Kind() {
case reflect.Struct:
if field, ok := t.FieldByName(name); ok {
fieldValue := v.FieldByIndex(field.Index)
return fieldValue.Interface(), nil
}
return nil, nil
case reflect.Map:
value := v.MapIndex(reflect.ValueOf(name))
if !value.IsValid() {
return nil, nil
}
return value.Interface(), nil
}
return nil, fmt.Errorf("cannot get value for '%s' because cannot handle values of type %v", name, t)
})
var FuncContains = Function{
Description: `Checks if the given <input> contains the given <search> element. If the input is a string this will reflect a part of the string, if slice an element of the slice, if map/object a key of the map/object.`,
Parameters: Parameters{{
Name: "search",
}, {
Name: "input",
}},
}.MustWithFunc(func(search interface{}, input interface{}) (bool, error) {
v := reflect.ValueOf(input)
if v.Kind() == reflect.Ptr && v.IsNil() {
return false, nil
}
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
for v.Kind() == reflect.Interface {
v = v.Elem()
}
t := v.Type()
switch v.Kind() {
case reflect.String:
return strings.Contains(v.String(), fmt.Sprint(search)), nil
case reflect.Struct:
key := fmt.Sprint(search)
_, ok := t.FieldByName(key)
return ok, nil
case reflect.Map:
key := reflect.ValueOf(search)
value := v.MapIndex(key)
return value.IsValid(), nil
case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ {
candidate := v.Index(i)
if candidate.IsValid() && reflect.DeepEqual(candidate.Interface(), search) {
return true, nil
}
}
return false, nil
default:
return false, fmt.Errorf("currently contains only supports either strings, maps, structs, arrays or slices but got: %v", reflect.TypeOf(input))
}
})
var FuncMap = Function{
Description: `Creates from the given key to value pair a new map.`,
Parameters: Parameters{{
Name: "input",
}},
}.MustWithFunc(func(in ...interface{}) (map[interface{}]interface{}, error) {
if len(in)%2 != 0 {
return nil, fmt.Errorf("expect always a key to value pair, this means the amount of parameters needs to be dividable by two, but got: %d", len(in))
}
result := make(map[interface{}]interface{}, len(in)/2)
for i := 0; i < len(in); i += 2 {
key, value := in[i], in[i+1]
result[key] = value
}
return result, nil
})
var FuncSlice = Function{
Description: `Creates from the given values a new slice.`,
Parameters: Parameters{{
Name: "input",
}},
}.MustWithFunc(func(in ...interface{}) ([]interface{}, error) {
return in, nil
})
var FuncsGeneral = Functions{
"optional": FuncOptional,
"empty": FuncEmpty,
"isNotEmpty": FuncIsNotEmpty,
"default": FuncDefault,
"contains": FuncContains,
"map": FuncMap,
"slice": FuncSlice,
"array": FuncSlice,
}
var CategoryGeneral = Category{
Functions: FuncsGeneral,
}
func empty(given interface{}) bool {
if given == nil {
return true
}
v := reflect.ValueOf(given)
if !v.IsValid() {
return true
}
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
// Basically adapted from text/template.isTrue
switch v.Kind() {
default:
return v.IsNil()
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
return v.Len() == 0
case reflect.Bool:
return v.Bool() == false
case reflect.Complex64, reflect.Complex128:
return v.Complex() == 0
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Struct:
return false
}
}