本文整理汇总了Golang中reflect.Value.SetUint方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.SetUint方法的具体用法?Golang Value.SetUint怎么用?Golang Value.SetUint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.SetUint方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: setFieldVal
// setFieldVal first converts v to the type of structField, then uses reflection
// to set the field to the converted value.
func setFieldVal(structField reflect.Value, name string, v string) error {
switch structField.Kind() {
case reflect.String:
structField.SetString(v)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
vInt, err := strconv.Atoi(v)
if err != nil {
return fmt.Errorf("envvar: Error parsing environment variable %s: %s\n%s", name, v, err.Error())
}
structField.SetInt(int64(vInt))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
vUint, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return fmt.Errorf("envvar: Error parsing environment variable %s: %s\n%s", name, v, err.Error())
}
structField.SetUint(uint64(vUint))
case reflect.Float32, reflect.Float64:
vFloat, err := strconv.ParseFloat(v, 64)
if err != nil {
return fmt.Errorf("envvar: Error parsing environment variable %s: %s\n%s", name, v, err.Error())
}
structField.SetFloat(vFloat)
case reflect.Bool:
vBool, err := strconv.ParseBool(v)
if err != nil {
return fmt.Errorf("envvar: Error parsing environment variable %s: %s\n%s", name, v, err.Error())
}
structField.SetBool(vBool)
default:
return fmt.Errorf("envvar: Unsupported struct field type: %s", structField.Type().String())
}
return nil
}
示例2: setModelValue
func (d base) setModelValue(driverValue, fieldValue reflect.Value) error {
switch fieldValue.Type().Kind() {
case reflect.Bool:
fieldValue.SetBool(d.dialect.parseBool(driverValue.Elem()))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldValue.SetInt(driverValue.Elem().Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// reading uint from int value causes panic
switch driverValue.Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldValue.SetUint(uint64(driverValue.Elem().Int()))
default:
fieldValue.SetUint(driverValue.Elem().Uint())
}
case reflect.Float32, reflect.Float64:
fieldValue.SetFloat(driverValue.Elem().Float())
case reflect.String:
fieldValue.SetString(string(driverValue.Elem().Bytes()))
case reflect.Slice:
if reflect.TypeOf(driverValue.Interface()).Elem().Kind() == reflect.Uint8 {
fieldValue.SetBytes(driverValue.Elem().Bytes())
}
case reflect.Struct:
switch fieldValue.Interface().(type) {
case time.Time:
fieldValue.Set(driverValue.Elem())
default:
if scanner, ok := fieldValue.Addr().Interface().(sql.Scanner); ok {
return scanner.Scan(driverValue.Interface())
}
}
}
return nil
}
示例3: convertAssignInt
func convertAssignInt(d reflect.Value, s int64) (err error) {
switch d.Type().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
d.SetInt(s)
if d.Int() != s {
err = strconv.ErrRange
d.SetInt(0)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if s < 0 {
err = strconv.ErrRange
} else {
x := uint64(s)
d.SetUint(x)
if d.Uint() != x {
err = strconv.ErrRange
d.SetUint(0)
}
}
case reflect.Bool:
d.SetBool(s != 0)
default:
err = cannotConvert(d, s)
}
return
}
示例4: Marshal
func (defaultMarshaler) Marshal(v reflect.Value, s string) error {
if v.Kind() == reflect.Slice {
n := reflect.New(v.Type().Elem())
m := valueMarshaler(n.Elem())
err := m.Marshal(n.Elem(), s)
if err != nil {
return err
}
v.Set(reflect.Append(v, n.Elem()))
return nil
}
switch v.Kind() {
case reflect.Int:
x, err := strconv.ParseInt(s, 0, 0)
v.SetInt(x)
return err
case reflect.Uint:
x, err := strconv.ParseUint(s, 0, 0)
v.SetUint(x)
return err
case reflect.Int64:
x, err := strconv.ParseInt(s, 0, 64)
v.SetInt(x)
return err
case reflect.String:
v.SetString(s)
return nil
default:
return fmt.Errorf("unhandled builtin type: %s", v.Type().String())
}
}
示例5: SetModelValue
func (d *base) SetModelValue(driverValue, fieldValue reflect.Value) error {
// ignore zero types
if !driverValue.Elem().IsValid() {
return nil
}
switch fieldValue.Type().Kind() {
case reflect.Bool:
fieldValue.SetBool(d.Dialect.ParseBool(driverValue.Elem()))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldValue.SetInt(driverValue.Elem().Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// reading uint from int value causes panic
switch driverValue.Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldValue.SetUint(uint64(driverValue.Elem().Int()))
default:
fieldValue.SetUint(driverValue.Elem().Uint())
}
case reflect.Float32, reflect.Float64:
fieldValue.SetFloat(driverValue.Elem().Float())
case reflect.String:
fieldValue.SetString(string(driverValue.Elem().Bytes()))
case reflect.Slice:
if reflect.TypeOf(driverValue.Interface()).Elem().Kind() == reflect.Uint8 {
fieldValue.SetBytes(driverValue.Elem().Bytes())
}
case reflect.Struct:
if _, ok := fieldValue.Interface().(time.Time); ok {
fieldValue.Set(driverValue.Elem())
}
}
return nil
}
示例6: boolAsUintDecoder
func boolAsUintDecoder(dv, sv reflect.Value) {
if sv.Bool() {
dv.SetUint(1)
} else {
dv.SetUint(0)
}
}
示例7: ValueToField
func (d *MySQL) ValueToField(value reflect.Value, field reflect.Value) error {
switch field.Type().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.SetInt(value.Elem().Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// reading uint from int value causes panic
switch value.Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.SetUint(uint64(value.Elem().Int()))
default:
field.SetUint(value.Elem().Uint())
}
case reflect.Float32, reflect.Float64:
field.SetFloat(value.Elem().Float())
case reflect.String:
if !value.Elem().IsValid() {
field.SetString("")
return nil
}
field.SetString(string(value.Elem().Bytes()))
case reflect.Slice:
if reflect.TypeOf(value.Interface()).Elem().Kind() == reflect.Uint8 {
field.SetBytes(value.Elem().Bytes())
}
case reflect.Struct:
if field.Type() == reflect.TypeOf(time.Time{}) {
field.Set(value.Elem())
}
}
return nil
}
示例8: uint64
// type(1) | name length(1) | raw name bytes | 0x00 | value bytes(8)
func (d *decodeState) uint64(v reflect.Value) {
d.off += 1 // type
klen := int(Int8(d.data[d.off:]))
d.off += 1 // name length
if klen > 0 {
key := d.data[d.off : d.off+klen-1]
d.off += klen
v = fieldByTag(v, key)
u, pv := d.indirect(v, false)
if u != nil {
d.off -= 1 + 1 + klen
if err := u.UnmarshalMCPACK(d.next()); err != nil {
d.error(err)
}
return
}
v = pv
}
val := Uint64(d.data[d.off:])
d.off += 8 // value
if !v.IsValid() {
return
}
v.SetUint(val)
}
示例9: setWithProperType
// setWithProperType sets proper value to field based on its type,
// but it does not return error for failing parsing,
// because we want to use default value that is already assigned to strcut.
func setWithProperType(t reflect.Type, key *Key, field reflect.Value, delim string) error {
switch t.Kind() {
case reflect.String:
if len(key.String()) == 0 {
return nil
}
field.SetString(key.String())
case reflect.Bool:
boolVal, err := key.Bool()
if err != nil {
return nil
}
field.SetBool(boolVal)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
durationVal, err := key.Duration()
// Skip zero value
if err == nil && int(durationVal) > 0 {
field.Set(reflect.ValueOf(durationVal))
return nil
}
intVal, err := key.Int64()
if err != nil || intVal == 0 {
return nil
}
field.SetInt(intVal)
// byte is an alias for uint8, so supporting uint8 breaks support for byte
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
durationVal, err := key.Duration()
// Skip zero value
if err == nil && int(durationVal) > 0 {
field.Set(reflect.ValueOf(durationVal))
return nil
}
uintVal, err := key.Uint64()
if err != nil {
return nil
}
field.SetUint(uintVal)
case reflect.Float64:
floatVal, err := key.Float64()
if err != nil {
return nil
}
field.SetFloat(floatVal)
case reflectTime:
timeVal, err := key.Time()
if err != nil {
return nil
}
field.Set(reflect.ValueOf(timeVal))
case reflect.Slice:
return setSliceWithProperType(key, field, delim)
default:
return fmt.Errorf("unsupported type '%s'", t)
}
return nil
}
示例10: setWithProperType
// This sets the value in a struct of an indeterminate type to the
// matching value from the request (via Form middleware) in the
// same type, so that not all deserialized values have to be strings.
// Supported types are string, int, float, and bool.
func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value, nameInTag string, errors Errors) {
if scan, ok := structField.Addr().Interface().(sql.Scanner); ok {
scan.Scan(val)
return
}
switch valueKind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if val == "" {
val = "0"
}
intVal, err := strconv.ParseInt(val, 10, 64)
if err != nil {
errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as integer")
} else {
structField.SetInt(intVal)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if val == "" {
val = "0"
}
uintVal, err := strconv.ParseUint(val, 10, 64)
if err != nil {
errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as unsigned integer")
} else {
structField.SetUint(uintVal)
}
case reflect.Bool:
if val == "" {
val = "false"
}
boolVal, err := strconv.ParseBool(val)
if err != nil {
errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as boolean")
} else {
structField.SetBool(boolVal)
}
case reflect.Float32:
if val == "" {
val = "0.0"
}
floatVal, err := strconv.ParseFloat(val, 32)
if err != nil {
errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as 32-bit float")
} else {
structField.SetFloat(floatVal)
}
case reflect.Float64:
if val == "" {
val = "0.0"
}
floatVal, err := strconv.ParseFloat(val, 64)
if err != nil {
errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as 64-bit float")
} else {
structField.SetFloat(floatVal)
}
case reflect.String:
structField.SetString(val)
}
}
示例11: setConfigVal
func setConfigVal(str string, field reflect.Value) error {
t := field.Kind()
switch {
case t == reflect.String:
field.SetString(str)
case t >= reflect.Int && t <= reflect.Int64:
if x, err := strconv.ParseInt(str, 10, 64); err != nil {
return err
} else {
field.SetInt(x)
}
case t >= reflect.Uint && t <= reflect.Uint64:
if x, err := strconv.ParseUint(str, 10, 64); err != nil {
return err
} else {
field.SetUint(x)
}
case t >= reflect.Float32 && t <= reflect.Float64:
if x, err := strconv.ParseFloat(str, 64); err != nil {
return err
} else {
field.SetFloat(x)
}
case t == reflect.Bool:
if x, err := strconv.ParseBool(str); err != nil {
return err
} else {
field.SetBool(x)
}
default:
return nil
}
return nil
}
示例12: readInteger
func (decoder *Decoder) readInteger(value reflect.Value) error {
uv, err := decoder.readU29()
if err != nil {
return err
}
vv := int32(uv)
if uv > 0xfffffff {
vv = int32(uv - 0x20000000)
}
switch value.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value.SetInt(int64(vv))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value.SetUint(uint64(uv))
case reflect.Interface:
value.Set(reflect.ValueOf(uv))
default:
return errors.New("invalid type:" + value.Type().String() + " for integer")
}
return nil
}
示例13: readFloat
func (decoder *Decoder) readFloat(value reflect.Value) error {
bytes, err := decoder.readBytes(8)
if err != nil {
return err
}
n := uint64(0)
for _, b := range bytes {
n <<= 8
n |= uint64(b)
}
v := math.Float64frombits(n)
switch value.Kind() {
case reflect.Float32, reflect.Float64:
value.SetFloat(v)
case reflect.Int32, reflect.Int, reflect.Int64:
value.SetInt(int64(v))
case reflect.Uint32, reflect.Uint, reflect.Uint64:
value.SetUint(uint64(v))
case reflect.Interface:
value.Set(reflect.ValueOf(v))
default:
return errors.New("invalid type:" + value.Type().String() + " for double")
}
return nil
}
示例14: 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())
}
示例15: 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)
}
}