Commit 8c540474 authored by Micah Stetson's avatar Micah Stetson Committed by Rob Pike

Fix a couple of bugs referencing data values in template.

Adds tests and fixes for two cases that fail with the current release.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/217115
parent 8d4fb690
...@@ -623,6 +623,9 @@ func (st *state) findVar(s string) reflect.Value { ...@@ -623,6 +623,9 @@ func (st *state) findVar(s string) reflect.Value {
if data == nil { if data == nil {
return nil return nil
} }
if intf, ok := data.(*reflect.InterfaceValue); ok {
data = intf.Elem()
}
switch typ := data.Type().(type) { switch typ := data.Type().(type) {
case *reflect.StructType: case *reflect.StructType:
...@@ -706,6 +709,8 @@ func empty(v reflect.Value) bool { ...@@ -706,6 +709,8 @@ func empty(v reflect.Value) bool {
return v.Get() == "" return v.Get() == ""
case *reflect.StructValue: case *reflect.StructValue:
return false return false
case *reflect.MapValue:
return false
case *reflect.ArrayValue: case *reflect.ArrayValue:
return v.Len() == 0 return v.Len() == 0
case *reflect.SliceValue: case *reflect.SliceValue:
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"container/vector" "container/vector"
"fmt" "fmt"
"io" "io"
"json"
"testing" "testing"
) )
...@@ -40,6 +41,7 @@ type S struct { ...@@ -40,6 +41,7 @@ type S struct {
true bool true bool
false bool false bool
mp map[string]string mp map[string]string
json interface{}
innermap U innermap U
stringmap map[string]string stringmap map[string]string
bytes []byte bytes []byte
...@@ -340,6 +342,16 @@ var tests = []*Test{ ...@@ -340,6 +342,16 @@ var tests = []*Test{
out: "55\n", out: "55\n",
}, },
&Test{
in: "{.section innermap}{.section mp}{innerkey}{.end}{.end}\n",
out: "55\n",
},
&Test{
in: "{.section json}{.repeated section maps}{a}{b}{.end}{.end}\n",
out: "1234\n",
},
&Test{ &Test{
in: "{stringmap.stringkey1}\n", in: "{stringmap.stringkey1}\n",
...@@ -391,6 +403,7 @@ func TestAll(t *testing.T) { ...@@ -391,6 +403,7 @@ func TestAll(t *testing.T) {
s.false = false s.false = false
s.mp = make(map[string]string) s.mp = make(map[string]string)
s.mp["mapkey"] = "Ahoy!" s.mp["mapkey"] = "Ahoy!"
s.json, _ = json.Decode("{\"maps\":[{\"a\":1,\"b\":2},{\"a\":3,\"b\":4}]}")
s.innermap.mp = make(map[string]int) s.innermap.mp = make(map[string]int)
s.innermap.mp["innerkey"] = 55 s.innermap.mp["innerkey"] = 55
s.stringmap = make(map[string]string) s.stringmap = make(map[string]string)
......
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