| 1 | package gosubmit |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "regexp" |
| 6 | "strconv" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | const ( |
| 11 | AutoFillEmail = "[email protected]" |
| 12 | AutoFillURL = "https://www.example.com" |
| 13 | ISO8601Date = "2006-01-02" |
| 14 | AutoFillDate = ISO8601Date |
| 15 | ) |
| 16 | |
| 17 | var AutoFillFile = []byte{0xd, 0xe, 0xa, 0xd, 0xb, 0xe, 0xe, 0xf} |
| 18 | |
| 19 | type Input interface { |
| 20 | Name() string |
| 21 | Type() string |
| 22 | Value() string |
| 23 | Values() []string |
| 24 | Options() []string |
| 25 | Fill(val string) (value string, ok bool) |
| 26 | Required() bool |
| 27 | Multiple() bool |
| 28 | Multipart() bool |
| 29 | AutoFill() []string |
| 30 | } |
| 31 | |
| 32 | type anyInput struct { |
| 33 | name string |
| 34 | inputType string |
| 35 | values []string |
| 36 | required bool |
| 37 | } |
| 38 | |
| 39 | func (i anyInput) Name() string { |
| 40 | return i.name |
| 41 | } |
| 42 | |
| 43 | func (i anyInput) Type() string { |
| 44 | return i.inputType |
| 45 | } |
| 46 | |
| 47 | func (i anyInput) Value() string { |
| 48 | if len(i.values) == 0 { |
| 49 | return "" |
| 50 | } |
| 51 | return i.values[0] |
| 52 | } |
| 53 | |
| 54 | func (i anyInput) AutoFill() (values []string) { |
| 55 | return []string{fmt.Sprintf("%s-%s", i.Name(), "autofill")} |
| 56 | } |
| 57 | |
| 58 | func (i anyInput) Values() []string { |
| 59 | return i.values |
| 60 | } |
| 61 | |
| 62 | func (i anyInput) Required() bool { |
| 63 | return i.required |
| 64 | } |
| 65 | |
| 66 | func (i anyInput) Multiple() bool { |
| 67 | return false |
| 68 | } |
| 69 | |
| 70 | func (i anyInput) Multipart() bool { |
| 71 | return false |
| 72 | } |
| 73 | |
| 74 | func (i anyInput) Options() (values []string) { |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | type FileInput struct { |
| 79 | anyInput |
| 80 | } |
| 81 | |
| 82 | func (f FileInput) Fill(val string) (value string, ok bool) { |
| 83 | return "", false |
| 84 | } |
| 85 | |
| 86 | func (f FileInput) Multipart() bool { |
| 87 | return true |
| 88 | } |
| 89 | |
| 90 | func (f FileInput) AutoFill() (values []string) { |
| 91 | values = append(values, string(AutoFillFile)) |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | type TextInput struct { |
| 96 | anyInput |
| 97 | pattern *regexp.Regexp |
| 98 | minLength int |
| 99 | maxLength int |
| 100 | } |
| 101 | |
| 102 | func (i TextInput) Fill(val string) (value string, ok bool) { |
| 103 | length := len(val) |
| 104 | ok = i.minLength == 0 && i.maxLength == 0 || length >= i.minLength && length <= i.maxLength |
| 105 | value = val |
| 106 | if i.pattern == nil { |
| 107 | return |
| 108 | } |
| 109 | ok = i.pattern.MatchString(value) |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | func (i TextInput) AutoFill() (value []string) { |
| 114 | length := 10 |
| 115 | if i.pattern != nil { |
| 116 | return |
| 117 | } |
| 118 | if i.minLength > 0 { |
| 119 | length = i.minLength |
| 120 | return []string{randomString(length)} |
| 121 | } |
| 122 | if i.maxLength > 0 { |
| 123 | length = i.maxLength |
| 124 | return []string{randomString(length)} |
| 125 | } |
| 126 | return i.anyInput.AutoFill() |
| 127 | } |
| 128 | |
| 129 | type HiddenInput struct { |
| 130 | anyInput |
| 131 | } |
| 132 | |
| 133 | func (i HiddenInput) Fill(val string) (value string, ok bool) { |
| 134 | return i.Value(), false |
| 135 | } |
| 136 | |
| 137 | type inputWithOptions struct { |
| 138 | anyInput |
| 139 | options []string |
| 140 | multiple bool |
| 141 | } |
| 142 | |
| 143 | func (i inputWithOptions) Options() []string { |
| 144 | return i.options |
| 145 | } |
| 146 | |
| 147 | func (i inputWithOptions) Multiple() bool { |
| 148 | return i.multiple |
| 149 | } |
| 150 | |
| 151 | func (i inputWithOptions) Fill(val string) (value string, ok bool) { |
| 152 | ok = false |
| 153 | for _, opt := range i.options { |
| 154 | if opt == val { |
| 155 | value = val |
| 156 | ok = true |
| 157 | } |
| 158 | } |
| 159 | return |
| 160 | } |
| 161 | |
| 162 | func (i inputWithOptions) AutoFill() (values []string) { |
| 163 | for _, opt := range i.options { |
| 164 | values = append(values, opt) |
| 165 | if !i.multiple { |
| 166 | return |
| 167 | } |
| 168 | } |
| 169 | return |
| 170 | } |
| 171 | |
| 172 | type EmailInput struct { |
| 173 | TextInput |
| 174 | } |
| 175 | |
| 176 | func (i EmailInput) AutoFill() []string { |
| 177 | return []string{AutoFillEmail} |
| 178 | } |
| 179 | |
| 180 | type URLInput struct { |
| 181 | TextInput |
| 182 | } |
| 183 | |
| 184 | func (i URLInput) AutoFill() []string { |
| 185 | return []string{AutoFillURL} |
| 186 | } |
| 187 | |
| 188 | type NumberInput struct { |
| 189 | anyInput |
| 190 | min int |
| 191 | max int |
| 192 | } |
| 193 | |
| 194 | func (i NumberInput) AutoFill() []string { |
| 195 | return []string{itoa(i.min)} |
| 196 | } |
| 197 | |
| 198 | func (i NumberInput) Fill(val string) (value string, ok bool) { |
| 199 | ok = false |
| 200 | intValue, err := strconv.Atoi(val) |
| 201 | if err != nil { |
| 202 | return |
| 203 | } |
| 204 | |
| 205 | ok = i.min == 0 && i.max == 0 || intValue >= i.min && intValue <= i.max |
| 206 | value = itoa(intValue) |
| 207 | return |
| 208 | } |
| 209 | |
| 210 | type DateInput struct { |
| 211 | anyInput |
| 212 | } |
| 213 | |
| 214 | func (i DateInput) Fill(val string) (value string, ok bool) { |
| 215 | value = val |
| 216 | _, err := time.Parse(ISO8601Date, val) |
| 217 | ok = err == nil |
| 218 | return |
| 219 | } |
| 220 | |
| 221 | func (i DateInput) AutoFill() []string { |
| 222 | return []string{AutoFillDate} |
| 223 | } |
| 224 | |
| 225 | type Checkbox struct { |
| 226 | inputWithOptions |
| 227 | } |
| 228 | |
| 229 | type Radio struct { |
| 230 | inputWithOptions |
| 231 | } |
| 232 | |
| 233 | type Select struct { |
| 234 | inputWithOptions |
| 235 | } |
| 236 | |
| 237 | type Button struct { |
| 238 | Name string |
| 239 | Value string |
| 240 | } |