Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
180541b2
Commit
180541b2
authored
Feb 28, 2012
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
text/template: fix redefinition bugs
R=golang-dev, adg CC=golang-dev
https://golang.org/cl/5696087
parent
69015b6f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
22 additions
and
9 deletions
+22
-9
src/pkg/text/template/multi_test.go
src/pkg/text/template/multi_test.go
+6
-0
src/pkg/text/template/parse/parse.go
src/pkg/text/template/parse/parse.go
+2
-0
src/pkg/text/template/parse/parse_test.go
src/pkg/text/template/parse/parse_test.go
+3
-0
src/pkg/text/template/template.go
src/pkg/text/template/template.go
+11
-9
No files found.
src/pkg/text/template/multi_test.go
View file @
180541b2
...
...
@@ -265,6 +265,12 @@ func TestRedefinition(t *testing.T) {
if
tmpl
,
err
=
New
(
"tmpl1"
)
.
Parse
(
`{{define "test"}}foo{{end}}`
);
err
!=
nil
{
t
.
Fatalf
(
"parse 1: %v"
,
err
)
}
if
_
,
err
=
tmpl
.
Parse
(
`{{define "test"}}bar{{end}}`
);
err
==
nil
{
t
.
Fatal
(
"expected error"
)
}
if
!
strings
.
Contains
(
err
.
Error
(),
"redefinition"
)
{
t
.
Fatalf
(
"expected redefinition error; got %v"
,
err
)
}
if
_
,
err
=
tmpl
.
New
(
"tmpl2"
)
.
Parse
(
`{{define "test"}}bar{{end}}`
);
err
==
nil
{
t
.
Fatal
(
"expected error"
)
}
...
...
src/pkg/text/template/parse/parse.go
View file @
180541b2
...
...
@@ -193,6 +193,8 @@ func (t *Tree) add(treeSet map[string]*Tree) {
// IsEmptyTree reports whether this tree (node) is empty of everything but space.
func
IsEmptyTree
(
n
Node
)
bool
{
switch
n
:=
n
.
(
type
)
{
case
nil
:
return
true
case
*
ActionNode
:
case
*
IfNode
:
case
*
ListNode
:
...
...
src/pkg/text/template/parse/parse_test.go
View file @
180541b2
...
...
@@ -287,6 +287,9 @@ var isEmptyTests = []isEmptyTest{
}
func
TestIsEmpty
(
t
*
testing
.
T
)
{
if
!
IsEmptyTree
(
nil
)
{
t
.
Errorf
(
"nil tree is not empty"
)
}
for
_
,
test
:=
range
isEmptyTests
{
tree
,
err
:=
New
(
"root"
)
.
Parse
(
test
.
input
,
""
,
""
,
make
(
map
[
string
]
*
Tree
),
nil
)
if
err
!=
nil
{
...
...
src/pkg/text/template/template.go
View file @
180541b2
...
...
@@ -178,10 +178,11 @@ func (t *Template) Parse(text string) (*Template, error) {
tmpl
=
t
.
New
(
name
)
}
// Even if t == tmpl, we need to install it in the common.tmpl map.
if
err
:=
t
.
associate
(
tmpl
);
err
!=
nil
{
if
replace
,
err
:=
t
.
associate
(
tmpl
,
tree
);
err
!=
nil
{
return
nil
,
err
}
else
if
replace
{
tmpl
.
Tree
=
tree
}
tmpl
.
Tree
=
tree
tmpl
.
leftDelim
=
t
.
leftDelim
tmpl
.
rightDelim
=
t
.
rightDelim
}
...
...
@@ -191,22 +192,23 @@ func (t *Template) Parse(text string) (*Template, error) {
// associate installs the new template into the group of templates associated
// with t. It is an error to reuse a name except to overwrite an empty
// template. The two are already known to share the common structure.
func
(
t
*
Template
)
associate
(
new
*
Template
)
error
{
// The boolean return value reports wither to store this tree as t.Tree.
func
(
t
*
Template
)
associate
(
new
*
Template
,
tree
*
parse
.
Tree
)
(
bool
,
error
)
{
if
new
.
common
!=
t
.
common
{
panic
(
"internal error: associate not common"
)
}
name
:=
new
.
name
if
old
:=
t
.
tmpl
[
name
];
old
!=
nil
{
oldIsEmpty
:=
parse
.
IsEmptyTree
(
old
.
Root
)
newIsEmpty
:=
new
.
Tree
!=
nil
&&
parse
.
IsEmptyTree
(
new
.
Root
)
if
!
oldIsEmpty
&&
!
newIsEmpty
{
return
fmt
.
Errorf
(
"template: redefinition of template %q"
,
name
)
}
newIsEmpty
:=
parse
.
IsEmptyTree
(
tree
.
Root
)
if
newIsEmpty
{
// Whether old is empty or not, new is empty; no reason to replace old.
return
nil
return
false
,
nil
}
if
!
oldIsEmpty
{
return
false
,
fmt
.
Errorf
(
"template: redefinition of template %q"
,
name
)
}
}
t
.
tmpl
[
name
]
=
new
return
nil
return
true
,
nil
}
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