go/webutil/formscanner.go

117 lines
2.3 KiB
Go
Raw Normal View History

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"
"strconv"
)
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
}
2024-11-12 20:40:37 +00:00
setError := func(name string, err error) {
s.err = fmt.Errorf("Error in field %s: %w", name, err)
}
2024-11-12 19:57:56 +00:00
switch v := val.(type) {
case *bool:
*v = s.form.Has(name)
case *string:
*v = s.form.Get(name)
case *int:
if i, err := strconv.ParseInt(s.form.Get(name), 10, 64); err != nil {
2024-11-12 20:40:37 +00:00
setError(name, err)
2024-11-12 19:57:56 +00:00
} else {
*v = int(i)
}
case *int8:
if i, err := strconv.ParseInt(s.form.Get(name), 10, 8); err != nil {
2024-11-12 20:40:37 +00:00
setError(name, err)
2024-11-12 19:57:56 +00:00
} else {
*v = int8(i)
}
case *int16:
if i, err := strconv.ParseInt(s.form.Get(name), 10, 16); err != nil {
2024-11-12 20:40:37 +00:00
setError(name, err)
2024-11-12 19:57:56 +00:00
} else {
*v = int16(i)
}
case *int32:
if i, err := strconv.ParseInt(s.form.Get(name), 10, 32); err != nil {
2024-11-12 20:40:37 +00:00
setError(name, err)
2024-11-12 19:57:56 +00:00
} else {
*v = int32(i)
}
case *int64:
if i, err := strconv.ParseInt(s.form.Get(name), 10, 64); err != nil {
2024-11-12 20:40:37 +00:00
setError(name, err)
2024-11-12 19:57:56 +00:00
} else {
*v = int64(i)
}
case *uint:
if i, err := strconv.ParseUint(s.form.Get(name), 10, 64); err != nil {
2024-11-12 20:40:37 +00:00
setError(name, err)
2024-11-12 19:57:56 +00:00
} else {
*v = uint(i)
}
case *uint8:
if i, err := strconv.ParseUint(s.form.Get(name), 10, 8); err != nil {
2024-11-12 20:40:37 +00:00
setError(name, err)
2024-11-12 19:57:56 +00:00
} else {
*v = uint8(i)
}
case *uint16:
if i, err := strconv.ParseUint(s.form.Get(name), 10, 16); err != nil {
2024-11-12 20:40:37 +00:00
setError(name, err)
2024-11-12 19:57:56 +00:00
} else {
*v = uint16(i)
}
case *uint32:
if i, err := strconv.ParseUint(s.form.Get(name), 10, 32); err != nil {
2024-11-12 20:40:37 +00:00
setError(name, err)
2024-11-12 19:57:56 +00:00
} else {
*v = uint32(i)
}
case *uint64:
if i, err := strconv.ParseUint(s.form.Get(name), 10, 64); err != nil {
2024-11-12 20:40:37 +00:00
setError(name, err)
2024-11-12 19:57:56 +00:00
} else {
*v = uint64(i)
}
case *float32:
if f, err := strconv.ParseFloat(s.form.Get(name), 32); err != nil {
2024-11-12 20:40:37 +00:00
setError(name, err)
2024-11-12 19:57:56 +00:00
} else {
*v = float32(f)
}
case *float64:
if f, err := strconv.ParseFloat(s.form.Get(name), 64); err != nil {
2024-11-12 20:40:37 +00:00
setError(name, err)
2024-11-12 19:57:56 +00:00
} else {
*v = float64(f)
}
default:
2024-11-12 20:40:37 +00:00
setError(name, ErrUnsupportedType)
2024-11-12 19:57:56 +00:00
}
return s
}
func (s *FormScanner) Error() error {
return s.err
}