-
Notifications
You must be signed in to change notification settings - Fork 2
/
input.go
240 lines (197 loc) · 3.8 KB
/
input.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package gosubmit
import (
"fmt"
"regexp"
"strconv"
"time"
)
const (
AutoFillEmail = "test@example.com"
AutoFillURL = "https://www.example.com"
ISO8601Date = "2006-01-02"
AutoFillDate = ISO8601Date
)
var AutoFillFile = []byte{0xd, 0xe, 0xa, 0xd, 0xb, 0xe, 0xe, 0xf}
type Input interface {
Name() string
Type() string
Value() string
Values() []string
Options() []string
Fill(val string) (value string, ok bool)
Required() bool
Multiple() bool
Multipart() bool
AutoFill() []string
}
type anyInput struct {
name string
inputType string
values []string
required bool
}
func (i anyInput) Name() string {
return i.name
}
func (i anyInput) Type() string {
return i.inputType
}
func (i anyInput) Value() string {
if len(i.values) == 0 {
return ""
}
return i.values[0]
}
func (i anyInput) AutoFill() (values []string) {
return []string{fmt.Sprintf("%s-%s", i.Name(), "autofill")}
}
func (i anyInput) Values() []string {
return i.values
}
func (i anyInput) Required() bool {
return i.required
}
func (i anyInput) Multiple() bool {
return false
}
func (i anyInput) Multipart() bool {
return false
}
func (i anyInput) Options() (values []string) {
return
}
type FileInput struct {
anyInput
}
func (f FileInput) Fill(val string) (value string, ok bool) {
return "", false
}
func (f FileInput) Multipart() bool {
return true
}
func (f FileInput) AutoFill() (values []string) {
values = append(values, string(AutoFillFile))
return
}
type TextInput struct {
anyInput
pattern *regexp.Regexp
minLength int
maxLength int
}
func (i TextInput) Fill(val string) (value string, ok bool) {
length := len(val)
ok = i.minLength == 0 && i.maxLength == 0 || length >= i.minLength && length <= i.maxLength
value = val
if i.pattern == nil {
return
}
ok = i.pattern.MatchString(value)
return
}
func (i TextInput) AutoFill() (value []string) {
length := 10
if i.pattern != nil {
return
}
if i.minLength > 0 {
length = i.minLength
return []string{randomString(length)}
}
if i.maxLength > 0 {
length = i.maxLength
return []string{randomString(length)}
}
return i.anyInput.AutoFill()
}
type HiddenInput struct {
anyInput
}
func (i HiddenInput) Fill(val string) (value string, ok bool) {
return i.Value(), false
}
type inputWithOptions struct {
anyInput
options []string
multiple bool
}
func (i inputWithOptions) Options() []string {
return i.options
}
func (i inputWithOptions) Multiple() bool {
return i.multiple
}
func (i inputWithOptions) Fill(val string) (value string, ok bool) {
ok = false
for _, opt := range i.options {
if opt == val {
value = val
ok = true
}
}
return
}
func (i inputWithOptions) AutoFill() (values []string) {
for _, opt := range i.options {
values = append(values, opt)
if !i.multiple {
return
}
}
return
}
type EmailInput struct {
TextInput
}
func (i EmailInput) AutoFill() []string {
return []string{AutoFillEmail}
}
type URLInput struct {
TextInput
}
func (i URLInput) AutoFill() []string {
return []string{AutoFillURL}
}
type NumberInput struct {
anyInput
min int
max int
}
func (i NumberInput) AutoFill() []string {
return []string{itoa(i.min)}
}
func (i NumberInput) Fill(val string) (value string, ok bool) {
ok = false
intValue, err := strconv.Atoi(val)
if err != nil {
return
}
ok = i.min == 0 && i.max == 0 || intValue >= i.min && intValue <= i.max
value = itoa(intValue)
return
}
type DateInput struct {
anyInput
}
func (i DateInput) Fill(val string) (value string, ok bool) {
value = val
_, err := time.Parse(ISO8601Date, val)
ok = err == nil
return
}
func (i DateInput) AutoFill() []string {
return []string{AutoFillDate}
}
type Checkbox struct {
inputWithOptions
}
type Radio struct {
inputWithOptions
}
type Select struct {
inputWithOptions
}
type Button struct {
Name string
Value string
}