本文整理汇总了Golang中reflect.Value.OverflowUint方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.OverflowUint方法的具体用法?Golang Value.OverflowUint怎么用?Golang Value.OverflowUint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.OverflowUint方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: unmarshalInteger
func unmarshalInteger(i int64, v reflect.Value) {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.OverflowInt(i) {
panic(&UnmarshalOverflowError{"integer " + strconv.FormatInt(i, 10), v.Type()})
}
v.SetInt(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if i < 0 {
panic(&UnmarshalOverflowError{"integer " + strconv.FormatInt(i, 10), v.Type()})
}
u := uint64(i)
if v.OverflowUint(u) {
panic(&UnmarshalOverflowError{"integer " + strconv.FormatUint(u, 10), v.Type()})
}
v.SetUint(u)
case reflect.Interface:
if v.NumMethod() == 0 {
v.Set(reflect.ValueOf(i))
return
}
fallthrough
default:
panic(&UnmarshalTypeError{"integer " + strconv.FormatInt(i, 10), v.Type()})
}
}
示例2: unmarshalUint
func (d *decoder) unmarshalUint(size uint, offset uint, result reflect.Value, uintType uint) (uint, error) {
if size > uintType/8 {
return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (uint%v size of %v)", uintType, size)
}
value, newOffset, err := d.decodeUint(size, offset)
if err != nil {
return 0, err
}
switch result.Kind() {
default:
return newOffset, newUnmarshalTypeError(value, result.Type())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n := int64(value)
if result.OverflowInt(n) {
return 0, newUnmarshalTypeError(value, result.Type())
}
result.SetInt(n)
return newOffset, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
if result.OverflowUint(value) {
return 0, newUnmarshalTypeError(value, result.Type())
}
result.SetUint(value)
return newOffset, nil
case reflect.Interface:
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
}
示例3: setUint
func setUint(rv reflect.Value, u uint64) error {
switch rv.Kind() {
case reflect.Ptr:
if rv.IsNil() {
if rv.CanSet() {
rv.Set(reflect.New(rv.Type().Elem()))
// fall through to set indirect below
} else {
return fmt.Errorf("trying to put uint into unsettable nil ptr")
}
}
return setUint(reflect.Indirect(rv), u)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if rv.OverflowUint(u) {
return fmt.Errorf("value %d does not fit into target of type %s", u, rv.Kind().String())
}
rv.SetUint(u)
return nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if (u == 0xffffffffffffffff) || rv.OverflowInt(int64(u)) {
return fmt.Errorf("value %d does not fit into target of type %s", u, rv.Kind().String())
}
rv.SetInt(int64(u))
return nil
case reflect.Interface:
rv.Set(reflect.ValueOf(u))
return nil
default:
return fmt.Errorf("cannot assign uint into Kind=%s Type=%#v %#v", rv.Kind().String(), rv.Type(), rv)
}
}
示例4: unmarshalInt32
func (d *decoder) unmarshalInt32(size uint, offset uint, result reflect.Value) (uint, error) {
if size > 4 {
return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (int32 size of %v)", size)
}
value, newOffset, err := d.decodeInt(size, offset)
if err != nil {
return 0, err
}
switch result.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n := int64(value)
if !result.OverflowInt(n) {
result.SetInt(n)
return newOffset, nil
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n := uint64(value)
if !result.OverflowUint(n) {
result.SetUint(n)
return newOffset, nil
}
case reflect.Interface:
if result.NumMethod() == 0 {
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
}
return newOffset, newUnmarshalTypeError(value, result.Type())
}
示例5: numberDecoder
func numberDecoder(node RMNode, data interface{}, v reflect.Value) {
val, err := redis.String(data, nil)
if err != nil {
panic(err)
}
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(val, 10, 64)
if err != nil || v.OverflowInt(n) {
panic(fmt.Sprintf("Unable to convert int: %s \n", val))
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n, err := strconv.ParseUint(val, 10, 64)
if err != nil || v.OverflowUint(n) {
panic(fmt.Sprintf("Unable to convert uint: %s \n", val))
}
v.SetUint(n)
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(val, v.Type().Bits())
if err != nil || v.OverflowFloat(n) {
panic(fmt.Sprintf("Unable to convert float: %s \n", val))
}
v.SetFloat(n)
default:
panic(fmt.Sprintf("Unsupported number convertion for type[%v] with value[%v]", v.Type(), data))
}
}
示例6: setStringValue
func setStringValue(v reflect.Value, value string) (err error) {
s := value
// if type is []byte
if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 {
v.SetBytes([]byte(s))
return
}
switch v.Kind() {
case reflect.String:
v.SetString(s)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var n int64
n, err = strconv.ParseInt(s, 10, 64)
if err != nil {
return
}
if v.OverflowInt(n) {
err = fmt.Errorf("overflow int64 for %d.", n)
return
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
var n uint64
n, err = strconv.ParseUint(s, 10, 64)
if err != nil {
return
}
if v.OverflowUint(n) {
err = fmt.Errorf("overflow uint64 for %d.", n)
return
}
v.SetUint(n)
case reflect.Float32, reflect.Float64:
var n float64
n, err = strconv.ParseFloat(s, v.Type().Bits())
if err != nil {
return
}
if v.OverflowFloat(n) {
err = fmt.Errorf("overflow float64 for %d.", n)
return
}
v.SetFloat(n)
case reflect.Bool:
var n bool
n, err = strconv.ParseBool(s)
if err != nil {
return
}
v.SetBool(n)
default:
err = errors.New(fmt.Sprintf("value %+v can only been set to primary type but was %+v", value, v))
}
return
}
示例7: decodeNumber
func (d *Decoder) decodeNumber(n *string, v reflect.Value) error {
switch v.Kind() {
case reflect.Interface:
i, err := d.decodeNumberToInterface(n)
if err != nil {
return err
}
v.Set(reflect.ValueOf(i))
return nil
case reflect.String:
if v.Type() == numberType { // Support Number value type
v.Set(reflect.ValueOf(Number(*n)))
return nil
}
v.Set(reflect.ValueOf(*n))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i, err := strconv.ParseInt(*n, 10, 64)
if err != nil {
return err
}
if v.OverflowInt(i) {
return &UnmarshalTypeError{
Value: fmt.Sprintf("number overflow, %s", *n),
Type: v.Type(),
}
}
v.SetInt(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
i, err := strconv.ParseUint(*n, 10, 64)
if err != nil {
return err
}
if v.OverflowUint(i) {
return &UnmarshalTypeError{
Value: fmt.Sprintf("number overflow, %s", *n),
Type: v.Type(),
}
}
v.SetUint(i)
case reflect.Float32, reflect.Float64:
i, err := strconv.ParseFloat(*n, 64)
if err != nil {
return err
}
if v.OverflowFloat(i) {
return &UnmarshalTypeError{
Value: fmt.Sprintf("number overflow, %s", *n),
Type: v.Type(),
}
}
v.SetFloat(i)
default:
return &UnmarshalTypeError{Value: "number", Type: v.Type()}
}
return nil
}
示例8: decodeUint
func decodeUint(rval reflect.Value, arg string) error {
v, err := strconv.ParseUint(arg, 10, 64)
if err != nil {
return err
}
if rval.OverflowUint(v) {
return fmt.Errorf("value %d would overflow %s", v, rval.Kind())
}
rval.Set(reflect.ValueOf(v).Convert(rval.Type()))
return nil
}
示例9: unmarshalStringToNumberOrBoolean
func unmarshalStringToNumberOrBoolean(d *decodeState, s []byte, v reflect.Value, fromQuoted bool) {
if fromQuoted {
d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
return
}
switch {
default:
d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
case isValidNumberBytes(s):
switch v.Kind() {
default:
d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s := string(s)
n, err := strconv.ParseInt(s, 10, 64)
if err != nil || v.OverflowInt(n) {
d.saveError(&UnmarshalTypeError{"string " + s, v.Type(), int64(d.off)})
break
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
s := string(s)
n, err := strconv.ParseUint(s, 10, 64)
if err != nil || v.OverflowUint(n) {
d.saveError(&UnmarshalTypeError{"string " + s, v.Type(), int64(d.off)})
break
}
v.SetUint(n)
case reflect.Float32, reflect.Float64:
s := string(s)
n, err := strconv.ParseFloat(s, v.Type().Bits())
if err != nil || v.OverflowFloat(n) {
d.saveError(&UnmarshalTypeError{"string " + s, v.Type(), int64(d.off)})
break
}
v.SetFloat(n)
}
case bytes.Equal(s, trueLiteral):
switch v.Kind() {
default:
d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
case reflect.Bool:
v.SetBool(true)
}
case bytes.Equal(s, falseLiteral):
switch v.Kind() {
default:
d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
case reflect.Bool:
v.SetBool(false)
}
}
}
示例10: resolve_uint
func resolve_uint(val string, v reflect.Value) error {
val = strings.Replace(val, "_", "", -1)
var value uint64
if val[0] == '-' {
return errors.New("Unsigned int with negative value: " + val)
}
if val[0] == '+' {
val = val[1:]
}
base := 10
if val == "0" {
v.Set(reflect.Zero(v.Type()))
return nil
}
if strings.HasPrefix(val, "0b") {
base = 2
val = val[2:]
} else if strings.HasPrefix(val, "0x") {
base = 16
val = val[2:]
} else if val[0] == '0' {
base = 8
val = val[1:]
} else if strings.Contains(val, ":") {
digits := strings.Split(val, ":")
bes := uint64(1)
for j := len(digits) - 1; j >= 0; j-- {
n, err := strconv.ParseUint(digits[j], 10, 64)
n *= bes
if err != nil || v.OverflowUint(n) {
return errors.New("Unsigned Integer: " + val)
}
value += n
bes *= 60
}
v.SetUint(value)
return nil
}
value, err := strconv.ParseUint(val, base, 64)
if err != nil || v.OverflowUint(value) {
return errors.New("Unsigned Integer: " + val)
}
v.SetUint(value)
return nil
}
示例11: resolve_uint
func resolve_uint(val string, v reflect.Value, useNumber bool, event yaml_event_t) (string, error) {
original := val
val = strings.Replace(val, "_", "", -1)
var value uint64
isNumberValue := v.Type() == numberType
if val[0] == '-' {
return "", fmt.Errorf("Unsigned int with negative value: '%s' at %s", original, event.start_mark)
}
if val[0] == '+' {
val = val[1:]
}
base := 0
if val == "0" {
if isNumberValue {
v.SetString("0")
} else {
v.Set(reflect.Zero(v.Type()))
}
return yaml_INT_TAG, nil
}
if strings.HasPrefix(val, "0o") {
base = 8
val = val[2:]
}
value, err := strconv.ParseUint(val, base, 64)
if err != nil {
return "", fmt.Errorf("Invalid unsigned integer: '%s' at %s", val, event.start_mark)
}
if isNumberValue {
v.SetString(strconv.FormatUint(value, 10))
} else {
if v.OverflowUint(value) {
return "", fmt.Errorf("Invalid unsigned integer: '%s' at %s", val, event.start_mark)
}
v.SetUint(value)
}
return yaml_INT_TAG, nil
}
示例12: parse_int
// called when 'i' was consumed
func (d *decoder) parse_int(v reflect.Value) {
start := d.offset - 1
d.read_until('e')
if d.buf.Len() == 0 {
panic(&SyntaxError{
Offset: start,
what: "empty integer value",
})
}
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(d.buf.String(), 10, 64)
check_for_int_parse_error(err, start)
if v.OverflowInt(n) {
panic(&UnmarshalTypeError{
Value: "integer " + d.buf.String(),
Type: v.Type(),
})
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
n, err := strconv.ParseUint(d.buf.String(), 10, 64)
check_for_int_parse_error(err, start)
if v.OverflowUint(n) {
panic(&UnmarshalTypeError{
Value: "integer " + d.buf.String(),
Type: v.Type(),
})
}
v.SetUint(n)
case reflect.Bool:
if d.buf.Len() == 1 && d.buf.Bytes()[0] == '0' {
v.SetBool(false)
}
v.SetBool(true)
default:
panic(&UnmarshalTypeError{
Value: "integer " + d.buf.String(),
Type: v.Type(),
})
}
d.buf.Reset()
}
示例13: parse_int
// called when 'i' was consumed
func (d *decoder) parse_int(v reflect.Value) {
start := d.offset - 1
d.read_until('e')
if d.buf.Len() == 0 {
panic(&SyntaxError{
Offset: start,
What: errors.New("empty integer value"),
})
}
s := d.buf.String()
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(s, 10, 64)
check_for_int_parse_error(err, start)
if v.OverflowInt(n) {
panic(&UnmarshalTypeError{
Value: "integer " + s,
Type: v.Type(),
})
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
n, err := strconv.ParseUint(s, 10, 64)
check_for_int_parse_error(err, start)
if v.OverflowUint(n) {
panic(&UnmarshalTypeError{
Value: "integer " + s,
Type: v.Type(),
})
}
v.SetUint(n)
case reflect.Bool:
v.SetBool(s != "0")
default:
panic(&UnmarshalTypeError{
Value: "integer " + s,
Type: v.Type(),
})
}
d.buf.Reset()
}
示例14: numberStore
func (d *decodeState) numberStore(item []byte, v reflect.Value, fromQuoted bool) {
s := string(item)
switch v.Kind() {
default:
if v.Kind() == reflect.String && v.Type() == numberType {
v.SetString(s)
break
}
if fromQuoted {
d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
}
d.error(&UnmarshalTypeError{"number", v.Type()})
case reflect.Interface:
n, err := d.convertNumber(string(item))
if err != nil {
d.saveError(err)
break
}
v.Set(reflect.ValueOf(n))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(s, 10, 64)
if err != nil || v.OverflowInt(n) {
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
break
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n, err := strconv.ParseUint(s, 10, 64)
if err != nil || v.OverflowUint(n) {
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
break
}
v.SetUint(n)
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(s, v.Type().Bits())
if err != nil || v.OverflowFloat(n) {
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
break
}
v.SetFloat(n)
}
}
示例15: setValue
// Set Value with given string
func (d *decodeState) setValue(v reflect.Value, s string) {
//fmt.Printf("SET(kind:%s, %s)\n", v.Kind(), s)
switch v.Kind() {
case reflect.String:
v.SetString(s)
case reflect.Bool:
v.SetBool(boolValue(s))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(s, 10, 64)
if err != nil || v.OverflowInt(n) {
d.saveError(&IniError{d.lineNum, d.line, "Invalid int"})
return
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n, err := strconv.ParseUint(s, 10, 64)
if err != nil || v.OverflowUint(n) {
d.saveError(&IniError{d.lineNum, d.line, "Invalid uint"})
return
}
v.SetUint(n)
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(s, v.Type().Bits())
if err != nil || v.OverflowFloat(n) {
d.saveError(&IniError{d.lineNum, d.line, "Invalid float"})
return
}
v.SetFloat(n)
case reflect.Slice:
d.sliceValue(v, s)
default:
d.saveError(&IniError{d.lineNum, d.line, fmt.Sprintf("Can't set value of type %s", v.Kind())})
}
}