本文整理汇总了Golang中reflect.StructValue.FieldByName方法的典型用法代码示例。如果您正苦于以下问题:Golang StructValue.FieldByName方法的具体用法?Golang StructValue.FieldByName怎么用?Golang StructValue.FieldByName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.StructValue
的用法示例。
在下文中一共展示了StructValue.FieldByName方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: makeVector3
// Makes a Vector3 StateValue. Panics if the StructValue fields do not match the vector.
func makeVector3(v *reflect.StructValue) *protocol.StateValue {
// If we panic here, struct layout was not as expected
x := v.FieldByName("X").(*reflect.FloatValue).Get()
y := v.FieldByName("Y").(*reflect.FloatValue).Get()
z := v.FieldByName("Z").(*reflect.FloatValue).Get()
vector3 := &protocol.Vector3{&x, &y, &z, nil}
sv := &protocol.StateValue{
Type: protocol.NewStateValue_Type(protocol.StateValue_VECTOR3),
Vector3Val: vector3,
}
return sv
}
示例2: translateMap
func (m *Model) translateMap(obj *reflect.StructValue) map[string]Value {
ret := make(map[string]Value)
for attr, typ := range m.attributes {
switch typ.(type) {
case *reflect.IntType:
ret[attr] = SysInt(obj.FieldByName(attr).(*reflect.IntValue).Get()).Value()
case *reflect.StringType:
ret[attr] = SysString(obj.FieldByName(attr).(*reflect.StringValue).Get()).Value()
case nil:
ret[attr] = new(_Null)
}
}
return ret
}
示例3: buildUpdateMap
func (m *Model) buildUpdateMap(st *reflect.StructValue, old map[string]Value) map[string]Value {
ret := make(map[string]Value)
for attr, typ := range m.attributes {
switch typ.(type) {
case *reflect.IntType:
if tmp := st.FieldByName(attr).(*reflect.IntValue).Get(); int(old[strings.ToLower(attr)].Int()) != tmp {
ret[attr] = SysInt(tmp).Value()
}
case *reflect.StringType:
if tmp := st.FieldByName(attr).(*reflect.StringValue).Get(); string(old[strings.ToLower(attr)].String()) != tmp {
ret[attr] = SysString(tmp).Value()
}
}
}
return ret
}
示例4: translateObjectValue
func (m *Model) translateObjectValue(v map[string]Value, p *reflect.StructValue) interface{} {
for lbl, _ := range m.Attributes() {
vl := v[strings.ToLower(lbl)]
switch vl.Kind() {
//TODO MakeZero ??
case IntKind:
tmp := reflect.NewValue(1).(*reflect.IntValue)
tmp.Set(int(vl.Int()))
p.FieldByName(lbl).SetValue(tmp)
case StringKind:
tmp := reflect.NewValue("").(*reflect.StringValue)
tmp.Set(string(vl.String()))
p.FieldByName(lbl).SetValue(tmp)
}
}
m.object_cache[int(v[strings.ToLower(m.identifier)].Int())] = v
return p.Interface()
}
示例5: object
// object consumes an object from d.data[d.off-1:], decoding into the value v.
// the first byte of the object ('{') has been read already.
func (d *decodeState) object(v reflect.Value) {
// Check for unmarshaler.
unmarshaler, pv := d.indirect(v, false)
if unmarshaler != nil {
d.off--
err := unmarshaler.UnmarshalJSON(d.next())
if err != nil {
d.error(err)
}
return
}
v = pv
// Decoding into nil interface? Switch to non-reflect code.
iv, ok := v.(*reflect.InterfaceValue)
if ok {
iv.Set(reflect.NewValue(d.objectInterface()))
return
}
// Check type of target: struct or map[string]T
var (
mv *reflect.MapValue
sv *reflect.StructValue
)
switch v := v.(type) {
case *reflect.MapValue:
// map must have string type
t := v.Type().(*reflect.MapType)
if t.Key() != reflect.Typeof("") {
d.saveError(&UnmarshalTypeError{"object", v.Type()})
break
}
mv = v
if mv.IsNil() {
mv.SetValue(reflect.MakeMap(t))
}
case *reflect.StructValue:
sv = v
default:
d.saveError(&UnmarshalTypeError{"object", v.Type()})
}
if mv == nil && sv == nil {
d.off--
d.next() // skip over { } in input
return
}
for {
// Read opening " of string key or closing }.
op := d.scanWhile(scanSkipSpace)
if op == scanEndObject {
// closing } - can only happen on first iteration.
break
}
if op != scanBeginLiteral {
d.error(errPhase)
}
// Read string key.
start := d.off - 1
op = d.scanWhile(scanContinue)
item := d.data[start : d.off-1]
key, ok := unquote(item)
if !ok {
d.error(errPhase)
}
// Figure out field corresponding to key.
var subv reflect.Value
if mv != nil {
subv = reflect.MakeZero(mv.Type().(*reflect.MapType).Elem())
} else {
// First try for field with that tag.
for i := 0; i < sv.NumField(); i++ {
f := sv.Type().(*reflect.StructType).Field(i)
if f.Tag == key {
subv = sv.Field(i)
break
}
}
if subv == nil {
// Second, exact match.
subv = sv.FieldByName(key)
if subv == nil {
// Third, case-insensitive match.
subv = sv.FieldByNameFunc(func(s string) bool { return matchName(key, s) })
}
}
}
// Read : before value.
if op == scanSkipSpace {
op = d.scanWhile(scanSkipSpace)
}
if op != scanObjectKey {
d.error(errPhase)
//.........这里部分代码省略.........
示例6: getId
func (m *Model) getId(st *reflect.StructValue) int {
return st.FieldByName(m.identifier).(*reflect.IntValue).Get()
}