]> Cypherpunks repositories - gostls13.git/commitdiff
dotted names
authorRob Pike <r@golang.org>
Fri, 31 Jul 2009 01:17:07 +0000 (18:17 -0700)
committerRob Pike <r@golang.org>
Fri, 31 Jul 2009 01:17:07 +0000 (18:17 -0700)
R=rsc
DELTA=28  (19 added, 0 deleted, 9 changed)
OCL=32550
CL=32554

src/pkg/rpc/debug.go
src/pkg/template/template.go
src/pkg/template/template_test.go

index 7cc79333ad8fa5b5af751dfbfeac542276b64f6c..3b927cf2475ccbc87d2361294a6a6a2756c57c8d 100644 (file)
@@ -33,8 +33,8 @@ const debugText =
                <th align=center>Method</th><th align=center>Calls</th>
                {.repeated section meth}
                        <tr>
-                       <td align=left font=fixed>{name}({.section m}{argType}, {replyType}) os.Error</td>
-                       <td align=center>{numCalls}</td>{.end}
+                       <td align=left font=fixed>{name}({m.argType}, {m.replyType}) os.Error</td>
+                       <td align=center>{m.numCalls}</td>
                        </tr>
                {.end}
                </table>
index 7c38b778fc8859167091a5fd493f15dc664b8e1d..dbbadeedfd7070104fa478e1a50b024a320a9523 100644 (file)
@@ -563,19 +563,27 @@ func (t *Template) parse() {
 // -- Execution
 
 // If the data for this template is a struct, find the named variable.
+// Names of the form a.b.c are walked down the data tree.
 // The special name "@" (the "cursor") denotes the current data.
 func (st *state) findVar(s string) reflect.Value {
        if s == "@" {
                return st.data
        }
        data := reflect.Indirect(st.data);
-       typ, ok := data.Type().(*reflect.StructType);
-       if ok {
-               if field, ok := typ.FieldByName(s); ok {
-                       return data.(*reflect.StructValue).Field(field.Index)
+       elems := strings.Split(s, ".", 0);
+       for i := 0; i < len(elems); i++ {
+               // Look up field; data must be a struct.
+               typ, ok := data.Type().(*reflect.StructType);
+               if !ok {
+                       return nil
                }
+               field, ok := typ.FieldByName(elems[i]);
+               if !ok {
+                       return nil
+               }
+               data = reflect.Indirect(data.(*reflect.StructValue).Field(field.Index));
        }
-       return nil
+       return data
 }
 
 // Is there no data to look at?
index 138978b6e55f772d7b792099169dafc6003b9a8a..7aeec6d37932e90d788398686385563d64152fd4 100644 (file)
@@ -27,6 +27,7 @@ type S struct {
        header string;
        integer int;
        raw string;
+       innerT T;
        data []T;
        pdata []*T;
        empty []*T;
@@ -199,6 +200,15 @@ var tests = []*Test {
                "ItemNumber2=ValueNumber2\n"
        },
 
+       // Nested names
+       &Test{
+               "{.section @ }\n"
+               "{innerT.item}={innerT.value}\n"
+               "{.end}",
+
+               "ItemNumber1=ValueNumber1\n"
+       },
+
        // Formatters
        &Test{
                "{.section pdata }\n"
@@ -232,6 +242,7 @@ func TestAll(t *testing.T) {
        s.header = "Header";
        s.integer = 77;
        s.raw = "&<>!@ #$%^";
+       s.innerT = t1;
        s.data = []T{ t1, t2 };
        s.pdata = []*T{ &t1, &t2 };
        s.empty = []*T{ };