go/webutil/scanner.go

86 lines
1.6 KiB
Go
Raw Permalink Normal View History

2024-11-15 18:27:39 +00:00
package webutil
import "strconv"
func scan(raw string, val any) error {
switch v := val.(type) {
case *string:
*v = raw
case *int:
if i, err := strconv.ParseInt(raw, 10, 64); err != nil {
return err
} else {
*v = int(i)
}
case *int8:
if i, err := strconv.ParseInt(raw, 10, 8); err != nil {
return err
} else {
*v = int8(i)
}
case *int16:
if i, err := strconv.ParseInt(raw, 10, 16); err != nil {
return err
} else {
*v = int16(i)
}
case *int32:
if i, err := strconv.ParseInt(raw, 10, 32); err != nil {
return err
} else {
*v = int32(i)
}
case *int64:
if i, err := strconv.ParseInt(raw, 10, 64); err != nil {
return err
} else {
*v = int64(i)
}
case *uint:
if i, err := strconv.ParseUint(raw, 10, 64); err != nil {
return err
} else {
*v = uint(i)
}
case *uint8:
if i, err := strconv.ParseUint(raw, 10, 8); err != nil {
return err
} else {
*v = uint8(i)
}
case *uint16:
if i, err := strconv.ParseUint(raw, 10, 16); err != nil {
return err
} else {
*v = uint16(i)
}
case *uint32:
if i, err := strconv.ParseUint(raw, 10, 32); err != nil {
return err
} else {
*v = uint32(i)
}
case *uint64:
if i, err := strconv.ParseUint(raw, 10, 64); err != nil {
return err
} else {
*v = uint64(i)
}
case *float32:
if f, err := strconv.ParseFloat(raw, 32); err != nil {
return err
} else {
*v = float32(f)
}
case *float64:
if f, err := strconv.ParseFloat(raw, 64); err != nil {
return err
} else {
*v = float64(f)
}
default:
return ErrUnsupportedType
}
return nil
}