Skip to content

Commit

Permalink
Merge pull request #66 from lucapette/gmile-issue-56
Browse files Browse the repository at this point in the history
Support range loops
  • Loading branch information
lucapette authored Feb 5, 2018
2 parents f6b5d74 + 636e74d commit 3cf04e1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ $ echo "#{{ Int 0 100}} {{ Name }} <{{ Email }}>" | fakedata

### Generators

All the generators listed under `fakedata -g` are available as functions into
the templates. If the generator name is a single word, then it's available as a
The generators listed under `fakedata -g` are available as functions into the
templates. If the generator name is a single word, then it's available as a
function with the same name capitalized (example: `int` becomes `Int`). If the
generator name is composed by multiple words joined by dots, then the function
name is again capitalized by the first letter of the word and joined together
Expand Down Expand Up @@ -276,17 +276,13 @@ returns a date between one year ago and today.

### Helpers

Beside the generator functions, the `fakedata` template implementation provides
a set of helper functions:
Beside the generator functions, `fakedata` templates provide a set of helper
functions:

- `Loop`
- `Odd`
- `Even`

When using a custom loop make sure to use `--limit 1`, otherwise the loop will
run multiple times! Running a template with `{{ range Loop 5}}` and `--limit 5`
will execute 25 times.

If you need to create your own loop for advanced templates you can use the `{{
Loop }}` function. This function takes a single integer as parameter which is
the number of iterations. `Loop` has to be used with `range` e.g.
Expand All @@ -297,6 +293,14 @@ the number of iterations. `Loop` has to be used with `range` e.g.
{{ end }}
```

`Loop` can take a second argument, so that you can specify a range and
`fakedata` will generate a random number of interations in that range. For
example:

```html
{{ range Loop 1 5 }}42{{ end }}
```

In combination with `Loop` and `range` you can use `Odd` and `Even` to determine
if the current iteration is odd or even. This is especially helpful when
creating HTML tables:
Expand Down
1 change: 1 addition & 0 deletions integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ var templateTests = []struct {
wantErr bool
}{
{"simple.tmpl", "simple-template.golden", false},
{"loop.tmpl", "loop.golden", false},
{"broken.tmpl", "broken-template.golden", true},
{"unknown-function.tmpl", "unknown-function.golden", true},
}
Expand Down
1 change: 1 addition & 0 deletions integration/fixtures/loop.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{range Loop 1 1}}42{{end}}
10 changes: 10 additions & 0 deletions integration/golden/loop.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
42
42
42
42
42
42
42
42
42
42
19 changes: 18 additions & 1 deletion pkg/fakedata/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fakedata

import (
"fmt"
"math/rand"
"os"
"strings"
"text/template"
Expand All @@ -17,7 +18,23 @@ func newTemplateFactory() *templateFactory {

func (tf templateFactory) getFunctions() template.FuncMap {
funcMap := template.FuncMap{
"Loop": func(i int) []int { return make([]int, i) },
"Loop": func(minmax ...int) []int {
var n int

if len(minmax) == 1 {
n = minmax[0]
} else {
min := minmax[0]
max := minmax[1]
if min == max {
n = min
} else {
n = rand.Intn(max-min) + min
}
}

return make([]int, n)
},
"Odd": func(i int) bool { return i%2 != 0 },
"Even": func(i int) bool { return i%2 == 0 },
}
Expand Down

0 comments on commit 3cf04e1

Please sign in to comment.