-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatcher-string.go
More file actions
72 lines (62 loc) · 1.65 KB
/
matcher-string.go
File metadata and controls
72 lines (62 loc) · 1.65 KB
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
package arp
import (
"errors"
"fmt"
"reflect"
)
type StringMatcher struct {
Value *string
FieldMatcherProps
}
func (m *StringMatcher) Parse(parentNode interface{}, node map[interface{}]interface{}) error {
if v, ok := node[TEST_KEY_MATCHES]; ok {
switch val := v.(type) {
case string:
m.Value = &val
default:
return errors.New(ObjectPrintf(fmt.Sprintf(MalformedDefinitionFmt, TEST_KEY_MATCHES, TYPE_STR), parentNode))
}
}
return m.ParseProps(node)
}
func (m *StringMatcher) Match(responseValue interface{}, datastore *DataStore) (bool, DataStore, error) {
store := NewDataStore()
typedResponseValue, ok := responseValue.(string)
if !ok {
m.ErrorStr = fmt.Sprintf(MismatchedMatcher, TYPE_STR, reflect.TypeOf(responseValue))
return false, store, nil
}
var status bool
var err error
if m.Value != nil {
resolved, err := (*datastore).ExpandVariable(*m.Value)
if err != nil {
return false, store, fmt.Errorf(BadVarMatcherFmt, *m.Value)
}
resolvedStr := varToString(resolved, *m.Value)
switch resolvedStr {
case Any:
status = true
case NotEmpty:
status = typedResponseValue != ""
if !status {
m.ErrorStr = fmt.Sprintf(NotEmptyErrFmt, typedResponseValue)
}
default:
status, _ = matchPattern(resolvedStr, []byte(typedResponseValue))
if !status {
m.ErrorStr = fmt.Sprintf(PatternErrFmt, typedResponseValue, resolvedStr)
}
}
}
if status {
m.ErrorStr = typedResponseValue
}
if status && m.DSName != "" {
err = store.PutVariable(m.DSName, responseValue)
}
return status, store, err
}
func (m *StringMatcher) SetError(error string) {
m.ErrorStr = fmt.Sprintf("%v (matching '%v')", error, *m.Value)
}