tpl: Add float template function
authorLucas Jenss <lucas@x3ro.de>
Wed, 27 Sep 2017 22:35:47 +0000 (00:35 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 28 Sep 2017 14:53:18 +0000 (16:53 +0200)
Add a template function that allows conversion to float. This is
useful, for example, when passing aspect ratios into templates,
which tend to not be integers.

Fixes #3307

docs/content/functions/float.md [new file with mode: 0644]
tpl/cast/cast.go
tpl/cast/cast_test.go
tpl/cast/init.go

diff --git a/docs/content/functions/float.md b/docs/content/functions/float.md
new file mode 100644 (file)
index 0000000..2a5f757
--- /dev/null
@@ -0,0 +1,26 @@
+---
+title: float
+linktitle: float
+description: Creates a `float` from the argument passed into the function.
+godocref:
+date: 2017-09-28
+publishdate: 2017-09-28
+lastmod: 2017-09-28
+categories: [functions]
+menu:
+  docs:
+    parent: "functions"
+keywords: [strings,floats]
+signature: ["float INPUT"]
+workson: []
+hugoversion:
+relatedfuncs: []
+deprecated: false
+aliases: []
+---
+
+Useful for turning strings into floating point numbers.
+
+```
+{{ float "1.23" }} → 1.23
+```
index 378467178524444fd5556fe1b4119b2022f5929b..999b31214e19ed3734048b40550b4021678b597a 100644 (file)
@@ -30,6 +30,22 @@ type Namespace struct {
 
 // ToInt converts the given value to an int.
 func (ns *Namespace) ToInt(v interface{}) (int, error) {
+       v = convertTemplateToString(v)
+       return _cast.ToIntE(v)
+}
+
+// ToString converts the given value to a string.
+func (ns *Namespace) ToString(v interface{}) (string, error) {
+       return _cast.ToStringE(v)
+}
+
+// ToFloat converts the given value to a float.
+func (ns *Namespace) ToFloat(v interface{}) (float64, error) {
+       v = convertTemplateToString(v)
+       return _cast.ToFloat64E(v)
+}
+
+func convertTemplateToString(v interface{}) interface{} {
        switch vv := v.(type) {
        case template.HTML:
                v = string(vv)
@@ -42,10 +58,5 @@ func (ns *Namespace) ToInt(v interface{}) (int, error) {
        case template.JSStr:
                v = string(vv)
        }
-       return _cast.ToIntE(v)
-}
-
-// ToString converts the given value to a string.
-func (ns *Namespace) ToString(v interface{}) (string, error) {
-       return _cast.ToStringE(v)
+       return v
 }
index a5e0db2afac7932c4ea47a0687bd38f158274dd4..fc20934f8d5ccfd95fb99d47b1a3868c2b5b8066 100644 (file)
@@ -81,3 +81,40 @@ func TestToString(t *testing.T) {
                assert.Equal(t, test.expect, result, errMsg)
        }
 }
+
+func TestToFloat(t *testing.T) {
+       t.Parallel()
+
+       ns := New()
+
+       for i, test := range []struct {
+               v      interface{}
+               expect interface{}
+       }{
+               {"1", 1.0},
+               {template.HTML("2"), 2.0},
+               {template.CSS("3"), 3.0},
+               {template.HTMLAttr("4"), 4.0},
+               {template.JS("-5.67"), -5.67},
+               {template.JSStr("6"), 6.0},
+               {"1.23", 1.23},
+               {"-1.23", -1.23},
+               {"0", 0.0},
+               {float64(2.12), 2.12},
+               {int64(123), 123.0},
+               {2, 2.0},
+               {t, false},
+       } {
+               errMsg := fmt.Sprintf("[%d] %v", i, test.v)
+
+               result, err := ns.ToFloat(test.v)
+
+               if b, ok := test.expect.(bool); ok && !b {
+                       require.Error(t, err, errMsg)
+                       continue
+               }
+
+               require.NoError(t, err, errMsg)
+               assert.Equal(t, test.expect, result, errMsg)
+       }
+}
index d1547310cf8a2ea3d8eb90ab80a1e6ac361b01e5..3aee6f03679ec0026594b4c085114b06d7f0654f 100644 (file)
@@ -43,6 +43,13 @@ func init() {
                        },
                )
 
+               ns.AddMethodMapping(ctx.ToFloat,
+                       []string{"float"},
+                       [][2]string{
+                               {`{{ "1234" | float | printf "%T" }}`, `float64`},
+                       },
+               )
+
                return ns
 
        }