Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
72084068
Commit
72084068
authored
Jan 24, 2011
by
Roger Peppe
Committed by
Russ Cox
Jan 24, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
template: treat map keys as zero, not non-existent.
R=adg, r, hoka, rsc CC=golang-dev
https://golang.org/cl/4036045
parent
e7183e75
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
12 deletions
+34
-12
template.go
src/pkg/template/template.go
+4
-1
template_test.go
src/pkg/template/template_test.go
+30
-11
No files found.
src/pkg/template/template.go
View file @
72084068
...
...
@@ -622,7 +622,10 @@ func (t *Template) lookup(st *state, v reflect.Value, name string) reflect.Value
}
return
av
.
FieldByName
(
name
)
case
*
reflect
.
MapValue
:
return
av
.
Elem
(
reflect
.
NewValue
(
name
))
if
v
:=
av
.
Elem
(
reflect
.
NewValue
(
name
));
v
!=
nil
{
return
v
}
return
reflect
.
MakeZero
(
typ
.
(
*
reflect
.
MapType
)
.
Elem
())
default
:
return
nil
}
...
...
src/pkg/template/template_test.go
View file @
72084068
...
...
@@ -522,9 +522,27 @@ func TestMapDriverType(t *testing.T) {
t
.
Error
(
"unexpected execute error:"
,
err
)
}
s
:=
b
.
String
()
expected
:=
"template: Ahoy!"
if
s
!=
expected
{
t
.
Errorf
(
"failed passing string as data: expected %q got %q"
,
"template: Ahoy!"
,
s
)
expect
:=
"template: Ahoy!"
if
s
!=
expect
{
t
.
Errorf
(
"failed passing string as data: expected %q got %q"
,
expect
,
s
)
}
}
func
TestMapNoEntry
(
t
*
testing
.
T
)
{
mp
:=
make
(
map
[
string
]
int
)
tmpl
,
err
:=
Parse
(
"template: {notthere}!"
,
nil
)
if
err
!=
nil
{
t
.
Error
(
"unexpected parse error:"
,
err
)
}
var
b
bytes
.
Buffer
err
=
tmpl
.
Execute
(
mp
,
&
b
)
if
err
!=
nil
{
t
.
Error
(
"unexpected execute error:"
,
err
)
}
s
:=
b
.
String
()
expect
:=
"template: 0!"
if
s
!=
expect
{
t
.
Errorf
(
"failed passing string as data: expected %q got %q"
,
expect
,
s
)
}
}
...
...
@@ -539,8 +557,9 @@ func TestStringDriverType(t *testing.T) {
t
.
Error
(
"unexpected execute error:"
,
err
)
}
s
:=
b
.
String
()
if
s
!=
"template: hello"
{
t
.
Errorf
(
"failed passing string as data: expected %q got %q"
,
"template: hello"
,
s
)
expect
:=
"template: hello"
if
s
!=
expect
{
t
.
Errorf
(
"failed passing string as data: expected %q got %q"
,
expect
,
s
)
}
}
...
...
@@ -555,18 +574,18 @@ func TestTwice(t *testing.T) {
t
.
Error
(
"unexpected parse error:"
,
err
)
}
s
:=
b
.
String
()
tex
t
:=
"template: hello"
if
s
!=
tex
t
{
t
.
Errorf
(
"failed passing string as data: expected %q got %q"
,
tex
t
,
s
)
expec
t
:=
"template: hello"
if
s
!=
expec
t
{
t
.
Errorf
(
"failed passing string as data: expected %q got %q"
,
expec
t
,
s
)
}
err
=
tmpl
.
Execute
(
"hello"
,
&
b
)
if
err
!=
nil
{
t
.
Error
(
"unexpected parse error:"
,
err
)
}
s
=
b
.
String
()
text
+=
tex
t
if
s
!=
tex
t
{
t
.
Errorf
(
"failed passing string as data: expected %q got %q"
,
tex
t
,
s
)
expect
+=
expec
t
if
s
!=
expec
t
{
t
.
Errorf
(
"failed passing string as data: expected %q got %q"
,
expec
t
,
s
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment