return true
}
switch v := v.(type) {
+ case *reflect.BoolValue:
+ return v.Get() == false;
case *reflect.StringValue:
return v.Get() == "";
case *reflect.StructValue:
}
// Look up a variable, up through the parent if necessary.
-func (t *Template) varValue(v *variableElement, st *state) reflect.Value {
- field := st.findVar(v.name);
+func (t *Template) varValue(name string, st *state) reflect.Value {
+ field := st.findVar(name);
if field == nil {
if st.parent == nil {
- t.execError(st, t.linenum, "name not found: %s", v.name)
+ t.execError(st, t.linenum, "name not found: %s", name)
}
- return t.varValue(v, st.parent);
+ return t.varValue(name, st.parent);
}
return field;
}
// If it has a formatter attached ({var|formatter}) run that too.
func (t *Template) writeVariable(v *variableElement, st *state) {
formatter := v.formatter;
- val := t.varValue(v, st).Interface();
+ val := t.varValue(v.name, st).Interface();
// is it in user-supplied map?
if t.fmap != nil {
if fn, ok := t.fmap[v.formatter]; ok {
// Execute a .section
func (t *Template) executeSection(s *sectionElement, st *state) {
// Find driver data for this section. It must be in the current struct.
- field := st.findVar(s.field);
+ field := t.varValue(s.field, st);
if field == nil {
t.execError(st, s.linenum, ".section: cannot find field %s in %s", s.field, reflect.Indirect(st.data).Type());
}
// Execute a .repeated section
func (t *Template) executeRepeated(r *repeatedElement, st *state) {
// Find driver data for this section. It must be in the current struct.
- field := st.findVar(r.field);
+ field := t.varValue(r.field, st);
if field == nil {
t.execError(st, r.linenum, ".repeated: cannot find field %s in %s", r.field, reflect.Indirect(st.data).Type());
}
emptystring string;
null []*T;
vec *vector.Vector;
+ true bool;
+ false bool;
}
var t1 = T{ "ItemNumber1", "ValueNumber1" }
out: "ItemNumber1=ValueNumber1\n"
},
+ &Test{
+ in: "{.section @ }\n"
+ "{innerT.item}={.section innerT}{.section value}{@}{.end}{.end}\n"
+ "{.end}",
+
+ out: "ItemNumber1=ValueNumber1\n"
+ },
+
// Formatters
&Test{
out: "\nheader\n"
},
+
+ &Test {
+ in: "{.section true}1{.or}2{.end}\n"
+ "{.section false}3{.or}4{.end}\n",
+
+ out: "1\n4\n"
+ },
}
func TestAll(t *testing.T) {
s.vec = vector.New(0);
s.vec.Push("elt1");
s.vec.Push("elt2");
+ s.true = true;
+ s.false = false;
var buf bytes.Buffer;
for i, test := range tests {