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
96da3e96
Commit
96da3e96
authored
Dec 24, 2009
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement .repeats for maps.
Fixes #309. R=rsc CC=golang-dev
https://golang.org/cl/181044
parent
8c557962
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
27 deletions
+37
-27
src/pkg/template/template.go
src/pkg/template/template.go
+20
-26
src/pkg/template/template_test.go
src/pkg/template/template_test.go
+17
-1
No files found.
src/pkg/template/template.go
View file @
96da3e96
...
...
@@ -833,41 +833,35 @@ func (t *Template) executeRepeated(r *repeatedElement, st *state) {
}
first
:=
true
if
array
,
ok
:=
field
.
(
reflect
.
ArrayOrSliceValue
);
ok
{
for
j
:=
0
;
j
<
array
.
Len
();
j
++
{
newst
:=
st
.
clone
(
array
.
Elem
(
j
))
// .alternates between elements
if
!
first
&&
r
.
altstart
>=
0
{
for
i
:=
r
.
altstart
;
i
<
r
.
altend
;
{
i
=
t
.
executeElement
(
i
,
newst
)
}
}
first
=
false
for
i
:=
start
;
i
<
end
;
{
// Code common to all the loops.
loopBody
:=
func
(
newst
*
state
)
{
// .alternates between elements
if
!
first
&&
r
.
altstart
>=
0
{
for
i
:=
r
.
altstart
;
i
<
r
.
altend
;
{
i
=
t
.
executeElement
(
i
,
newst
)
}
}
first
=
false
for
i
:=
start
;
i
<
end
;
{
i
=
t
.
executeElement
(
i
,
newst
)
}
}
if
array
,
ok
:=
field
.
(
reflect
.
ArrayOrSliceValue
);
ok
{
for
j
:=
0
;
j
<
array
.
Len
();
j
++
{
loopBody
(
st
.
clone
(
array
.
Elem
(
j
)))
}
}
else
if
m
,
ok
:=
field
.
(
*
reflect
.
MapValue
);
ok
{
for
_
,
key
:=
range
m
.
Keys
()
{
loopBody
(
st
.
clone
(
m
.
Elem
(
key
)))
}
}
else
if
ch
:=
iter
(
field
);
ch
!=
nil
{
for
{
e
:=
ch
.
Recv
()
if
ch
.
Closed
()
{
break
}
newst
:=
st
.
clone
(
e
)
// .alternates between elements
if
!
first
&&
r
.
altstart
>=
0
{
for
i
:=
r
.
altstart
;
i
<
r
.
altend
;
{
i
=
t
.
executeElement
(
i
,
newst
)
}
}
first
=
false
for
i
:=
start
;
i
<
end
;
{
i
=
t
.
executeElement
(
i
,
newst
)
}
loopBody
(
st
.
clone
(
e
))
}
}
else
{
t
.
execError
(
st
,
r
.
linenum
,
".repeated: cannot repeat %s (type %s)"
,
...
...
src/pkg/template/template_test.go
View file @
96da3e96
...
...
@@ -42,6 +42,7 @@ type S struct {
false
bool
mp
map
[
string
]
string
innermap
U
stringmap
map
[
string
]
string
bytes
[]
byte
}
...
...
@@ -314,12 +315,24 @@ var tests = []*Test{
out
:
"Ahoy!
\n
"
,
},
&
Test
{
in
:
"{innermap.mp.innerkey}
\n
"
,
out
:
"55
\n
"
,
},
&
Test
{
in
:
"{stringmap.stringkey1}
\n
"
,
out
:
"stringresult
\n
"
,
},
&
Test
{
in
:
"{.repeated section stringmap}
\n
"
+
"{@}
\n
"
+
"{.end}"
,
out
:
"stringresult
\n
"
+
"stringresult
\n
"
,
},
}
func
TestAll
(
t
*
testing
.
T
)
{
...
...
@@ -342,6 +355,9 @@ func TestAll(t *testing.T) {
s
.
mp
[
"mapkey"
]
=
"Ahoy!"
s
.
innermap
.
mp
=
make
(
map
[
string
]
int
)
s
.
innermap
.
mp
[
"innerkey"
]
=
55
s
.
stringmap
=
make
(
map
[
string
]
string
)
s
.
stringmap
[
"stringkey1"
]
=
"stringresult"
// the same value so repeated section is order-independent
s
.
stringmap
[
"stringkey2"
]
=
"stringresult"
s
.
bytes
=
strings
.
Bytes
(
"hello"
)
var
buf
bytes
.
Buffer
...
...
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