-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.go
118 lines (93 loc) · 2.78 KB
/
common.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
package malleable
import (
"fmt"
"strconv"
"strings"
)
// NOTE: created because github.com/alecthomas/participle/v2 parses default
// bool type as true if something is found.
type Boolean bool
func (b *Boolean) Capture(values []string) error {
out, err := strconv.ParseBool(values[0])
if err != nil {
return err
}
*b = (Boolean)(out)
return nil
}
// NOTE: default comma-separated string list parser and stringer, e.g.
// curl*,lynx*,wget*.
type CommaSeparatedList []string
func (l *CommaSeparatedList) Capture(values []string) error {
s := strings.Split(values[0], ",")
for i := range s {
s[i] = strings.TrimSpace(s[i])
}
*l = s
return nil
}
func (l CommaSeparatedList) String() string {
return strings.Join(([]string)(l), ",")
}
// NOTE: default space-separated string list parser and stringer, e.g.
// /jquery-3.3.1.min.js /jquery-1.3.3.7.min.js /someotherurl.
type SpaceSeparatedList []string
func (l *SpaceSeparatedList) Capture(values []string) error {
s := strings.Split(values[0], " ")
for i := range s {
s[i] = strings.TrimSpace(s[i])
}
*l = s
return nil
}
func (l SpaceSeparatedList) String() string {
return strings.Join(([]string)(l), " ")
}
// NOTE: key-value type with "header" prefix, used for headers parsing
// and (mostly) stringer, e.g. header "Accept-Encoding" "gzip, deflate";.
type Header struct {
Name string `parser:"@String"`
Value string `parser:"@String"`
}
func (h Header) String() string {
return fmt.Sprintf("header %s %s;\n", strconv.Quote(h.Name), strconv.Quote(h.Value))
}
// NOTE: key-value type with "parameter" prefix, used for parameters parsing
// and (mostly) stringer, e.g. parameter "param_name" "param_value";.
type Parameter struct {
Name string `parser:"@String"`
Value string `parser:"@String"`
}
func (p Parameter) String() string {
return fmt.Sprintf("parameter %s %s;\n", strconv.Quote(p.Name), strconv.Quote(p.Value))
}
// NOTE: parser and stringer for "string" function.
type String string
func (s String) String() string {
return fmt.Sprintf("string %v;\n", strconv.Quote((string)(s)))
}
// NOTE: parser and stringer for "stringw" function.
type StringW string
func (s StringW) String() string {
return fmt.Sprintf("stringw %v;\n", strconv.Quote((string)(s)))
}
// NOTE: parser and stringer for "data" function.
type Data string
func (s Data) String() string {
return fmt.Sprintf("data %v;\n", strconv.Quote((string)(s)))
}
// NOTE: parser and stringer for function sequences, e.g.
// http-get output, transforms in post-ex, etc.
type Function struct {
Func string `parser:"@Ident"`
Args []string `parser:"@String* \";\""`
}
func (f Function) String() string {
var p strings.Builder
p.WriteString(f.Func)
for _, n := range f.Args {
p.WriteString(fmt.Sprintf(" %s", strconv.Quote(n)))
}
p.WriteString(";\n")
return p.String()
}