Skip to content

Commit

Permalink
add parseFloat and parseInt filters
Browse files Browse the repository at this point in the history
  • Loading branch information
HeavyHorst committed Aug 13, 2020
1 parent 5b8f800 commit cfcb86a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/content/template/template-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ weight: 20

## Builtin filters

<details>
<summary> **parseInt** -- Takes the given string and parses it as a base-10 integer (64bit) </summary>

```
{{ "12000" | parseInt }}
```
</details>

<details>
<summary> **parseFloat** -- Takes the given string and parses it as a float64 </summary>

```
{{ "12000.45" | parseFloat }}
```
</details>

<details>
<summary> **base64** -- Encodes a string as base64 </summary>

Expand Down
45 changes: 45 additions & 0 deletions pkg/template/template_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"path/filepath"
"reflect"
"sort"
"strconv"
"strings"

"github.com/HeavyHorst/memkv"
Expand All @@ -27,6 +28,8 @@ import (

func init() {
pongo2.RegisterFilter("sortByLength", filterSortByLength)
pongo2.RegisterFilter("parseInt", filterParseInt)
pongo2.RegisterFilter("parseFloat", filterParseFloat)
pongo2.RegisterFilter("parseYAML", filterUnmarshalYAML)
pongo2.RegisterFilter("parseJSON", filterUnmarshalYAML) // just an alias
pongo2.RegisterFilter("parseYAMLArray", filterUnmarshalYAML) // deprecated
Expand Down Expand Up @@ -140,6 +143,48 @@ func filterToYAML(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2
return pongo2.AsValue(string(b)), nil
}

func filterParseInt(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if !in.IsString() {
return in, nil
}

ins := in.String()
if ins == "" {
return pongo2.AsValue(0), nil
}

result, err := strconv.ParseInt(ins, 10, 64)
if err != nil {
return nil, &pongo2.Error{
Sender: "filter:filterParseInt",
OrigError: err,
}
}

return pongo2.AsValue(result), nil
}

func filterParseFloat(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if !in.IsString() {
return in, nil
}

ins := in.String()
if ins == "" {
return pongo2.AsValue(0.0), nil
}

result, err := strconv.ParseFloat(ins, 10)
if err != nil {
return nil, &pongo2.Error{
Sender: "filter:filterParseFloat",
OrigError: err,
}
}

return pongo2.AsValue(result), nil
}

func filterUnmarshalYAML(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if !in.IsString() {
return in, nil
Expand Down
20 changes: 20 additions & 0 deletions pkg/template/template_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ func (s *FilterSuite) TestFilterBase(t *C) {
t.Check(res.String(), Equals, "bar")
}

func (s *FilterSuite) TestFilterParseInt(t *C) {
in := pongo2.AsValue("100")
res, err := filterParseInt(in, nil)
if err != nil {
t.Error(err.OrigError)
}

t.Check(res.Integer(), Equals, 100)
}

func (s *FilterSuite) TestFilterParseFloat(t *C) {
in := pongo2.AsValue("22.45")
res, err := filterParseFloat(in, nil)
if err != nil {
t.Error(err.OrigError)
}

t.Check(res.Float(), Equals, 22.45)
}

func (s *FilterSuite) TestFilterDir(t *C) {
in := pongo2.AsValue("/etc/foo/bar")
res, err := filterDir(in, nil)
Expand Down

0 comments on commit cfcb86a

Please sign in to comment.