From 3194896b33500bab959147bac38ab4fb93dd55bb Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Wed, 14 Feb 2024 00:41:29 +0100 Subject: Cleanup --- form.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 form.go (limited to 'form.go') diff --git a/form.go b/form.go new file mode 100644 index 0000000..c5dffa0 --- /dev/null +++ b/form.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "log" + "net/http" + + "github.com/gorilla/schema" +) + +var schemaDecoder *schema.Decoder + +func init() { + schemaDecoder = schema.NewDecoder() +} + +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) + } +} -- cgit v1.2.3-70-g09d2