2024-11-12 19:57:56 +00:00
|
|
|
package webutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-11-12 20:40:37 +00:00
|
|
|
"fmt"
|
2024-11-12 19:57:56 +00:00
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrUnsupportedType = errors.New("unsupported type")
|
|
|
|
|
|
|
|
type FormScanner struct {
|
|
|
|
form url.Values
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := val.(type) {
|
|
|
|
case *bool:
|
|
|
|
*v = s.form.Has(name)
|
|
|
|
default:
|
2024-11-15 18:27:39 +00:00
|
|
|
if err := scan(s.form.Get(name), v); err != nil {
|
|
|
|
s.err = fmt.Errorf("Error in field %s: %w", name, err)
|
|
|
|
}
|
2024-11-12 19:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FormScanner) Error() error {
|
|
|
|
return s.err
|
|
|
|
}
|