summaryrefslogtreecommitdiff
path: root/src/pkg/datafmt/datafmt.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2009-08-05 15:56:44 -0700
committerRobert Griesemer <gri@golang.org>2009-08-05 15:56:44 -0700
commit8b5f87a023f0ac728c619e34d9295a1e664f197c (patch)
treeadc5c7fd097b41c24adee9429bb45cd37883a9ff /src/pkg/datafmt/datafmt.go
parentd783faf961422b4832dcda901fae99402af4a8a4 (diff)
downloadgolang-8b5f87a023f0ac728c619e34d9295a1e664f197c.tar.gz
- FieldByName lookup through anonymous fields
- FieldByIndex - changed StructField.Index type from int -> []int - adjustments to reflect clients R=rsc,r DELTA=336 (263 added, 47 deleted, 26 changed) OCL=32731 CL=32802
Diffstat (limited to 'src/pkg/datafmt/datafmt.go')
-rw-r--r--src/pkg/datafmt/datafmt.go54
1 files changed, 7 insertions, 47 deletions
diff --git a/src/pkg/datafmt/datafmt.go b/src/pkg/datafmt/datafmt.go
index be5575d57..60dde3bdb 100644
--- a/src/pkg/datafmt/datafmt.go
+++ b/src/pkg/datafmt/datafmt.go
@@ -410,49 +410,6 @@ func (s *State) error(msg string) {
}
-// getField searches in val, which must be a struct, for a field
-// with the given name. It returns the value and the embedded depth
-// where it was found.
-//
-func getField(val reflect.Value, fieldname string) (reflect.Value, int) {
- // do we have a struct in the first place?
- sval, ok := val.(*reflect.StructValue);
- if !ok {
- return nil, 0;
- }
- styp := sval.Type().(*reflect.StructType);
-
- // look for field at the top level
- if field, ok := styp.FieldByName(fieldname); ok {
- return sval.Field(field.Index), 0;
- }
-
- // look for field in anonymous fields
- var field reflect.Value;
- level := 1000; // infinity (no struct has that many levels)
- for i := 0; i < styp.NumField(); i++ {
- f := styp.Field(i);
- if f.Anonymous {
- f, l := getField(sval.Field(i), fieldname);
- // keep the most shallow field
- if f != nil {
- switch {
- case l < level:
- field, level = f, l;
- case l == level:
- // more than one field at the same level,
- // possibly an error unless there is a more
- // shallow field found later
- field = nil;
- }
- }
- }
- }
-
- return field, level + 1;
-}
-
-
// TODO At the moment, unnamed types are simply mapped to the default
// names below. For instance, all unnamed arrays are mapped to
// 'array' which is not really sufficient. Eventually one may want
@@ -613,10 +570,13 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool {
default:
// value is value of named field
- field, _ := getField(value, t.fieldName);
- if field == nil {
- // TODO consider just returning false in this case
- s.error(fmt.Sprintf("error: no field `%s` in `%s`", t.fieldName, value.Type()));
+ var field reflect.Value;
+ if sval, ok := value.(*reflect.StructValue); ok {
+ field = sval.FieldByName(t.fieldName);
+ if field == nil {
+ // TODO consider just returning false in this case
+ s.error(fmt.Sprintf("error: no field `%s` in `%s`", t.fieldName, value.Type()));
+ }
}
value = field;
}