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: adds truth and bool template functions #10666

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

khayyamsaleem
Copy link
Contributor

@khayyamsaleem khayyamsaleem commented Jan 28, 2023

The behavior of truth and bool is described in the corresponding test cases and examples. The decision-making around the behavior is a based on combination of the existing behavior of strconv.ParseBool in go and the MDN definition of "truthy" as JavaScript has the most interop with the Hugo ecosystem.

Fixes #9160 and (indirectly) #5792

@khayyamsaleem
Copy link
Contributor Author

@bep open to discussion on the behavior / naming for sure; thought this would open a more productive discussion on #9160 as it was low-hanging fruit that has been open for a long time.

@khayyamsaleem
Copy link
Contributor Author

khayyamsaleem commented Jan 28, 2023

For truth, could also just use the rules in hreflect.IsTruthful, but thought that the MDN standards may be more familiar to end-users. I guess it's unlikely to come up with something that EVERYONE likes here, so best to provide some capability and gauge the response from discourse or GH issues after the release.

@bep
Copy link
Member

bep commented Feb 23, 2023

@khayyamsaleem thanks for this. My 50 cents about this, and please don't adjust your code before we agree on something is.

As to truth, these test cases needs to pass and behave the same for all "truthy values" on the left side:

{{ if "foo" }}OK{{ else }}Failed{{ end }}
{{ if ("foo" | truth) }}OK{{ else }}Failed{{ end }}
{{ if ("foo" | not | not ) }}OK{{ else }}Failed{{ end }}

Which means that if would be a thin wrapper around IsTruthFul.

As to bool, I'm tempted to just use strconv.ParseBool. Combining it with some JavaScript behaviour is surely going to get us into the Yaml trap (YAML < 3 had "yes/no" as bools).

//cc @jmooring

@jmooring
Copy link
Member

A colleague of mine told me about a 700+ line YAML file they had to debug a few years ago. It had some country codes. Norway was falsy.

@khayyamsaleem
Copy link
Contributor Author

@bep I'm fine with the suggestions; better to have one definition of truthiness for the codebase.

@bep
Copy link
Member

bep commented Feb 24, 2023

@khayyamsaleem OK, if you adjust this PR to that much simpler scenario, I will merge.

To be clear:

@bep
Copy link
Member

bep commented Feb 24, 2023

OK, I changed my mind again. Looking at the other cast.* functions we have, and also on other places we do similar, I think it's a pretty clear precedence to just use cast.ToBoolE, so I suggest we just do that for the bool func.

@stereobooster
Copy link

HI @bep. Does this PR needs more work? As far as I understood ToBool function is ok. Do we need to adjust ToTruth function (if reflect.IsTruthful(v) true else false)? Can I help here?

@khayyamsaleem
Copy link
Contributor Author

@bep pardon the delay here. Updated PR per your last review.

The behavior of `truth` and `bool` is described in the corresponding
test cases and examples. The decision-making around the behavior is a
based on combination of the existing behavior of strconv.ParseBool in go
and the MDN definition of "truthy" as JavaScript has the most interop
with the Hugo ecosystem.

Addresses gohugoio#9160 and (indirectly) gohugoio#5792
@khayyamsaleem khayyamsaleem force-pushed the feat/bool-and-truth-tpl-funcs-9160 branch 2 times, most recently from c38fbe1 to 858c07a Compare October 18, 2023 04:24
@jmooring
Copy link
Member

This is not what we agreed to:
#10666 (comment)

cast.ToBool behaves as I expect, though I think we should return false on error.

cast.ToTruth should use hreflect.IsTruthful. The string "false" is truthy.

@khayyamsaleem
Copy link
Contributor Author

Ahhh pardon, you're right. It did seem awkward for Truth and bool to disagree on "false" though.

@khayyamsaleem khayyamsaleem force-pushed the feat/bool-and-truth-tpl-funcs-9160 branch 3 times, most recently from a93a70e to 496ae98 Compare October 20, 2023 12:47
Concession from review, where cast.ToTruth should behave exactly like hreflect.IsTruthful.
Additionally, cast.ToBool always returns a bool with nil error.
@khayyamsaleem khayyamsaleem force-pushed the feat/bool-and-truth-tpl-funcs-9160 branch from 496ae98 to 2843423 Compare October 20, 2023 13:13
@jmooring
Copy link
Member

jmooring commented Oct 20, 2023

@bep I have not tested this yet, but I think the truth function belongs in the compare namespace as IsTruthy. This function does not cast a value into another data type; it performs a comparison.

{{ compare.IsTruthy $foo }}
{{ isTruthy $foo }} (alias)

and

{{ cast.ToBool $foo}}
{{ bool $foo }} (alias)

Of course, that also raises the question, do we want a compare.IsFalsy too? It depends on whether you like the symmetry, or not.

@khayyamsaleem
Copy link
Contributor Author

Are we waiting for @bep 's feedback here, or would we prefer to move the function to the compare namespace?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add bool to template functions
4 participants