本文整理汇总了Golang中reflect.Value.SetValue方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.SetValue方法的具体用法?Golang Value.SetValue怎么用?Golang Value.SetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.SetValue方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: decodeBSONData
func decodeBSONData(d *decodeState, kind int, value reflect.Value) {
start := d.offset
d.skipValue(kind)
bd := BSONData{Kind: kind, Data: make([]byte, d.offset-start)}
copy(bd.Data, d.data[start:d.offset])
value.SetValue(reflect.NewValue(bd))
}
示例2: fitValue
func fitValue(x interface{}, v reflect.Value) os.Error {
pv, ok := v.(*reflect.PtrValue)
for ok {
if x == nil {
pv.PointTo(nil)
return nil
}
pv.PointTo(reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem()))
v = pv.Elem()
pv, ok = v.(*reflect.PtrValue)
}
// Same type? No conversion is necessary.
xv := reflect.NewValue(x)
if xv != nil && v != nil && xv.Type() == v.Type() {
v.SetValue(xv)
return nil
}
// Going to the empty interface? No conversion is necessary.
iv, ok := v.(*reflect.InterfaceValue)
if ok && iv.Type().(*reflect.InterfaceType).NumMethod() == 0 {
iv.Set(reflect.NewValue(x))
return nil
}
switch t := x.(type) {
case int64:
iv, ok := v.(*reflect.IntValue)
if ok {
if iv.Overflow(t) {
return os.ERANGE
}
iv.Set(t)
break
}
uv, ok := v.(*reflect.UintValue)
if ok {
if t < 0 || uv.Overflow(uint64(t)) {
return os.ERANGE
}
uv.Set(uint64(t))
break
}
return &FitError{"cannot hold an integer", t, "slot", v.Type()}
case uint64:
uv, ok := v.(*reflect.UintValue)
if ok {
if uv.Overflow(t) {
return os.ERANGE
}
uv.Set(t)
break
}
iv, ok := v.(*reflect.IntValue)
if ok {
if t > math.MaxInt64 || iv.Overflow(int64(t)) {
return os.ERANGE
}
iv.Set(int64(t))
break
}
return &FitError{"cannot hold an integer", t, "slot", v.Type()}
case []byte:
return fitBytes(t, v)
case nil:
v.SetValue(reflect.MakeZero(v.Type()))
case []interface{}:
return fitSeq(t, v)
default:
return &FitError{"unknown source type", x, "slot", v.Type()}
}
return nil
}
示例3: literal
// literal consumes a literal from d.data[d.off-1:], decoding into the value v.
// The first byte of the literal has been read already
// (that's how the caller knows it's a literal).
func (d *decodeState) literal(v reflect.Value) {
// All bytes inside literal return scanContinue op code.
start := d.off - 1
op := d.scanWhile(scanContinue)
// Scan read one byte too far; back up.
d.off--
d.scan.undo(op)
item := d.data[start:d.off]
// Check for unmarshaler.
wantptr := item[0] == 'n' // null
unmarshaler, pv := d.indirect(v, wantptr)
if unmarshaler != nil {
err := unmarshaler.UnmarshalJSON(item)
if err != nil {
d.error(err)
}
return
}
v = pv
switch c := item[0]; c {
case 'n': // null
switch v.(type) {
default:
d.saveError(&UnmarshalTypeError{"null", v.Type()})
case *reflect.InterfaceValue, *reflect.PtrValue, *reflect.MapValue:
v.SetValue(nil)
}
case 't', 'f': // true, false
value := c == 't'
switch v := v.(type) {
default:
d.saveError(&UnmarshalTypeError{"bool", v.Type()})
case *reflect.BoolValue:
v.Set(value)
case *reflect.InterfaceValue:
v.Set(reflect.NewValue(value))
}
case '"': // string
s, ok := unquote(item)
if !ok {
d.error(errPhase)
}
switch v := v.(type) {
default:
d.saveError(&UnmarshalTypeError{"string", v.Type()})
case *reflect.StringValue:
v.Set(s)
case *reflect.InterfaceValue:
v.Set(reflect.NewValue(s))
}
default: // number
if c != '-' && (c < '0' || c > '9') {
d.error(errPhase)
}
s := string(item)
switch v := v.(type) {
default:
d.error(&UnmarshalTypeError{"number", v.Type()})
case *reflect.InterfaceValue:
n, err := strconv.Atof64(s)
if err != nil {
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
break
}
v.Set(reflect.NewValue(n))
case *reflect.IntValue:
n, err := strconv.Atoi(s)
if err != nil {
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
break
}
v.Set(n)
case *reflect.Int8Value:
n, err := strconv.Atoi(s)
if err != nil || int(int8(n)) != n {
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
break
}
v.Set(int8(n))
case *reflect.Int16Value:
n, err := strconv.Atoi(s)
if err != nil || int(int16(n)) != n {
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
break
}
v.Set(int16(n))
case *reflect.Int32Value:
n, err := strconv.Atoi(s)
if err != nil || int(int32(n)) != n {
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
//.........这里部分代码省略.........
示例4: literal
// literal consumes a literal from d.data[d.off-1:], decoding into the value v.
// The first byte of the literal has been read already
// (that's how the caller knows it's a literal).
func (d *decodeState) literal(v reflect.Value) {
// All bytes inside literal return scanContinue op code.
start := d.off - 1
op := d.scanWhile(scanContinue)
// Scan read one byte too far; back up.
d.off--
d.scan.undo(op)
item := d.data[start:d.off]
// Check for unmarshaler.
wantptr := item[0] == 'n' // null
unmarshaler, pv := d.indirect(v, wantptr)
if unmarshaler != nil {
err := unmarshaler.UnmarshalJSON(item)
if err != nil {
d.error(err)
}
return
}
v = pv
switch c := item[0]; c {
case 'n': // null
switch v.(type) {
default:
d.saveError(&UnmarshalTypeError{"null", v.Type()})
case *reflect.InterfaceValue, *reflect.PtrValue, *reflect.MapValue:
v.SetValue(nil)
}
case 't', 'f': // true, false
value := c == 't'
switch v := v.(type) {
default:
d.saveError(&UnmarshalTypeError{"bool", v.Type()})
case *reflect.BoolValue:
v.Set(value)
case *reflect.InterfaceValue:
v.Set(reflect.NewValue(value))
}
case '"': // string
s, ok := unquoteBytes(item)
if !ok {
d.error(errPhase)
}
switch v := v.(type) {
default:
d.saveError(&UnmarshalTypeError{"string", v.Type()})
case *reflect.SliceValue:
if v.Type() != byteSliceType {
d.saveError(&UnmarshalTypeError{"string", v.Type()})
break
}
b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
n, err := base64.StdEncoding.Decode(b, s)
if err != nil {
d.saveError(err)
break
}
v.Set(reflect.NewValue(b[0:n]).(*reflect.SliceValue))
case *reflect.StringValue:
v.Set(string(s))
case *reflect.InterfaceValue:
v.Set(reflect.NewValue(string(s)))
}
default: // number
if c != '-' && (c < '0' || c > '9') {
d.error(errPhase)
}
s := string(item)
switch v := v.(type) {
default:
d.error(&UnmarshalTypeError{"number", v.Type()})
case *reflect.InterfaceValue:
n, err := strconv.Atof64(s)
if err != nil {
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
break
}
v.Set(reflect.NewValue(n))
case *reflect.IntValue:
n, err := strconv.Atoi64(s)
if err != nil || v.Overflow(n) {
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
break
}
v.Set(n)
case *reflect.UintValue:
n, err := strconv.Atoui64(s)
if err != nil || v.Overflow(n) {
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
break
//.........这里部分代码省略.........