本文整理汇总了Golang中reflect.Value.Set方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.Set方法的具体用法?Golang Value.Set怎么用?Golang Value.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.Set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: decodeMap
// decodeMap treats the next bytes as an XDR encoded variable array of 2-element
// structures whose fields are of the same type as the map keys and elements
// represented by the passed reflection value. Pointers are automatically
// indirected and allocated as necessary. It returns the the number of bytes
// actually read.
//
// An UnmarshalError is returned if any issues are encountered while decoding
// the elements.
func (d *Decoder) decodeMap(v reflect.Value) (int, error) {
dataLen, n, err := d.DecodeUint()
if err != nil {
return n, err
}
// Allocate storage for the underlying map if needed.
vt := v.Type()
if v.IsNil() {
v.Set(reflect.MakeMap(vt))
}
// Decode each key and value according to their type.
keyType := vt.Key()
elemType := vt.Elem()
for i := uint32(0); i < dataLen; i++ {
key := reflect.New(keyType).Elem()
n2, err := d.decode(key)
n += n2
if err != nil {
return n, err
}
val := reflect.New(elemType).Elem()
n2, err = d.decode(val)
n += n2
if err != nil {
return n, err
}
v.SetMapIndex(key, val)
}
return n, nil
}
示例2: decodeMap
func decodeMap(d *decodeState, kind int, v reflect.Value) {
t := v.Type()
if t.Key().Kind() != reflect.String || kind != kindDocument {
d.saveErrorAndSkip(kind, t)
return
}
if v.IsNil() {
v.Set(reflect.MakeMap(t))
}
subv := reflect.New(t.Elem()).Elem()
offset := d.beginDoc()
for {
kind, name := d.scanKindName()
if kind == 0 {
break
}
if kind == kindNull {
continue
}
subv.Set(reflect.Zero(t.Elem()))
d.decodeValue(kind, subv)
v.SetMapIndex(reflect.ValueOf(string(name)), subv)
}
d.endDoc(offset)
}
示例3: setField
func setField(field reflect.Value, defaultVal string) {
var iface interface{}
var err error
switch field.Kind() {
case reflect.Bool:
iface, err = strconv.ParseBool(defaultVal)
case reflect.Int:
iface, err = strconv.ParseInt(defaultVal, 10, 64)
iface = int(iface.(int64))
case reflect.Int8:
iface, err = strconv.ParseInt(defaultVal, 10, 8)
iface = int8(iface.(int64))
case reflect.Int16:
iface, err = strconv.ParseInt(defaultVal, 10, 16)
iface = int16(iface.(int64))
case reflect.Int32:
iface, err = strconv.ParseInt(defaultVal, 10, 32)
iface = int32(iface.(int64))
case reflect.Int64:
t, err := time.ParseDuration(defaultVal)
if err == nil {
iface, err = t, nil
} else {
iface, err = strconv.ParseInt(defaultVal, 10, 64)
}
case reflect.Uint:
iface, err = strconv.ParseUint(defaultVal, 10, 64)
iface = uint(iface.(uint64))
case reflect.Uint8:
iface, err = strconv.ParseUint(defaultVal, 10, 8)
iface = uint8(iface.(uint64))
case reflect.Uint16:
iface, err = strconv.ParseUint(defaultVal, 10, 16)
iface = uint16(iface.(uint64))
case reflect.Uint32:
iface, err = strconv.ParseUint(defaultVal, 10, 32)
iface = uint32(iface.(uint64))
case reflect.Uint64:
iface, err = strconv.ParseUint(defaultVal, 10, 64)
case reflect.Uintptr:
iface, err = strconv.ParseUint(defaultVal, 10, 64)
iface = uintptr(iface.(uint64))
case reflect.Float32:
iface, err = strconv.ParseFloat(defaultVal, 32)
iface = float32(iface.(float64))
case reflect.Float64:
iface, err = strconv.ParseFloat(defaultVal, 64)
case reflect.String:
iface = defaultVal
default:
err = errInvalidFieldType
}
if err == nil {
if field.CanSet() {
field.Set(reflect.ValueOf(iface))
}
}
}
示例4: unmarshalDictionary
func (p *Decoder) unmarshalDictionary(pval *plistValue, val reflect.Value) {
typ := val.Type()
switch val.Kind() {
case reflect.Struct:
tinfo, err := getTypeInfo(typ)
if err != nil {
panic(err)
}
subvalues := pval.value.(*dictionary).m
for _, finfo := range tinfo.fields {
p.unmarshal(subvalues[finfo.name], finfo.value(val))
}
case reflect.Map:
if val.IsNil() {
val.Set(reflect.MakeMap(typ))
}
subvalues := pval.value.(*dictionary).m
for k, sval := range subvalues {
keyv := reflect.ValueOf(k).Convert(typ.Key())
mapElem := val.MapIndex(keyv)
if !mapElem.IsValid() {
mapElem = reflect.New(typ.Elem()).Elem()
}
p.unmarshal(sval, mapElem)
val.SetMapIndex(keyv, mapElem)
}
default:
panic(&incompatibleDecodeTypeError{typ, pval.kind})
}
}
示例5: decodeBSONData
func decodeBSONData(d *decodeState, kind int, v 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])
v.Set(reflect.ValueOf(bd))
}
示例6: decodeTime
func decodeTime(r io.Reader, f reflect.Value) error {
s, err := decodeStr(r)
if err != nil {
return err
}
var t time.Time
if s != "" {
// Samsung has trailing dots.
s = strings.TrimRight(s, ".")
// Jolla Sailfish has trailing "Z".
s = strings.TrimRight(s, "Z")
t, err = time.Parse(timeFormat, s)
if err != nil {
// Nokia lumia has numTZ
t, err = time.Parse(timeFormatNumTZ, s)
if err != nil {
return err
}
}
}
f.Set(reflect.ValueOf(t))
return nil
}
示例7: setSliceFieldValue
func (p *untypedParamBinder) setSliceFieldValue(target reflect.Value, defaultValue interface{}, data []string) error {
if len(data) == 0 && p.parameter.Required && p.parameter.Default == nil {
return errors.Required(p.Name, p.parameter.In)
}
defVal := reflect.Zero(target.Type())
if defaultValue != nil {
defVal = reflect.ValueOf(defaultValue)
}
if len(data) == 0 {
target.Set(defVal)
return nil
}
sz := len(data)
value := reflect.MakeSlice(reflect.SliceOf(target.Type().Elem()), sz, sz)
for i := 0; i < sz; i++ {
if err := p.setFieldValue(value.Index(i), nil, data[i]); err != nil {
return err
}
}
target.Set(value)
return nil
}
示例8: getSetter
func getSetter(outt reflect.Type, out reflect.Value) Setter {
setterMutex.RLock()
style := setterStyle[outt]
setterMutex.RUnlock()
if style == setterNone {
return nil
}
if style == setterUnknown {
setterMutex.Lock()
defer setterMutex.Unlock()
if outt.Implements(setterIface) {
setterStyle[outt] = setterType
} else if reflect.PtrTo(outt).Implements(setterIface) {
setterStyle[outt] = setterAddr
} else {
setterStyle[outt] = setterNone
return nil
}
style = setterStyle[outt]
}
if style == setterAddr {
if !out.CanAddr() {
return nil
}
out = out.Addr()
} else if outt.Kind() == reflect.Ptr && out.IsNil() {
out.Set(reflect.New(outt.Elem()))
}
return out.Interface().(Setter)
}
示例9: mappingSlice
func (d *decoder) mappingSlice(n *node, out reflect.Value) (good bool) {
outt := out.Type()
if outt.Elem() != mapItemType {
d.terror(n, yaml_MAP_TAG, out)
return false
}
mapType := d.mapType
d.mapType = outt
var slice []MapItem
var l = len(n.children)
for i := 0; i < l; i += 2 {
if isMerge(n.children[i]) {
d.merge(n.children[i+1], out)
continue
}
item := MapItem{}
k := reflect.ValueOf(&item.Key).Elem()
if d.unmarshal(n.children[i], k) {
v := reflect.ValueOf(&item.Value).Elem()
if d.unmarshal(n.children[i+1], v) {
slice = append(slice, item)
}
}
}
out.Set(reflect.ValueOf(slice))
d.mapType = mapType
return true
}
示例10: Add
// Add adds a new process with a given name to the network.
// It returns true on success or panics and returns false on error.
func (n *Graph) Add(c interface{}, name string) bool {
// Check if passed interface is a valid pointer to struct
v := reflect.ValueOf(c)
if v.Kind() != reflect.Ptr || v.IsNil() {
panic("flow.Graph.Add() argument is not a valid pointer")
return false
}
v = v.Elem()
if v.Kind() != reflect.Struct {
panic("flow.Graph.Add() argument is not a valid pointer to struct")
return false
}
// Set the link to self in the proccess so that it could use it
var vNet reflect.Value
vCom := v.FieldByName("Component")
if vCom.IsValid() && vCom.Type().Name() == "Component" {
vNet = vCom.FieldByName("Net")
} else {
vGraph := v.FieldByName("Graph")
if vGraph.IsValid() && vGraph.Type().Name() == "Graph" {
vNet = vGraph.FieldByName("Net")
}
}
if vNet.IsValid() && vNet.CanSet() {
vNet.Set(reflect.ValueOf(n))
}
// Add to the map of processes
n.procs[name] = c
return true
}
示例11: 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
}
示例12: indirect
// indirect walks down v allocating pointers as needed,
// until it gets to a non-pointer.
func indirect(v reflect.Value) reflect.Value {
// If v is a named type and is addressable,
// start with its address, so that if the type has pointer methods,
// we find them.
if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
v = v.Addr()
}
for {
// Load value from interface, but only if the result will be
// usefully addressable.
if v.Kind() == reflect.Interface && !v.IsNil() {
e := v.Elem()
if e.Kind() == reflect.Ptr && !e.IsNil() && e.Elem().Kind() == reflect.Ptr {
v = e
continue
}
}
if v.Kind() != reflect.Ptr {
break
}
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
return v
}
示例13: sequence
func (d *decoder) sequence(n *node, out reflect.Value) (good bool) {
if set := d.setter("!!seq", &out, &good); set != nil {
defer set()
}
if out.Kind() == reflect.Interface {
// No type hints. Will have to use a generic sequence.
iface := out
out = settableValueOf(make([]interface{}, 0))
iface.Set(out)
}
if out.Kind() != reflect.Slice {
return false
}
et := out.Type().Elem()
l := len(n.children)
for i := 0; i < l; i++ {
e := reflect.New(et).Elem()
if ok := d.unmarshal(n.children[i], e); ok {
out.Set(reflect.Append(out, e))
}
}
return true
}
示例14: hydrateNestedComponent
func hydrateNestedComponent(v reflect.Value, component *token) error {
// create a new object to hold the property value
var vnew, varr = newValue(v)
if err := hydrateComponent(vnew, component); err != nil {
return utils.NewError(hydrateNestedComponent, "unable to decode component", component, err)
}
if varr {
// for arrays, append the new value into the array structure
voldval := dereferencePointerValue(v)
if !voldval.CanSet() {
return utils.NewError(hydrateNestedComponent, "unable to set array value", v, nil)
} else {
voldval.Set(reflect.Append(voldval, vnew))
}
} else if !v.CanSet() {
return utils.NewError(hydrateNestedComponent, "unable to set pointer value", v, nil)
} else {
// everything else should be a pointer, set it directly
v.Set(vnew)
}
return nil
}
示例15: decodeSliceValue
func decodeSliceValue(d *Decoder, v reflect.Value) error {
n, err := d.DecodeArrayLen()
if err != nil {
return err
}
if n == -1 {
v.Set(reflect.Zero(v.Type()))
return nil
}
if n == 0 && v.IsNil() {
v.Set(reflect.MakeSlice(v.Type(), 0, 0))
return nil
}
if v.Cap() >= n {
v.Set(v.Slice(0, n))
} else if v.Len() < v.Cap() {
v.Set(v.Slice(0, v.Cap()))
}
for i := 0; i < n; i++ {
if i >= v.Len() {
v.Set(growSliceValue(v, n))
}
sv := v.Index(i)
if err := d.DecodeValue(sv); err != nil {
return err
}
}
return nil
}