-
Notifications
You must be signed in to change notification settings - Fork 16
/
compact.go
44 lines (36 loc) · 839 Bytes
/
compact.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
package dynaml
import (
"github.com/mandelsoft/spiff/yaml"
)
func init() {
RegisterFunction("compact", func_compact)
}
func func_compact(arguments []interface{}, binding Binding) (interface{}, EvaluationInfo, bool) {
info := DefaultInfo()
if len(arguments) != 1 {
return info.Error("compact takes exactly 1 argument")
}
list, ok := arguments[0].([]yaml.Node)
if !ok {
return info.Error("invalid type for function compact")
}
var newList []yaml.Node
for _, v := range list {
found := true
if v != nil && v.Value() != nil {
switch elem := v.Value().(type) {
case string:
found = len(elem) > 0
case int64:
case map[string]yaml.Node:
found = len(elem) > 0
case []yaml.Node:
found = len(elem) > 0
}
if found {
newList = append(newList, v)
}
}
}
return newList, info, true
}