-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.go
More file actions
270 lines (233 loc) · 6.29 KB
/
forms.go
File metadata and controls
270 lines (233 loc) · 6.29 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (c) CattleCloud LLC
// SPDX-License-Identifier: BSD-3-Clause
// Package forms provides a way to safely and conveniently extract html Form
// data using a definied schema.
package forms
import (
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/shoenig/go-conceal"
)
var (
ErrNoValue = errors.New("expected value to exist")
ErrMulitpleValues = errors.New("expected only one value to exist")
ErrFieldNotPresent = errors.New("requested field does not exist")
ErrParseFailure = errors.New("could not parse value")
)
// Parse uses the given Schema to parse the HTTP form values in the given HTTP
// Request. If the values of the form do not match the schema, or required values
// are missing, an error is returned.
func Parse(r *http.Request, schema Schema) error {
if err := r.ParseForm(); err != nil {
return err
}
return ParseValues(r.Form, schema)
}
// ParseValues uses the given Schema to parse the values in the given url.Values.
// If the values do not match the schema, or required values are missing, an
// error is returned.
func ParseValues(data url.Values, schema Schema) error {
for name, parser := range schema {
values := data[name]
if err := parser.Parse(values); err != nil {
return fmt.Errorf("%s: %w", ErrParseFailure.Error(), err)
}
}
return nil
}
// A Schema describes how a set of url.Values should be parsed.
// Typically these are coming from an http.Request.Form from inside an
// http.Handler responding to an inbound request.
type Schema map[string]Parser
// do we care about multi-value? we could provide parsers into slices
// automatically, for example
// A Parser implementation is capable of extracting a value from the value of
// an url.Values, which is a slice of string.
type Parser interface {
Parse([]string) error
}
// StringType represents any type compatible with the Go string built-in type,
// to be used as a destination for writing the value of an environment variable.
type StringType interface {
~string
}
type stringParser[T StringType] struct {
required bool
destination *T
}
func (p *stringParser[T]) Parse(values []string) error {
switch {
case len(values) > 1:
return ErrMulitpleValues
case len(values) == 0 && p.required:
return ErrNoValue
case len(values) == 0:
return nil
default:
*p.destination = T(values[0])
}
return nil
}
// String is used to extract a form data value into a Go string. If the value
// is not a string or is missing then an error is returned during parsing.
func String[T StringType](s *T) Parser {
return &stringParser[T]{
required: true,
destination: s,
}
}
// StringOr is used to extract a form data value into a Go string. If the value
// is missing, then the alt value is used instead.
func StringOr[T StringType](s *T, alt T) Parser {
*s = alt
return &stringParser[T]{
required: false,
destination: s,
}
}
// Secret is used to extract a form data value into a Go conceal.Text. If the
// value is missing then an error is returned during parsing.
func Secret(s **conceal.Text) Parser {
return &secretParser{
required: true,
destination: s,
}
}
type secretParser struct {
required bool
destination **conceal.Text
}
func (p *secretParser) Parse(values []string) error {
switch {
case len(values) > 1:
return ErrMulitpleValues
case len(values) == 0 && p.required:
return ErrNoValue
case len(values) == 0:
return nil
default:
text := conceal.New(values[0])
*p.destination = text
}
return nil
}
// IntType represents any type compatible with the Go integer built-in types,
// to be used as a destination for writing the value of a form value.
type IntType interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}
type intParser[T IntType] struct {
required bool
destination *T
}
func (p *intParser[T]) Parse(values []string) error {
switch {
case len(values) > 1:
return ErrMulitpleValues
case len(values) == 0 && p.required:
return ErrNoValue
case len(values) == 0:
return nil
}
i, err := strconv.Atoi(values[0])
if err != nil {
return err
}
*p.destination = T(i)
return nil
}
// Int is used to extract a form data value into a Go int. If the value is not
// an int or is missing then an error is returned during parsing.
func Int[T IntType](i *T) Parser {
return &intParser[T]{
required: true,
destination: i,
}
}
// IntOr is used to extract a form data value into a Go int. If the value is
// missing, then the alt value is used instead.
func IntOr[T IntType](i *T, alt T) Parser {
*i = alt
return &intParser[T]{
required: false,
destination: i,
}
}
type floatParser struct {
required bool
destination *float64
}
// Float is used to extract a form data value into a Go float64. If the value is
// not a float or is missing then an error is returned during parsing.
func Float(f *float64) Parser {
return &floatParser{
required: true,
destination: f,
}
}
// FloatOr is used to extract a form data value into a Go float64. If the value
// is missing, then the alt value is used instead.
func FloatOr(f *float64, alt float64) Parser {
*f = alt
return &floatParser{
required: false,
destination: f,
}
}
func (p *floatParser) Parse(values []string) error {
switch {
case len(values) > 1:
return ErrMulitpleValues
case len(values) == 0 && p.required:
return ErrNoValue
case len(values) == 0:
return nil
}
f, err := strconv.ParseFloat(values[0], 64)
if err != nil {
return err
}
*p.destination = f
return nil
}
type boolParser struct {
required bool
destination *bool
}
// Bool is used to extract a form data value into a Go bool. If the value is not
// a bool or is missing than an error is returned during parsing.
func Bool(b *bool) Parser {
return &boolParser{
required: true,
destination: b,
}
}
// BoolOr is used to extract a form data value into a Go bool. If the value is
// missing, then the alt value is used instead.
func BoolOr(b *bool, alt bool) Parser {
*b = alt
return &boolParser{
required: false,
destination: b,
}
}
func (p *boolParser) Parse(values []string) error {
switch {
case len(values) > 1:
return ErrMulitpleValues
case len(values) == 0 && p.required:
return ErrNoValue
case len(values) == 0:
return nil
}
b, err := strconv.ParseBool(values[0])
if err != nil {
return err
}
*p.destination = b
return nil
}