Parse errors instead of panics

This commit is contained in:
some 2024-10-08 18:04:57 -04:00
parent 1e12efce13
commit aa1da8078a
Signed by: some
GPG Key ID: 65D0589220B9BFC8
3 changed files with 8 additions and 6 deletions

View File

@ -7,13 +7,14 @@ import (
type GenericValues map[string][]string type GenericValues map[string][]string
func (gv GenericValues) Parse(data any, tag string) { func (gv GenericValues) Parse(data any, tag string) (err error) {
if gv == nil { if gv == nil {
gv = make(GenericValues) gv = make(GenericValues)
} }
d := reflect.ValueOf(data) d := reflect.ValueOf(data)
if d.Kind() != reflect.Struct { if d.Kind() != reflect.Struct {
panic(fmt.Errorf("expected struct input for data")) err = fmt.Errorf("expected struct input for data")
return
} }
for i := 0; i < d.NumField(); i++ { for i := 0; i < d.NumField(); i++ {
key, ok := d.Type().Field(i).Tag.Lookup(tag) key, ok := d.Type().Field(i).Tag.Lookup(tag)
@ -38,4 +39,5 @@ func (gv GenericValues) Parse(data any, tag string) {
} }
gv[key] = v gv[key] = v
} }
return
} }

View File

@ -6,8 +6,8 @@ import (
type Header map[string][]string type Header map[string][]string
func (h Header) Parse(data any) { func (h Header) Parse(data any) error {
GenericValues(h).Parse(data, "header") return GenericValues(h).Parse(data, "header")
} }
func (h Header) Get(key string) (value string) { func (h Header) Get(key string) (value string) {

View File

@ -2,6 +2,6 @@ package router
type Values map[string][]string type Values map[string][]string
func (v Values) Parse(data any) { func (v Values) Parse(data any) error {
GenericValues(v).Parse(data, "form") return GenericValues(v).Parse(data, "form")
} }