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
func (gv GenericValues) Parse(data any, tag string) {
func (gv GenericValues) Parse(data any, tag string) (err error) {
if gv == nil {
gv = make(GenericValues)
}
d := reflect.ValueOf(data)
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++ {
key, ok := d.Type().Field(i).Tag.Lookup(tag)
@ -38,4 +39,5 @@ func (gv GenericValues) Parse(data any, tag string) {
}
gv[key] = v
}
return
}

View File

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

View File

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