package webutil import ( "errors" "fmt" "net/url" ) var ErrUnsupportedType = errors.New("unsupported type") type FormScanFunc func(name string, val any) (bool, error) type FormScanner struct { form url.Values err error scanFunc FormScanFunc } func NewFormScanner(form url.Values) *FormScanner { return &FormScanner{form: form} } func (s *FormScanner) Scan(name string, val any) *FormScanner { if s.err != nil { return s } if s.scanFunc != nil { ok, err := s.scanFunc(name, val) s.err = err if ok || err != nil { return s } } switch v := val.(type) { case *bool: *v = s.form.Has(name) default: if err := scan(s.form.Get(name), v); err != nil { s.err = fmt.Errorf("Error in field %s: %w", name, err) } } return s } func (s *FormScanner) Error() error { return s.err }