Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tpl: Add float template function #3921

Merged
merged 1 commit into from
Sep 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/content/functions/float.md
Original file line number Diff line number Diff line change
@@ -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
```
23 changes: 17 additions & 6 deletions tpl/cast/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change exisiting methods. This doesn't look correct and I'm surprised there were not red tests.

Copy link
Contributor Author

@x3ro x3ro Sep 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change exisiting methods

Why not? Should I just copy-and-paste the code that was used in toInt? I just took what was there before (the switch-case for the template types) and moved it into a separate helper function.

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)
Expand All @@ -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
}
37 changes: 37 additions & 0 deletions tpl/cast/cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests assumes that the input is strings only. I suspect your implementation will fail on 2, int64(2), float64(2) etc.

Copy link
Contributor Author

@x3ro x3ro Sep 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed additional additional test cases, they all pass.

PS: Never mind me, there seems to be a difference between running make check and go test in that directory. Investigating

PPS: I just commited a typo, nvm

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I see now -- I read your original code slightly wrong, but it helped with the test cases.

{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)
}
}
7 changes: 7 additions & 0 deletions tpl/cast/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ func init() {
},
)

ns.AddMethodMapping(ctx.ToFloat,
[]string{"float"},
[][2]string{
{`{{ "1234" | float | printf "%T" }}`, `float64`},
},
)

return ns

}
Expand Down