本文整理汇总了Golang中reflect.Value.OverflowInt方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.OverflowInt方法的具体用法?Golang Value.OverflowInt怎么用?Golang Value.OverflowInt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.OverflowInt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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())
}
示例2: 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)
}
}
示例3: 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
}
}
示例4: 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()})
}
}
示例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: resolve_int
func resolve_int(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
sign := int64(1)
if val[0] == '-' {
sign = -1
val = val[1:]
} else 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 integer: '%s' at %s", original, event.start_mark)
}
var val64 int64
if value <= math.MaxInt64 {
val64 = int64(value)
if sign == -1 {
val64 = -val64
}
} else if sign == -1 && value == uint64(math.MaxInt64)+1 {
val64 = math.MinInt64
} else {
return "", fmt.Errorf("Invalid integer: '%s' at %s", original, event.start_mark)
}
if isNumberValue {
v.SetString(strconv.FormatInt(val64, 10))
} else {
if v.OverflowInt(val64) {
return "", fmt.Errorf("Invalid integer: '%s' at %s", original, event.start_mark)
}
v.SetInt(val64)
}
return yaml_INT_TAG, nil
}
示例7: setInt
func setInt(v reflect.Value, x interface{}) error {
xx := x.(int64)
if v.OverflowInt(xx) {
return fmt.Errorf("bigquery: value %v overflows struct field of type %v", xx, v.Type())
}
v.SetInt(xx)
return nil
}
示例8: 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
}
示例9: 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
}
示例10: decodeInt
func decodeInt(rval reflect.Value, arg string) error {
v, err := strconv.ParseInt(arg, 10, 64)
if err != nil {
return err
}
if rval.OverflowInt(v) {
return fmt.Errorf("value %d would overflow %s", v, rval.Kind())
}
rval.Set(reflect.ValueOf(v).Convert(rval.Type()))
return nil
}
示例11: resolve_int
func resolve_int(val string, v reflect.Value) error {
val = strings.Replace(val, "_", "", -1)
var value int64
sign := int64(1)
if val[0] == '-' {
sign = -1
val = val[1:]
} else 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 := int64(1)
for j := len(digits) - 1; j >= 0; j-- {
n, err := strconv.ParseInt(digits[j], 10, 64)
n *= bes
if err != nil || v.OverflowInt(n) {
return errors.New("Integer: " + val)
}
value += n
bes *= 60
}
value *= sign
v.SetInt(value)
return nil
}
value, err := strconv.ParseInt(val, base, 64)
value *= sign
if err != nil || v.OverflowInt(value) {
return errors.New("Integer: " + val)
}
v.SetInt(value)
return nil
}
示例12: 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)
}
}
}
示例13: unmarshalNumber
func unmarshalNumber(number float64, value reflect.Value) error {
switch k := value.Kind(); k {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intv := int64(number)
if value.OverflowInt(intv) {
fmt.Errorf("Number will overflow field of type %q", k)
}
value.SetInt(intv)
case reflect.Float32, reflect.Float64:
value.SetFloat(float64(number))
default:
return fmt.Errorf("Cannot unmarshal number into %q", k)
}
return nil
}
示例14: setInt
func setInt(rv reflect.Value, i int64) error {
switch rv.Kind() {
case reflect.Ptr:
return setInt(reflect.Indirect(rv), i)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if rv.OverflowInt(i) {
return fmt.Errorf("value %d does not fit into target of type %s", i, rv.Kind().String())
}
rv.SetInt(i)
return nil
case reflect.Interface:
rv.Set(reflect.ValueOf(i))
return nil
default:
return fmt.Errorf("cannot assign int into Kind=%s Type=%#v %#v", rv.Kind().String(), rv.Type(), rv)
}
}
示例15: 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()
}