This commit is contained in:
jdl 2024-11-12 21:40:37 +01:00
parent c8582acf01
commit 1691e1c477

View File

@ -2,6 +2,7 @@ package webutil
import ( import (
"errors" "errors"
"fmt"
"net/url" "net/url"
"strconv" "strconv"
) )
@ -22,6 +23,10 @@ func (s *FormScanner) Scan(name string, val any) *FormScanner {
return s return s
} }
setError := func(name string, err error) {
s.err = fmt.Errorf("Error in field %s: %w", name, err)
}
switch v := val.(type) { switch v := val.(type) {
case *bool: case *bool:
*v = s.form.Has(name) *v = s.form.Has(name)
@ -29,78 +34,78 @@ func (s *FormScanner) Scan(name string, val any) *FormScanner {
*v = s.form.Get(name) *v = s.form.Get(name)
case *int: case *int:
if i, err := strconv.ParseInt(s.form.Get(name), 10, 64); err != nil { if i, err := strconv.ParseInt(s.form.Get(name), 10, 64); err != nil {
s.err = err setError(name, err)
} else { } else {
*v = int(i) *v = int(i)
} }
case *int8: case *int8:
if i, err := strconv.ParseInt(s.form.Get(name), 10, 8); err != nil { if i, err := strconv.ParseInt(s.form.Get(name), 10, 8); err != nil {
s.err = err setError(name, err)
} else { } else {
*v = int8(i) *v = int8(i)
} }
case *int16: case *int16:
if i, err := strconv.ParseInt(s.form.Get(name), 10, 16); err != nil { if i, err := strconv.ParseInt(s.form.Get(name), 10, 16); err != nil {
s.err = err setError(name, err)
} else { } else {
*v = int16(i) *v = int16(i)
} }
case *int32: case *int32:
if i, err := strconv.ParseInt(s.form.Get(name), 10, 32); err != nil { if i, err := strconv.ParseInt(s.form.Get(name), 10, 32); err != nil {
s.err = err setError(name, err)
} else { } else {
*v = int32(i) *v = int32(i)
} }
case *int64: case *int64:
if i, err := strconv.ParseInt(s.form.Get(name), 10, 64); err != nil { if i, err := strconv.ParseInt(s.form.Get(name), 10, 64); err != nil {
s.err = err setError(name, err)
} else { } else {
*v = int64(i) *v = int64(i)
} }
case *uint: case *uint:
if i, err := strconv.ParseUint(s.form.Get(name), 10, 64); err != nil { if i, err := strconv.ParseUint(s.form.Get(name), 10, 64); err != nil {
s.err = err setError(name, err)
} else { } else {
*v = uint(i) *v = uint(i)
} }
case *uint8: case *uint8:
if i, err := strconv.ParseUint(s.form.Get(name), 10, 8); err != nil { if i, err := strconv.ParseUint(s.form.Get(name), 10, 8); err != nil {
s.err = err setError(name, err)
} else { } else {
*v = uint8(i) *v = uint8(i)
} }
case *uint16: case *uint16:
if i, err := strconv.ParseUint(s.form.Get(name), 10, 16); err != nil { if i, err := strconv.ParseUint(s.form.Get(name), 10, 16); err != nil {
s.err = err setError(name, err)
} else { } else {
*v = uint16(i) *v = uint16(i)
} }
case *uint32: case *uint32:
if i, err := strconv.ParseUint(s.form.Get(name), 10, 32); err != nil { if i, err := strconv.ParseUint(s.form.Get(name), 10, 32); err != nil {
s.err = err setError(name, err)
} else { } else {
*v = uint32(i) *v = uint32(i)
} }
case *uint64: case *uint64:
if i, err := strconv.ParseUint(s.form.Get(name), 10, 64); err != nil { if i, err := strconv.ParseUint(s.form.Get(name), 10, 64); err != nil {
s.err = err setError(name, err)
} else { } else {
*v = uint64(i) *v = uint64(i)
} }
case *float32: case *float32:
if f, err := strconv.ParseFloat(s.form.Get(name), 32); err != nil { if f, err := strconv.ParseFloat(s.form.Get(name), 32); err != nil {
s.err = err setError(name, err)
} else { } else {
*v = float32(f) *v = float32(f)
} }
case *float64: case *float64:
if f, err := strconv.ParseFloat(s.form.Get(name), 64); err != nil { if f, err := strconv.ParseFloat(s.form.Get(name), 64); err != nil {
s.err = err setError(name, err)
} else { } else {
*v = float64(f) *v = float64(f)
} }
default: default:
s.err = ErrUnsupportedType setError(name, ErrUnsupportedType)
} }
return s return s