-
Notifications
You must be signed in to change notification settings - Fork 16
/
match.go
81 lines (70 loc) · 1.67 KB
/
match.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
package dynaml
import (
"regexp"
"strconv"
"github.com/mandelsoft/spiff/yaml"
)
func func_match(arguments []interface{}, binding Binding) (interface{}, EvaluationInfo, bool) {
info := DefaultInfo()
if len(arguments) < 2 || len(arguments) > 3 {
return info.Error("match takes two or three arguments")
}
pattern, ok := arguments[0].(string)
if !ok {
return info.Error("pattern string for argument one of function match required")
}
if arguments[1] == nil {
return false, info, true
}
occ := 0
if len(arguments) == 3 {
switch v := arguments[2].(type) {
case int64:
occ = int(v)
if occ == 0 {
return info.Error("repetition count may not be zero")
}
case bool:
if v {
occ = -1
} else {
occ = 1
}
default:
return info.Error("simple value for argument two of function match required")
}
}
elem := ""
switch v := arguments[1].(type) {
case string:
elem = v
case int64:
elem = strconv.FormatInt(v, 10)
case bool:
elem = strconv.FormatBool(v)
default:
return info.Error("simple value for argument two of function match required")
}
re, err := regexp.Compile(pattern)
if err != nil {
return info.Error("match: %s", err)
}
if occ == 0 {
list := re.FindStringSubmatch(elem)
return MakeStringList(list, info), info, true
} else {
list := re.FindAllStringSubmatch(elem, occ)
newList := make([]yaml.Node, len(list))
for i, v := range list {
newList[i] = NewNode(MakeStringList(v, info), info)
}
return newList, info, true
}
}
func MakeStringList(list []string, info EvaluationInfo) []yaml.Node {
newList := make([]yaml.Node, len(list))
for i, v := range list {
newList[i] = NewNode(v, info)
}
return newList
}