summaryrefslogtreecommitdiff
path: root/form/decode.go
diff options
context:
space:
mode:
Diffstat (limited to 'form/decode.go')
-rw-r--r--form/decode.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/form/decode.go b/form/decode.go
new file mode 100644
index 0000000..7ebdfc2
--- /dev/null
+++ b/form/decode.go
@@ -0,0 +1,29 @@
+package form
+
+import (
+ "gosten/csrf"
+ "log"
+ "net/http"
+
+ "github.com/gorilla/schema"
+)
+
+var schemaDecoder *schema.Decoder
+
+func init() {
+ schemaDecoder = schema.NewDecoder()
+ schemaDecoder.IgnoreUnknownKeys(true)
+}
+
+func Parse[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).(csrf.Enabled); ok {
+ withCsrf.SetCsrfField(r)
+ }
+}