blob: db0d0975f08c23cf11fcb32a4af02eda6e124526 (
plain)
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
|
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/schema"
)
var schemaDecoder *schema.Decoder
func init() {
schemaDecoder = schema.NewDecoder()
schemaDecoder.IgnoreUnknownKeys(true)
}
type fieldError struct {
Field string
Issue string
}
func (fe fieldError) Error() string {
return fmt.Sprintf("%s: %v", fe.Field, fe.Issue)
}
func (fe fieldError) FieldError() (field, err string) {
return fe.Field, fe.Issue
}
func parseForm[T any](r *http.Request, data *T) {
if err := r.ParseForm(); err != nil {
log.Panic("Parsing form: ", err)
}
if err := schemaDecoder.Decode(data, r.PostForm); err != nil {
log.Panic("Decoding form: ", err)
}
if withCsrf, ok := any(data).(WithCsrf); ok {
withCsrf.SetCsrfField(r)
}
}
|