From aa1da8078a0df285afa4f63a8e2742226c9a1093 Mon Sep 17 00:00:00 2001 From: some Date: Tue, 8 Oct 2024 18:04:57 -0400 Subject: [PATCH] Parse errors instead of panics --- common.go | 6 ++++-- header.go | 4 ++-- values.go | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/common.go b/common.go index fca9607..edbd905 100644 --- a/common.go +++ b/common.go @@ -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 } diff --git a/header.go b/header.go index f175110..b1e6556 100644 --- a/header.go +++ b/header.go @@ -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) { diff --git a/values.go b/values.go index 050efa5..d0f9d8d 100644 --- a/values.go +++ b/values.go @@ -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") }