本文整理汇总了Golang中reflect.StructValue.FieldByIndex方法的典型用法代码示例。如果您正苦于以下问题:Golang StructValue.FieldByIndex方法的具体用法?Golang StructValue.FieldByIndex怎么用?Golang StructValue.FieldByIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.StructValue
的用法示例。
在下文中一共展示了StructValue.FieldByIndex方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: unmarshalPaths
// unmarshalPaths walks down an XML structure looking for
// wanted paths, and calls unmarshal on them.
func (p *Parser) unmarshalPaths(sv *reflect.StructValue, paths map[string]pathInfo, path string, start *StartElement) os.Error {
if info, _ := paths[path]; info.complete {
return p.unmarshal(sv.FieldByIndex(info.fieldIdx), start)
}
for {
tok, err := p.Token()
if err != nil {
return err
}
switch t := tok.(type) {
case StartElement:
k := path + ">" + fieldName(t.Name.Local)
if _, found := paths[k]; found {
if err := p.unmarshalPaths(sv, paths, k, &t); err != nil {
return err
}
continue
}
if err := p.Skip(); err != nil {
return err
}
case EndElement:
return nil
}
}
panic("unreachable")
}
示例2: writeStruct
func (e *encodeState) writeStruct(v *reflect.StructValue) {
offset := e.beginDoc()
for name, f := range compileStruct(v.Type().(*reflect.StructType)) {
e.encodeValue(name, v.FieldByIndex(f.Index))
}
e.WriteByte(0)
e.endDoc(offset)
}
示例3: unmarshal
//.........这里部分代码省略.........
if _, ok := v.Interface().(Name); ok {
v.Set(reflect.NewValue(start.Name).(*reflect.StructValue))
break
}
sv = v
typ := sv.Type().(*reflect.StructType)
styp = typ
// Assign name.
if f, ok := typ.FieldByName("XMLName"); ok {
// Validate element name.
if f.Tag != "" {
tag := f.Tag
ns := ""
i := strings.LastIndex(tag, " ")
if i >= 0 {
ns, tag = tag[0:i], tag[i+1:]
}
if tag != start.Name.Local {
return UnmarshalError("expected element type <" + tag + "> but have <" + start.Name.Local + ">")
}
if ns != "" && ns != start.Name.Space {
e := "expected element <" + tag + "> in name space " + ns + " but have "
if start.Name.Space == "" {
e += "no name space"
} else {
e += start.Name.Space
}
return UnmarshalError(e)
}
}
// Save
v := sv.FieldByIndex(f.Index)
if _, ok := v.Interface().(Name); !ok {
return UnmarshalError(sv.Type().String() + " field XMLName does not have type xml.Name")
}
v.(*reflect.StructValue).Set(reflect.NewValue(start.Name).(*reflect.StructValue))
}
// Assign attributes.
// Also, determine whether we need to save character data or comments.
for i, n := 0, typ.NumField(); i < n; i++ {
f := typ.Field(i)
switch f.Tag {
case "attr":
strv, ok := sv.FieldByIndex(f.Index).(*reflect.StringValue)
if !ok {
return UnmarshalError(sv.Type().String() + " field " + f.Name + " has attr tag but is not type string")
}
// Look for attribute.
val := ""
k := strings.ToLower(f.Name)
for _, a := range start.Attr {
if fieldName(a.Name.Local) == k {
val = a.Value
break
}
}
strv.Set(val)
case "comment":
if saveComment == nil {
saveComment = sv.FieldByIndex(f.Index)
}
示例4: object
//.........这里部分代码省略.........
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 {
var f reflect.StructField
var ok bool
// First try for field with that tag.
st := sv.Type().(*reflect.StructType)
for i := 0; i < sv.NumField(); i++ {
f = st.Field(i)
if f.Tag == key {
ok = true
break
}
}
if !ok {
// Second, exact match.
f, ok = st.FieldByName(key)
}
if !ok {
// Third, case-insensitive match.
f, ok = st.FieldByNameFunc(func(s string) bool { return matchName(key, s) })
}
// Extract value; name must be exported.
if ok {
if f.PkgPath != "" {
d.saveError(&UnmarshalFieldError{key, st, f})
} else {
subv = sv.FieldByIndex(f.Index)
}
}
}
// Read : before value.
if op == scanSkipSpace {
op = d.scanWhile(scanSkipSpace)
}
if op != scanObjectKey {
d.error(errPhase)
}
// Read value.
d.value(subv)
// Write value back to map;
// if using struct, subv points into struct already.
if mv != nil {
mv.SetElem(reflect.NewValue(key), subv)
}
// Next token must be , or }.
op = d.scanWhile(scanSkipSpace)
if op == scanEndObject {
break
}
if op != scanObjectValue {
d.error(errPhase)
}
}
}
示例5: unmarshal
//.........这里部分代码省略.........
if _, ok := v.Interface().(Name); ok {
v.Set(reflect.NewValue(start.Name).(*reflect.StructValue))
break
}
sv = v
typ := sv.Type().(*reflect.StructType)
styp = typ
// Assign name.
if f, ok := typ.FieldByName("XMLName"); ok {
// Validate element name.
if f.Tag != "" {
tag := f.Tag
ns := ""
i := strings.LastIndex(tag, " ")
if i >= 0 {
ns, tag = tag[0:i], tag[i+1:]
}
if tag != start.Name.Local {
return UnmarshalError("expected element type <" + tag + "> but have <" + start.Name.Local + ">")
}
if ns != "" && ns != start.Name.Space {
e := "expected element <" + tag + "> in name space " + ns + " but have "
if start.Name.Space == "" {
e += "no name space"
} else {
e += start.Name.Space
}
return UnmarshalError(e)
}
}
// Save
v := sv.FieldByIndex(f.Index)
if _, ok := v.Interface().(Name); !ok {
return UnmarshalError(sv.Type().String() + " field XMLName does not have type xml.Name")
}
v.(*reflect.StructValue).Set(reflect.NewValue(start.Name).(*reflect.StructValue))
}
// Assign attributes.
// Also, determine whether we need to save character data or comments.
for i, n := 0, typ.NumField(); i < n; i++ {
f := typ.Field(i)
switch f.Tag {
case "attr":
strv, ok := sv.FieldByIndex(f.Index).(*reflect.StringValue)
if !ok {
return UnmarshalError(sv.Type().String() + " field " + f.Name + " has attr tag but is not type string")
}
// Look for attribute.
val := ""
k := strings.ToLower(f.Name)
for _, a := range start.Attr {
if fieldName(a.Name.Local) == k {
val = a.Value
break
}
}
strv.Set(val)
case "comment":
if saveComment == nil {
saveComment = sv.FieldByIndex(f.Index)
}