Skip to content

Commit

Permalink
Fix forloop.last with offset modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jun 29, 2017
1 parent d924e0b commit 394036d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
38 changes: 18 additions & 20 deletions tags/loop.go
Expand Up @@ -47,42 +47,40 @@ func loopTagParser(node chunks.ASTControlTag) (func(io.Writer, chunks.Context) e
if rt.Kind() != reflect.Array && rt.Kind() != reflect.Slice {
return nil
}
start := loop.Offset
limit := rt.Len()
if loop.Offset > 0 {
if loop.Offset > rt.Len() {
return nil
}
rt = rt.Slice(loop.Offset, rt.Len())
}
length := rt.Len()
if loop.Limit != nil {
limit = *loop.Limit
length = *loop.Limit
if length > rt.Len() {
length = rt.Len()
}
}
const forloopName = "forloop"
defer func(index, forloop interface{}) {
ctx.Set(forloopName, index)
ctx.Set(loop.Variable, forloop)
}(ctx.Get(forloopName), ctx.Get(loop.Variable))
// for forloop variable
var (
first = true
index = 1
length = limit
)
for i := start; i < rt.Len(); i++ {
if limit == 0 {
break
}
limit--
for i := 0; i < length; i++ {
j := i
if loop.Reversed {
j = rt.Len() - 1 - i
}
ctx.Set(loop.Variable, rt.Index(j).Interface())
ctx.Set(forloopName, map[string]interface{}{
"first": first,
"last": limit == 0,
"index": index,
"index0": index - 1,
"rindex": length + 1 - index,
"rindex0": length - index,
"first": i == 0,
"last": i == length-1,
"index": i + 1,
"index0": i,
"rindex": length - i,
"rindex0": length - i - 1,
"length": length,
})
first, index = false, index+1
err := ctx.RenderASTSequence(w, node.Body)
if err == errLoopBreak {
break
Expand Down
24 changes: 19 additions & 5 deletions tags/tags_test.go
Expand Up @@ -61,28 +61,42 @@ var tagTests = []struct{ in, expected string }{
{`{%for a in ar reversed%}{{a}}.{%endfor%}`, "third.second.first."},
{`{%for a in ar limit:2%}{{a}}.{%endfor%}`, "first.second."},
{`{%for a in ar offset:1%}{{a}}.{%endfor%}`, "second.third."},
{`{%for a in ar reversed offset:1%}{{a}}.{%endfor%}`, "second.first."},
{`{%for a in ar reversed limit:1%}{{a}}.{%endfor%}`, "third."},
{`{%for a in ar limit:1 offset:1%}{{a}}.{%endfor%}`, "second."},
{`{%for a in ar reversed limit:1 offset:1%}{{a}}.{%endfor%}`, "second."},
// TODO investigate how these combine; does it depend on the order
// {`{%for a in ar reversed offset:1%}{{a}}.{%endfor%}`, "second.first."},
// {`{%for a in ar limit:1 offset:1%}{{a}}.{%endfor%}`, "second."},
// {`{%for a in ar reversed limit:1 offset:1%}{{a}}.{%endfor%}`, "second."},

// loop variables
{`{%for a in ar%}{{forloop.index}}:{{forloop.first}} {%endfor%}`, "1:true 2: 3: "},
{`{%for a in ar%}{{forloop.index}}:{{forloop.last}} {%endfor%}`, "1: 2: 3:true "},
{`{%for a in ar%}{{forloop.first}}.{%endfor%}`, "true.false.false."},
{`{%for a in ar%}{{forloop.last}}.{%endfor%}`, "false.false.true."},
{`{%for a in ar%}{{forloop.index}}.{%endfor%}`, "1.2.3."},
{`{%for a in ar%}{{forloop.index0}}.{%endfor%}`, "0.1.2."},
{`{%for a in ar%}{{forloop.rindex}}.{%endfor%}`, "3.2.1."},
{`{%for a in ar%}{{forloop.rindex0}}.{%endfor%}`, "2.1.0."},
{`{%for a in ar%}{{forloop.length}}.{%endfor%}`, "3.3.3."},

{`{%for i in ar%}{{forloop.index}}[{%for j in ar%}{{forloop.index}}{%endfor%}]{{forloop.index}}{%endfor%}`,
"1[123]12[123]23[123]3"},

{`{%for a in ar reversed%}{{forloop.first}}.{%endfor%}`, "true.false.false."},
{`{%for a in ar reversed%}{{forloop.last}}.{%endfor%}`, "false.false.true."},
{`{%for a in ar reversed%}{{forloop.index}}.{%endfor%}`, "1.2.3."},
{`{%for a in ar reversed%}{{forloop.rindex}}.{%endfor%}`, "3.2.1."},
{`{%for a in ar reversed%}{{forloop.length}}.{%endfor%}`, "3.3.3."},

{`{%for a in ar limit:2%}{{forloop.index}}.{%endfor%}`, "1.2."},
{`{%for a in ar limit:2%}{{forloop.rindex}}.{%endfor%}`, "2.1."},
{`{%for a in ar limit:2%}{{forloop.first}}.{%endfor%}`, "true.false."},
{`{%for a in ar limit:2%}{{forloop.last}}.{%endfor%}`, "false.true."},
{`{%for a in ar limit:2%}{{forloop.length}}.{%endfor%}`, "2.2."},

{`{%for a in ar offset:1%}{{forloop.index}}.{%endfor%}`, "1.2."},
{`{%for a in ar offset:1%}{{forloop.rindex}}.{%endfor%}`, "2.1."},
{`{%for a in ar offset:1%}{{forloop.first}}.{%endfor%}`, "true.false."},
{`{%for a in ar offset:1%}{{forloop.last}}.{%endfor%}`, "false.true."},
{`{%for a in ar offset:1%}{{forloop.length}}.{%endfor%}`, "2.2."},

{`{%for a in ar%}{%if a == 'second'%}{%break%}{%endif%}{{a}}{%endfor%}`, "first"},
{`{%for a in ar%}{%if a == 'second'%}{%continue%}{%endif%}{{a}}.{%endfor%}`, "first.third."},

Expand Down

0 comments on commit 394036d

Please sign in to comment.