1
0
mirror of https://github.com/golang/go synced 2024-11-20 04:54:44 -07:00

use FieldByName where possible.

R=rsc
DELTA=20  (0 added, 12 deleted, 8 changed)
OCL=31758
CL=31758
This commit is contained in:
Rob Pike 2009-07-16 21:24:30 -07:00
parent e1b8cb8cf0
commit 1737157189
3 changed files with 6 additions and 14 deletions

View File

@ -423,11 +423,8 @@ func getField(val reflect.Value, fieldname string) (reflect.Value, int) {
styp := sval.Type().(*reflect.StructType);
// look for field at the top level
for i := 0; i < styp.NumField(); i++ {
f := styp.Field(i);
if f.Name == fieldname {
return sval.Field(i), 0;
}
if field, ok := styp.FieldByName(fieldname); ok {
return sval.Field(field.Index), 0;
}
// look for field in anonymous fields

View File

@ -184,10 +184,8 @@ func (b *_StructBuilder) Key(k string) Builder {
}
if v, ok := reflect.Indirect(b.val).(*reflect.StructValue); ok {
t := v.Type().(*reflect.StructType);
for i := 0; i < t.NumField(); i++ {
if t.Field(i).Name == k {
return &_StructBuilder{ v.Field(i) }
}
if field, ok := t.FieldByName(k); ok {
return &_StructBuilder{ v.Field(field.Index) }
}
// Again, case-insensitive.
for i := 0; i < t.NumField(); i++ {

View File

@ -571,11 +571,8 @@ func (st *state) findVar(s string) reflect.Value {
data := reflect.Indirect(st.data);
typ, ok := data.Type().(*reflect.StructType);
if ok {
for i := 0; i < typ.NumField(); i++ {
f := typ.Field(i);
if f.Name == s {
return data.(*reflect.StructValue).Field(i)
}
if field, ok := typ.FieldByName(s); ok {
return data.(*reflect.StructValue).Field(field.Index)
}
}
return nil