Commit 238274ed authored by Rob Pike's avatar Rob Pike

exp/template: handle pointers and values in empty interfaces.

R=golang-dev, r, mattn.jp
CC=golang-dev
https://golang.org/cl/4664064
parent 8d538c6d
...@@ -245,8 +245,9 @@ func isExported(name string) bool { ...@@ -245,8 +245,9 @@ func isExported(name string) bool {
} }
func (s *state) evalField(data reflect.Value, fieldName string) reflect.Value { func (s *state) evalField(data reflect.Value, fieldName string) reflect.Value {
for data.Kind() == reflect.Ptr { var isNil bool
data = reflect.Indirect(data) if data, isNil = indirect(data); isNil {
s.errorf("%s is nil pointer", fieldName)
} }
switch data.Kind() { switch data.Kind() {
case reflect.Struct: case reflect.Struct:
...@@ -270,7 +271,7 @@ func (s *state) evalFieldOrCall(data reflect.Value, fieldName string, args []nod ...@@ -270,7 +271,7 @@ func (s *state) evalFieldOrCall(data reflect.Value, fieldName string, args []nod
return s.evalCall(data, function, fieldName, false, args, final) return s.evalCall(data, function, fieldName, false, args, final)
} }
ptr := data ptr := data
for data.Kind() == reflect.Ptr { for data.Kind() == reflect.Ptr && !data.IsNil() {
ptr, data = data, reflect.Indirect(data) ptr, data = data, reflect.Indirect(data)
} }
// Is it a method? We use the pointer because it has value methods too. // Is it a method? We use the pointer because it has value methods too.
...@@ -475,16 +476,30 @@ func (s *state) evalEmptyInterface(data reflect.Value, typ reflect.Type, n node) ...@@ -475,16 +476,30 @@ func (s *state) evalEmptyInterface(data reflect.Value, typ reflect.Type, n node)
panic("not reached") panic("not reached")
} }
// indirect returns the item at the end of indirection, and a bool to indicate if it's nil.
func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
for v.Kind() == reflect.Ptr {
if v.IsNil() {
return v, true
}
v = v.Elem()
}
return v, false
}
// printValue writes the textual representation of the value to the output of // printValue writes the textual representation of the value to the output of
// the template. // the template.
func (s *state) printValue(n node, v reflect.Value) { func (s *state) printValue(n node, v reflect.Value) {
if !v.IsValid() { if !v.IsValid() {
fmt.Fprint(s.wr, "<no value>")
return return
} }
switch v.Kind() { switch v.Kind() {
case reflect.Ptr: case reflect.Ptr:
if v.IsNil() { var isNil bool
s.errorf("%s: nil value", n) if v, isNil = indirect(v); isNil {
fmt.Fprint(s.wr, "<nil>")
return
} }
case reflect.Chan, reflect.Func, reflect.Interface: case reflect.Chan, reflect.Func, reflect.Interface:
s.errorf("can't print %s of type %s", n, v.Type()) s.errorf("can't print %s of type %s", n, v.Type())
......
...@@ -32,8 +32,51 @@ type T struct { ...@@ -32,8 +32,51 @@ type T struct {
MSIone map[string]int // one element, for deterministic output MSIone map[string]int // one element, for deterministic output
MSIEmpty map[string]int MSIEmpty map[string]int
SMSI []map[string]int SMSI []map[string]int
// Empty interface; used to see if we can dig inside one. // Empty interfaces; used to see if we can dig inside one.
EmptyInterface interface{} Empty0 interface{} // nil
Empty1 interface{}
Empty2 interface{}
Empty3 interface{}
Empty4 interface{}
// Pointers
PI *int
PSI *[]int
NIL *int
}
var tVal = &T{
I: 17,
U16: 16,
X: "x",
U: &U{"v"},
SI: []int{3, 4, 5},
SB: []bool{true, false},
MSI: map[string]int{"one": 1, "two": 2, "three": 3},
MSIone: map[string]int{"one": 1},
SMSI: []map[string]int{
{"one": 1, "two": 2},
{"eleven": 11, "twelve": 12},
},
Empty1: 3,
Empty2: "empty2",
Empty3: []int{7, 8},
Empty4: &U{"v"},
PI: newInt(23),
PSI: newIntSlice(21, 22, 23),
}
// Helpers for creation.
func newInt(n int) *int {
p := new(int)
*p = n
return p
}
func newIntSlice(n ...int) *[]int {
p := new([]int)
*p = make([]int, len(n))
copy(*p, n)
return p
} }
// Simple methods with and without arguments. // Simple methods with and without arguments.
...@@ -81,22 +124,6 @@ type U struct { ...@@ -81,22 +124,6 @@ type U struct {
V string V string
} }
var tVal = &T{
I: 17,
U16: 16,
X: "x",
U: &U{"v"},
SI: []int{3, 4, 5},
SB: []bool{true, false},
MSI: map[string]int{"one": 1, "two": 2, "three": 3},
MSIone: map[string]int{"one": 1},
SMSI: []map[string]int{
{"one": 1, "two": 2},
{"eleven": 11, "twelve": 12},
},
EmptyInterface: []int{7, 8},
}
type execTest struct { type execTest struct {
name string name string
input string input string
...@@ -109,9 +136,11 @@ var execTests = []execTest{ ...@@ -109,9 +136,11 @@ var execTests = []execTest{
// Trivial cases. // Trivial cases.
{"empty", "", "", nil, true}, {"empty", "", "", nil, true},
{"text", "some text", "some text", nil, true}, {"text", "some text", "some text", nil, true},
// Fields of structs. // Fields of structs.
{".X", "-{{.X}}-", "-x-", tVal, true}, {".X", "-{{.X}}-", "-x-", tVal, true},
{".U.V", "-{{.U.V}}-", "-v-", tVal, true}, {".U.V", "-{{.U.V}}-", "-v-", tVal, true},
// Dots of all kinds to test basic evaluation. // Dots of all kinds to test basic evaluation.
{"dot int", "<{{.}}>", "<13>", 13, true}, {"dot int", "<{{.}}>", "<13>", 13, true},
{"dot uint", "<{{.}}>", "<14>", uint(14), true}, {"dot uint", "<{{.}}>", "<14>", uint(14), true},
...@@ -125,14 +154,30 @@ var execTests = []execTest{ ...@@ -125,14 +154,30 @@ var execTests = []execTest{
a int a int
b string b string
}{7, "seven"}, true}, }{7, "seven"}, true},
// Pointers.
{"*int", "{{.PI}}", "23", tVal, true},
{"*[]int", "{{.PSI}}", "[21 22 23]", tVal, true},
{"*[]int[1]", "{{index .PSI 1}}", "22", tVal, true},
{"NIL", "{{.NIL}}", "<nil>", tVal, true},
// Emtpy interfaces holding values.
{"empty nil", "{{.Empty0}}", "<no value>", tVal, true},
{"empty with int", "{{.Empty1}}", "3", tVal, true},
{"empty with string", "{{.Empty2}}", "empty2", tVal, true},
{"empty with slice", "{{.Empty3}}", "[7 8]", tVal, true},
{"empty with struct", "{{.Empty4}}", "{v}", tVal, true},
// Method calls. // Method calls.
{".Method0", "-{{.Method0}}-", "-resultOfMethod0-", tVal, true}, {".Method0", "-{{.Method0}}-", "-resultOfMethod0-", tVal, true},
{".Method1(1234)", "-{{.Method1 1234}}-", "-1234-", tVal, true}, {".Method1(1234)", "-{{.Method1 1234}}-", "-1234-", tVal, true},
{".Method1(.I)", "-{{.Method1 .I}}-", "-17-", tVal, true}, {".Method1(.I)", "-{{.Method1 .I}}-", "-17-", tVal, true},
{".Method2(3, .X)", "-{{.Method2 3 .X}}-", "-Method2: 3 x-", tVal, true}, {".Method2(3, .X)", "-{{.Method2 3 .X}}-", "-Method2: 3 x-", tVal, true},
{".Method2(.U16, `str`)", "-{{.Method2 .U16 `str`}}-", "-Method2: 16 str-", tVal, true}, {".Method2(.U16, `str`)", "-{{.Method2 .U16 `str`}}-", "-Method2: 16 str-", tVal, true},
// Pipelines. // Pipelines.
{"pipeline", "-{{.Method0 | .Method2 .U16}}-", "-Method2: 16 resultOfMethod0-", tVal, true}, {"pipeline", "-{{.Method0 | .Method2 .U16}}-", "-Method2: 16 resultOfMethod0-", tVal, true},
// If. // If.
{"if true", "{{if true}}TRUE{{end}}", "TRUE", tVal, true}, {"if true", "{{if true}}TRUE{{end}}", "TRUE", tVal, true},
{"if false", "{{if false}}TRUE{{else}}FALSE{{end}}", "FALSE", tVal, true}, {"if false", "{{if false}}TRUE{{else}}FALSE{{end}}", "FALSE", tVal, true},
...@@ -148,6 +193,7 @@ var execTests = []execTest{ ...@@ -148,6 +193,7 @@ var execTests = []execTest{
{"if slice", "{{if .SI}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true}, {"if slice", "{{if .SI}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true},
{"if emptymap", "{{if .MSIEmpty}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, {"if emptymap", "{{if .MSIEmpty}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
{"if map", "{{if .MSI}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true}, {"if map", "{{if .MSI}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true},
// Printf. // Printf.
{"printf", `{{printf "hello, printf"}}`, "hello, printf", tVal, true}, {"printf", `{{printf "hello, printf"}}`, "hello, printf", tVal, true},
{"printf int", `{{printf "%04x" 127}}`, "007f", tVal, true}, {"printf int", `{{printf "%04x" 127}}`, "007f", tVal, true},
...@@ -158,19 +204,23 @@ var execTests = []execTest{ ...@@ -158,19 +204,23 @@ var execTests = []execTest{
{"printf field", `{{printf "%s" .U.V}}`, "v", tVal, true}, {"printf field", `{{printf "%s" .U.V}}`, "v", tVal, true},
{"printf method", `{{printf "%s" .Method0}}`, "resultOfMethod0", tVal, true}, {"printf method", `{{printf "%s" .Method0}}`, "resultOfMethod0", tVal, true},
{"printf lots", `{{printf "%d %s %g %s" 127 "hello" 7-3i .Method0}}`, "127 hello (7-3i) resultOfMethod0", tVal, true}, {"printf lots", `{{printf "%d %s %g %s" 127 "hello" 7-3i .Method0}}`, "127 hello (7-3i) resultOfMethod0", tVal, true},
// HTML. // HTML.
{"html", `{{html "<script>alert(\"XSS\");</script>"}}`, {"html", `{{html "<script>alert(\"XSS\");</script>"}}`,
"&lt;script&gt;alert(&#34;XSS&#34;);&lt;/script&gt;", nil, true}, "&lt;script&gt;alert(&#34;XSS&#34;);&lt;/script&gt;", nil, true},
{"html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`, {"html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`,
"&lt;script&gt;alert(&#34;XSS&#34;);&lt;/script&gt;", nil, true}, "&lt;script&gt;alert(&#34;XSS&#34;);&lt;/script&gt;", nil, true},
// JS.
// JavaScript.
{"js", `{{js .}}`, `It\'d be nice.`, `It'd be nice.`, true}, {"js", `{{js .}}`, `It\'d be nice.`, `It'd be nice.`, true},
// Booleans // Booleans
{"not", "{{not true}} {{not false}}", "false true", nil, true}, {"not", "{{not true}} {{not false}}", "false true", nil, true},
{"and", "{{and 0 0}} {{and 1 0}} {{and 0 1}} {{and 1 1}}", "false false false true", nil, true}, {"and", "{{and 0 0}} {{and 1 0}} {{and 0 1}} {{and 1 1}}", "false false false true", nil, true},
{"or", "{{or 0 0}} {{or 1 0}} {{or 0 1}} {{or 1 1}}", "false true true true", nil, true}, {"or", "{{or 0 0}} {{or 1 0}} {{or 0 1}} {{or 1 1}}", "false true true true", nil, true},
{"boolean if", "{{if and true 1 `hi`}}TRUE{{else}}FALSE{{end}}", "TRUE", tVal, true}, {"boolean if", "{{if and true 1 `hi`}}TRUE{{else}}FALSE{{end}}", "TRUE", tVal, true},
{"boolean if not", "{{if and true 1 `hi` | not}}TRUE{{else}}FALSE{{end}}", "FALSE", nil, true}, {"boolean if not", "{{if and true 1 `hi` | not}}TRUE{{else}}FALSE{{end}}", "FALSE", nil, true},
// Indexing. // Indexing.
{"slice[0]", "{{index .SI 0}}", "3", tVal, true}, {"slice[0]", "{{index .SI 0}}", "3", tVal, true},
{"slice[1]", "{{index .SI 1}}", "4", tVal, true}, {"slice[1]", "{{index .SI 1}}", "4", tVal, true},
...@@ -181,6 +231,7 @@ var execTests = []execTest{ ...@@ -181,6 +231,7 @@ var execTests = []execTest{
{"map[NO]", "{{index .MSI `XXX`}}", "", tVal, false}, {"map[NO]", "{{index .MSI `XXX`}}", "", tVal, false},
{"map[WRONG]", "{{index .MSI 10}}", "", tVal, false}, {"map[WRONG]", "{{index .MSI 10}}", "", tVal, false},
{"double index", "{{index .SMSI 1 `eleven`}}", "11", tVal, true}, {"double index", "{{index .SMSI 1 `eleven`}}", "11", tVal, true},
// With. // With.
{"with true", "{{with true}}{{.}}{{end}}", "true", tVal, true}, {"with true", "{{with true}}{{.}}{{end}}", "true", tVal, true},
{"with false", "{{with false}}{{.}}{{else}}FALSE{{end}}", "FALSE", tVal, true}, {"with false", "{{with false}}{{.}}{{else}}FALSE{{end}}", "FALSE", tVal, true},
...@@ -196,6 +247,8 @@ var execTests = []execTest{ ...@@ -196,6 +247,8 @@ var execTests = []execTest{
{"with slice", "{{with .SI}}{{.}}{{else}}EMPTY{{end}}", "[3 4 5]", tVal, true}, {"with slice", "{{with .SI}}{{.}}{{else}}EMPTY{{end}}", "[3 4 5]", tVal, true},
{"with emptymap", "{{with .MSIEmpty}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, {"with emptymap", "{{with .MSIEmpty}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
{"with map", "{{with .MSIone}}{{.}}{{else}}EMPTY{{end}}", "map[one:1]", tVal, true}, {"with map", "{{with .MSIone}}{{.}}{{else}}EMPTY{{end}}", "map[one:1]", tVal, true},
{"with empty interface, struct field", "{{with .Empty4}}{{.V}}{{end}}", "v", tVal, true},
// Range. // Range.
{"range []int", "{{range .SI}}-{{.}}-{{end}}", "-3--4--5-", tVal, true}, {"range []int", "{{range .SI}}-{{.}}-{{end}}", "-3--4--5-", tVal, true},
{"range empty no else", "{{range .SIEmpty}}-{{.}}-{{end}}", "", tVal, true}, {"range empty no else", "{{range .SIEmpty}}-{{.}}-{{end}}", "", tVal, true},
...@@ -207,7 +260,8 @@ var execTests = []execTest{ ...@@ -207,7 +260,8 @@ var execTests = []execTest{
{"range empty map no else", "{{range .MSIEmpty}}-{{.}}-{{end}}", "", tVal, true}, {"range empty map no else", "{{range .MSIEmpty}}-{{.}}-{{end}}", "", tVal, true},
{"range map else", "{{range .MSI | .MSort}}-{{.}}-{{else}}EMPTY{{end}}", "-one--three--two-", tVal, true}, {"range map else", "{{range .MSI | .MSort}}-{{.}}-{{else}}EMPTY{{end}}", "-one--three--two-", tVal, true},
{"range empty map else", "{{range .MSIEmpty}}-{{.}}-{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, {"range empty map else", "{{range .MSIEmpty}}-{{.}}-{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
{"range empty interface", "{{range .EmptyInterface}}-{{.}}-{{else}}EMPTY{{end}}", "-7--8-", tVal, true}, {"range empty interface", "{{range .Empty3}}-{{.}}-{{else}}EMPTY{{end}}", "-7--8-", tVal, true},
// Error handling. // Error handling.
{"error method, error", "{{.EPERM true}}", "", tVal, false}, {"error method, error", "{{.EPERM true}}", "", tVal, false},
{"error method, no error", "{{.EPERM false}}", "false", tVal, true}, {"error method, no error", "{{.EPERM false}}", "false", tVal, true},
......
...@@ -83,6 +83,10 @@ func index(item interface{}, indices ...interface{}) (interface{}, os.Error) { ...@@ -83,6 +83,10 @@ func index(item interface{}, indices ...interface{}) (interface{}, os.Error) {
v := reflect.ValueOf(item) v := reflect.ValueOf(item)
for _, i := range indices { for _, i := range indices {
index := reflect.ValueOf(i) index := reflect.ValueOf(i)
var isNil bool
if v, isNil = indirect(v); isNil {
return nil, fmt.Errorf("index of nil pointer")
}
switch v.Kind() { switch v.Kind() {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
var x int64 var x int64
......
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