Skip to content

Commit 7ab1fda

Browse files
committed
Specify error message when trying to lookup value of an unexported field
1 parent 679ff5e commit 7ab1fda

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

mustache.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,13 @@ func call(v reflect.Value, method reflect.Method) reflect.Value {
369369

370370
// Evaluate interfaces and pointers looking for a value that can look up the name, via a
371371
// struct field, method, or map key, and return the result of the lookup.
372-
func lookup(contextChain *vector.Vector, name string) (reflect.Value) {
372+
func lookup(contextChain *vector.Vector, name string) reflect.Value {
373373
defer func() {
374374
if r := recover(); r != nil {
375-
fmt.Printf("Panic while looking up %s - %s", name, r)
375+
fmt.Printf("Panic while looking up %q: %s\n", name, r)
376376
}
377377
}()
378-
378+
379379
Outer:
380380
for i := contextChain.Len() - 1; i >= 0; i-- {
381381
v := contextChain.At(i).(reflect.Value)
@@ -491,6 +491,12 @@ func renderElement(element interface{}, contextChain *vector.Vector, buf io.Writ
491491
case *textElement:
492492
buf.Write(elem.text)
493493
case *varElement:
494+
defer func() {
495+
if r := recover(); r != nil {
496+
fmt.Printf("Panic while looking up %q: %s\n", elem.name, r)
497+
}
498+
}()
499+
494500
val := lookup(contextChain, elem.name)
495501
if val.IsValid() {
496502
if elem.raw {

mustache_test.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ type Test struct {
1515
}
1616

1717
type Data struct {
18-
a bool
19-
b string
18+
A bool
19+
B string
2020
}
2121

2222
type User struct {
@@ -48,7 +48,6 @@ func (u *User) Func5() (*settings, os.Error) {
4848
return &settings{true}, nil
4949
}
5050

51-
5251
func (u *User) Func6() (*vector.Vector, os.Error) {
5352
var v vector.Vector
5453
v.Push(&settings{true})
@@ -72,6 +71,7 @@ func makeVector(n int) *vector.Vector {
7271
}
7372

7473
var tests = []Test{
74+
7575
{`hello world`, nil, "hello world"},
7676
{`hello {{name}}`, map[string]string{"name": "world"}, "hello world"},
7777
{`{{var}}`, map[string]string{"var": "5 > 2"}, "5 > 2"},
@@ -89,21 +89,21 @@ var tests = []Test{
8989
{`{{#has}}{{/has}}`, &User{"Mike", 1}, ""},
9090

9191
//section tests
92-
{`{{#a}}{{b}}{{/a}}`, Data{true, "hello"}, "hello"},
93-
{`{{#a}}{{{b}}}{{/a}}`, Data{true, "5 > 2"}, "5 > 2"},
94-
{`{{#a}}{{b}}{{/a}}`, Data{true, "5 > 2"}, "5 > 2"},
95-
{`{{#a}}{{b}}{{/a}}`, Data{false, "hello"}, ""},
92+
{`{{#A}}{{B}}{{/A}}`, Data{true, "hello"}, "hello"},
93+
{`{{#A}}{{{B}}}{{/A}}`, Data{true, "5 > 2"}, "5 > 2"},
94+
{`{{#A}}{{B}}{{/A}}`, Data{true, "5 > 2"}, "5 > 2"},
95+
{`{{#A}}{{B}}{{/A}}`, Data{false, "hello"}, ""},
9696
{`{{a}}{{#b}}{{b}}{{/b}}{{c}}`, map[string]string{"a": "a", "b": "b", "c": "c"}, "abc"},
97-
{`{{#a}}{{b}}{{/a}}`, struct {
98-
a []struct {
99-
b string
97+
{`{{#A}}{{B}}{{/A}}`, struct {
98+
A []struct {
99+
B string
100100
}
101101
}{[]struct {
102-
b string
102+
B string
103103
}{{"a"}, {"b"}, {"c"}}},
104104
"abc",
105105
},
106-
{`{{#a}}{{b}}{{/a}}`, struct{ a []map[string]string }{[]map[string]string{{"b": "a"}, {"b": "b"}, {"b": "c"}}}, "abc"},
106+
{`{{#A}}{{b}}{{/A}}`, struct{ A []map[string]string }{[]map[string]string{{"b": "a"}, {"b": "b"}, {"b": "c"}}}, "abc"},
107107

108108
{`{{#users}}{{Name}}{{/users}}`, map[string]interface{}{"users": []User{{"Mike", 1}}}, "Mike"},
109109

@@ -184,14 +184,13 @@ func TestSectionPartial(t *testing.T) {
184184
}
185185

186186
func TestMultiContext(t *testing.T) {
187-
output := Render(`{{hello}} {{world}}`, map[string]string{"hello": "hello"}, struct{ world string }{"world"})
188-
output2 := Render(`{{hello}} {{world}}`, struct{ world string }{"world"}, map[string]string{"hello": "hello"})
187+
output := Render(`{{hello}} {{World}}`, map[string]string{"hello": "hello"}, struct{ World string }{"world"})
188+
output2 := Render(`{{hello}} {{World}}`, struct{ World string }{"world"}, map[string]string{"hello": "hello"})
189189
if output != "hello world" || output2 != "hello world" {
190190
t.Fatalf("TestMultiContext expected %q got %q", "hello world", output)
191191
}
192192
}
193193

194-
195194
var malformed = []Test{
196195
{`{{#a}}{{}}{{/a}}`, Data{true, "hello"}, "empty tag"},
197196
{`{{}}`, nil, "empty tag"},

0 commit comments

Comments
 (0)