Commit a93c5c85 authored by Rob Pike's avatar Rob Pike

add FieldByName to the interface of reflect.StructType

R=rsc
DELTA=34  (33 added, 0 deleted, 1 changed)
OCL=31752
CL=31754
parent 1399badb
......@@ -214,9 +214,18 @@ func TestAll(t *testing.T) { // TODO(r): wrap up better
styp := etyp.(*StructType);
f := styp.Field(0);
testType(t, 5, f.Type, "chan *int32");
f = styp.Field(1);
f, present := styp.FieldByName("d");
if !present {
t.Errorf("FieldByName says present field is absent");
}
testType(t, 6, f.Type, "float32");
f, present = styp.FieldByName("absent");
if present {
t.Errorf("FieldByName says absent field is present");
}
typ = Typeof(([32]int32)(nil));
testType(t, 7, typ, "[32]int32");
testType(t, 8, typ.(*ArrayType).Elem(), "int32");
......
......@@ -455,6 +455,7 @@ type StructField struct {
Type Type;
Tag string;
Offset uintptr;
Index int;
Anonymous bool;
}
......@@ -482,6 +483,29 @@ func (t *StructType) Field(i int) (f StructField) {
return;
}
// FieldByName returns the field with the provided name and a boolean to indicate
// that the field was found..
func (t *StructType) FieldByName(name string) (f StructField, present bool) {
for i, p := range t.fields {
if p.name == nil || *p.name != name {
continue;
}
f.Name = *p.name;
f.Type = toType(*p.typ);
if p.pkgPath != nil {
f.PkgPath = *p.pkgPath;
}
if p.tag != nil {
f.Tag = *p.tag;
}
f.Offset = p.offset;
f.Index = i;
present = true;
break;
}
return;
}
// NumField returns the number of struct fields.
func (t *StructType) NumField() int {
return len(t.fields);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment