// Scan implements the database/sql Scanner interface. It will receive a value
// from an SQL database and assign it to i, so long as the provided data is of
// type nil, uint8, string, or another integer or float type that doesn't
// overflow uint8. All other types will result in an error.
Scan implements the database/sql Scanner interface. It will receive a value
from an SQL database and assign it to i, so long as the provided data is of
type nil, uint8, string, or another integer or float type that doesn't
overflow uint8. All other types will result in an error.
// Scan implements the database / sql Scanner interface . It will receive a value // from an SQL database and assign it to i so long as the provided data is of // type nil uint8 string or another integer or float type that doesn ' t // overflow uint8 . All other types will result in an error .
func (i *Uint8) Scan(src interface{}) error {
	if i == nil {
		return fmt.Errorf("null.Uint8: Scan called on nil pointer")
	}
	if src == nil {
		i.Uint8 = 0
		i.Valid = false
		return nil
	}
	switch val := src.(type) {
	case uint8:
		i.Uint8 = val
		i.Valid = true
		return nil
	case uint, uint16, uint32, uint64:
		v := reflect.ValueOf(src)
		vi := v.Uint()
		if vi > math.MaxUint8 {
			return fmt.Errorf("null.Uint8: failed to scan type %T (%v): overflow", src, src)
		}
		i.Uint8 = uint8(vi)
		i.Valid = true
		return nil
	case int, int8, int16, int32, int64:
		v := reflect.ValueOf(src)
		vi := v.Int()
		if vi > math.MaxUint8 {
			return fmt.Errorf("null.Uint8: failed to scan type %T (%v): overflow", src, src)
		} else if vi < 0 {
			return fmt.Errorf("null.Uint8: failed to scan type %T (%v): negative value", src, src)
		}
		i.Uint8 = uint8(vi)
		i.Valid = true
		return nil
	case string:
		parsedUint, err := strconv.ParseUint(val, 10, 0)
		if err != nil {
			return fmt.Errorf("null.Uint8: failed to scan type %T (%v): %v", src, src, err)
		}
		i.Uint8 = uint8(parsedUint)
		i.Valid = true
		return nil
	case float64:
		s := strconv.FormatFloat(val, 'g', -1, 64)
		parsedUint, err := strconv.ParseInt(s, 10, 8)
		if err != nil {
			return fmt.Errorf("null.Uint8: failed to convert driver.Value type %T (%v): %v", src, s, err)
		}
		i.Uint8 = uint8(parsedUint)
		i.Valid = true
		return nil
	case float32:
		s := strconv.FormatFloat(float64(val), 'g', -1, 32)
		parsedUint, err := strconv.ParseInt(s, 10, 8)
		if err != nil {
			return fmt.Errorf("null.Uint8: failed to convert driver.Value type %T (%v): %v", src, s, err)
		}
		i.Uint8 = uint8(parsedUint)
		i.Valid = true
		return nil
	default:
		return fmt.Errorf("null.Uint8: cannot scan type %T (%v)", src, src)
	}
}
func ( i * Uint8 ) Scan ( src interface { } ) error { if i == nil { return fmt . Errorf ( " " ) } if src == nil { i . Uint8 = 0 i . Valid = false return nil } switch val := src . ( type ) { case uint8 : i . Uint8 = val i . Valid = true return nil case uint , uint16 , uint32 , uint64 : v := reflect . ValueOf ( src ) vi := v . Uint ( ) if vi > math . MaxUint8 { return fmt . Errorf ( " " , src , src ) } i . Uint8 = uint8 ( vi ) i . Valid = true return nil case int , int8 , int16 , int32 , int64 : v := reflect . ValueOf ( src ) vi := v . Int ( ) if vi > math . MaxUint8 { return fmt . Errorf ( " " , src , src ) } else if vi < 0 { return fmt . Errorf ( " " , src , src ) } i . Uint8 = uint8 ( vi ) i . Valid = true return nil case string : parsedUint , err := strconv . ParseUint ( val , 10 , 0 ) if err != nil { return fmt . Errorf ( " " , src , src , err ) } i . Uint8 = uint8 ( parsedUint ) i . Valid = true return nil case float64 : s := strconv . FormatFloat ( val , 'g' , - 1 , 64 ) parsedUint , err := strconv . ParseInt ( s , 10 , 8 ) if err != nil { return fmt . Errorf ( " " , src , s , err ) } i . Uint8 = uint8 ( parsedUint ) i . Valid = true return nil case float32 : s := strconv . FormatFloat ( float64 ( val ) , 'g' , - 1 , 32 ) parsedUint , err := strconv . ParseInt ( s , 10 , 8 ) if err != nil { return fmt . Errorf ( " " , src , s , err ) } i . Uint8 = uint8 ( parsedUint ) i . Valid = true return nil default : return fmt . Errorf ( " " , src , src ) } }
Scan implements the database/sql Scanner interface.
Scan implements the database / sql Scanner interface .
// Use a string intermediate so we can generate an error on any loss of // precision. // Use a string intermediate so we can generate an error on any loss of // precision.