diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2024-02-14 00:41:29 +0100 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2024-02-14 00:41:29 +0100 |
commit | 3194896b33500bab959147bac38ab4fb93dd55bb (patch) | |
tree | 420de5cc68b8f5bd57c871ce0ca27a880eadf7f8 /form | |
parent | 53124974a4f6fd7c16579b8fc6b40ad620737af9 (diff) | |
download | gosten-3194896b33500bab959147bac38ab4fb93dd55bb.tar.gz gosten-3194896b33500bab959147bac38ab4fb93dd55bb.tar.bz2 gosten-3194896b33500bab959147bac38ab4fb93dd55bb.zip |
Cleanup
Diffstat (limited to '')
-rw-r--r-- | form.go | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -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) + } +} |