-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_argument_transforms.go
178 lines (141 loc) · 4.06 KB
/
step_argument_transforms.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
package elicit
import (
"fmt"
"os"
"reflect"
"regexp"
"regexp/syntax"
"strconv"
"strings"
)
type transform struct {
regex *regexp.Regexp
fn interface{}
}
type transformMap map[reflect.Type][]*transform
const (
txWarnPrefix = "warning: registered transform %q => [%v] "
txWarnNotFunc = txWarnPrefix + "must be a function.\n"
txWarnBadRegex = txWarnPrefix + "has an invalid regular expression: %s.\n"
txWarnParamType = txWarnPrefix + "must take one argument of type []string.\n"
txWarnReturn = txWarnPrefix + "must return precisely one value.\n"
)
func (tm transformMap) init() {
tm.register(`.*`, func(params []string) string {
return params[0]
})
tm.register(`-?\d+`, func(params []string) int {
i, err := strconv.Atoi(params[0])
if err != nil {
panic(fmt.Errorf("converting %q to int: %s", params[0], err))
}
return i
})
tm.register(`(?:.+,\s*)*.+`, func(params []string) []string {
ss := []string{}
for _, s := range strings.Split(params[0], ",") {
s = strings.TrimSpace(s)
ss = append(ss, s)
}
return ss
})
tm.register(`(?:-?\d+,\s*)*-?\d+`, func(params []string) []int {
si := []int{}
for _, s := range strings.Split(params[0], ",") {
s = strings.TrimSpace(s)
i, err := strconv.Atoi(s)
if err != nil {
panic(fmt.Errorf("converting %q to int: %s", s, err))
}
si = append(si, i)
}
return si
})
}
func (tm transformMap) register(pattern string, fn interface{}) {
if regex, typ, ok := tm.validate(pattern, fn); ok {
tm[typ] = append(tm[typ], &transform{regex: regex, fn: fn})
}
}
func (tm transformMap) validate(pattern string, transform interface{}) (*regexp.Regexp, reflect.Type, bool) {
fn := reflect.ValueOf(transform)
fnSig := fn.Type()
if fnSig.Kind() != reflect.Func {
fmt.Fprintf(os.Stderr, txWarnNotFunc, pattern, fnSig)
return nil, nil, false
}
cleanPattern := ensureCompleteMatch(pattern)
regex, err := regexp.Compile(cleanPattern)
if err != nil {
fmt.Fprintf(os.Stderr, txWarnBadRegex, pattern, fnSig, err.(*syntax.Error).Code)
return nil, nil, false
}
stringSliceType := reflect.TypeOf((*[]string)(nil)).Elem()
if fnSig.NumIn() != 1 || fnSig.In(0) != stringSliceType {
fmt.Fprintf(os.Stderr, txWarnParamType, pattern, fnSig)
return nil, nil, false
}
if fnSig.NumOut() != 1 {
fmt.Fprintf(os.Stderr, txWarnReturn, pattern, fnSig)
return nil, nil, false
}
typ := fnSig.Out(0)
return regex, typ, true
}
func (tm transformMap) convertParams(s *step, fn reflect.Value, stringParams []string) ([]reflect.Value, bool) {
if stringParams == nil {
return nil, false
}
if !tm.paramCountMatch(s, fn, stringParams) {
return nil, false
}
var c []reflect.Value
ok := true
if c, ok = tm.convertStringParams(fn, stringParams); !ok {
return nil, false
}
for _, tbl := range s.tables {
c = append(c, reflect.ValueOf(makeTable(tbl)))
}
for _, tb := range s.textBlocks {
c = append(c, reflect.ValueOf(tb))
}
return c, true
}
func (tm transformMap) paramCountMatch(s *step, fn reflect.Value, stringParams []string) bool {
paramCount, tableParamCount, textBlockParamCount := s.context.stepImpls.countStepImplParams(fn)
return len(stringParams) == paramCount && tableParamCount == len(s.tables) && textBlockParamCount == len(s.textBlocks)
}
func (tm transformMap) convertStringParams(fn reflect.Value, stringParams []string) ([]reflect.Value, bool) {
c := make([]reflect.Value, len(stringParams))
for i, param := range stringParams {
if i == 0 {
if fn.Type().In(0) != typeTestingT {
return nil, false
}
} else {
pt := fn.Type().In(i)
if t, ok := tm.convertParam(param, pt); ok {
c[i] = t
} else {
return nil, false
}
}
}
return c, true
}
func (tm transformMap) convertParam(param string, target reflect.Type) (reflect.Value, bool) {
for _, tx := range tm[target] {
params := tx.regex.FindStringSubmatch(param)
if params == nil {
continue
}
fn := reflect.ValueOf(tx.fn)
in := []reflect.Value{
reflect.ValueOf(params),
}
out := fn.Call(in)
return reflect.ValueOf(out[0].Interface()), true
}
return reflect.Value{}, false
}