mirror of
https://github.com/golang/go
synced 2024-11-20 04:44:40 -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:
parent
e1b8cb8cf0
commit
1737157189
@ -423,11 +423,8 @@ func getField(val reflect.Value, fieldname string) (reflect.Value, int) {
|
|||||||
styp := sval.Type().(*reflect.StructType);
|
styp := sval.Type().(*reflect.StructType);
|
||||||
|
|
||||||
// look for field at the top level
|
// look for field at the top level
|
||||||
for i := 0; i < styp.NumField(); i++ {
|
if field, ok := styp.FieldByName(fieldname); ok {
|
||||||
f := styp.Field(i);
|
return sval.Field(field.Index), 0;
|
||||||
if f.Name == fieldname {
|
|
||||||
return sval.Field(i), 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// look for field in anonymous fields
|
// look for field in anonymous fields
|
||||||
|
@ -184,10 +184,8 @@ func (b *_StructBuilder) Key(k string) Builder {
|
|||||||
}
|
}
|
||||||
if v, ok := reflect.Indirect(b.val).(*reflect.StructValue); ok {
|
if v, ok := reflect.Indirect(b.val).(*reflect.StructValue); ok {
|
||||||
t := v.Type().(*reflect.StructType);
|
t := v.Type().(*reflect.StructType);
|
||||||
for i := 0; i < t.NumField(); i++ {
|
if field, ok := t.FieldByName(k); ok {
|
||||||
if t.Field(i).Name == k {
|
return &_StructBuilder{ v.Field(field.Index) }
|
||||||
return &_StructBuilder{ v.Field(i) }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Again, case-insensitive.
|
// Again, case-insensitive.
|
||||||
for i := 0; i < t.NumField(); i++ {
|
for i := 0; i < t.NumField(); i++ {
|
||||||
|
@ -571,11 +571,8 @@ func (st *state) findVar(s string) reflect.Value {
|
|||||||
data := reflect.Indirect(st.data);
|
data := reflect.Indirect(st.data);
|
||||||
typ, ok := data.Type().(*reflect.StructType);
|
typ, ok := data.Type().(*reflect.StructType);
|
||||||
if ok {
|
if ok {
|
||||||
for i := 0; i < typ.NumField(); i++ {
|
if field, ok := typ.FieldByName(s); ok {
|
||||||
f := typ.Field(i);
|
return data.(*reflect.StructValue).Field(field.Index)
|
||||||
if f.Name == s {
|
|
||||||
return data.(*reflect.StructValue).Field(i)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user