-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path05-expected-response-match-parser.go
119 lines (93 loc) · 2.38 KB
/
05-expected-response-match-parser.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
package main
import (
"errors"
"fmt"
"regexp"
"strings"
)
func expectedResponseMatchParser(context *context) {
context.log("05 expected-response-match-parser")
expectedResponse := context.SpecTriplet.ExpectedResponse
for _, line := range expectedResponse.allLines() {
parts := strings.Split(line.Text, regexpIdentifier)
count := len(parts)
if count == 1 {
continue
}
if (count-4)%3 != 0 {
errorHandler(
context,
fmt.Errorf(
"regexp must be formed %soptional-capture-name%sregexp%s",
regexpIdentifier,
regexpIdentifier,
regexpIdentifier,
),
)
return
}
var re *regexp.Regexp
var err error
if parts[0] != "" {
re, err = regexp.Compile(regexp.QuoteMeta(parts[0]))
if errorHandler(context, err) {
return
}
line.RegexpNames = append(line.RegexpNames, ":prefix")
line.Regexps = append(line.Regexps, re)
}
for i := 1; i < count-1; i += 3 {
captureName := parts[i]
if captureName == ":prefix" || captureName == ":postfix" {
errorHandler(
context,
errors.New("capture names cannot be :prefix or :postfix"),
)
return
}
reString := "("
switch parts[i+1] {
case ":date":
reString +=
"(Mon|Tue|Wed|Thu|Fri|Sat|Sun), " +
"(0\\d|1\\d|2\\d|3[01]) " +
"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) " +
"2\\d{3} " +
"(0\\d|1\\d|2[0-3]):" +
"(0\\d|1\\d|2\\d|3\\d|4\\d|5\\d):" +
"(0\\d|1\\d|2\\d|3\\d|4\\d|5\\d) " +
"(A|M|N|Y|Z|UT|GMT|[A-Z]{3}|[+-](0\\d|1[012]))"
case ":b62:22":
reString += "[0-9A-Za-z]{22}"
case ":iso8601:µs:z":
reString += "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}[.]\\d{6}Z"
case ":uuid":
reString +=
"[[:xdigit:]]{8}-" +
"[[:xdigit:]]{4}-" +
"[[:xdigit:]]{4}-" +
"[[:xdigit:]]{4}-" +
"[[:xdigit:]]{12}"
default:
reString += parts[i+1]
}
reString += ")"
re, err = regexp.Compile(reString)
if errorHandler(context, err) {
return
}
line.RegexpNames = append(line.RegexpNames, parts[i])
line.Regexps = append(line.Regexps, re)
postfix := parts[i+2]
if postfix != "" {
re, err = regexp.Compile(regexp.QuoteMeta(postfix))
if errorHandler(context, err) {
return
}
line.RegexpNames = append(line.RegexpNames, ":postfix")
line.Regexps = append(line.Regexps, re)
}
}
}
expectedResponseSubstituter(context)
}