Skip to content

Commit

Permalink
Merge pull request kelseyhightower#668 from dcode/master
Browse files Browse the repository at this point in the history
Add atoi function to template funcs and test
  • Loading branch information
okushchenko authored Feb 20, 2018
2 parents 9671ca0 + 7b7d6fc commit b77b923
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,14 @@ Wrapper for [net.LookupIP](https://golang.org/pkg/net/#LookupIP) function. The w
{{end}}
```

### atoi

Alias for the [strconv.Atoi](https://golang.org/pkg/strconv/#Atoi) function.

```
{{seq 1 (atoi (getv "/count"))}}
```

## Example Usage

```Bash
Expand Down
2 changes: 2 additions & 0 deletions resource/template/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -45,6 +46,7 @@ func newFuncMap() map[string]interface{} {
m["mod"] = func(a, b int) int { return a % b }
m["mul"] = func(a, b int) int { return a * b }
m["seq"] = Seq
m["atoi"] = strconv.Atoi
return m
}

Expand Down
19 changes: 19 additions & 0 deletions resource/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,25 @@ dest = "./tmp/test.conf"
[1 2 3]
`,
updateStore: func(tr *TemplateResource) {},
}, templateTest{
desc: "atoi test",
toml: `
[template]
src = "test.conf.tmpl"
dest = "./tmp/test.conf"
keys = [
"/test/count/",
]
`,
tmpl: `
{{ seq 1 (atoi (getv "/test/count")) }}
`,
expected: `
[1 2 3]
`,
updateStore: func(tr *TemplateResource) {
tr.store.Set("/test/count", "3")
},
},
}

Expand Down

0 comments on commit b77b923

Please sign in to comment.