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

tpl: Add float template function #3921

merged 1 commit into from
Sep 28, 2017

Conversation

x3ro
Copy link
Contributor

@x3ro x3ro commented Sep 27, 2017

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

Feedback very welcome, this is the first time I've tampered with the hugo source 🎉

@@ -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.

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.

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 gohugoio#3307
@bep bep merged commit 57adc53 into gohugoio:master Sep 28, 2017
@github-actions
Copy link

github-actions bot commented Feb 9, 2022

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add float function to parse string to floating point value
2 participants