Skip to content

Conversation

@tianon
Copy link

@tianon tianon commented Dec 22, 2020

👋 ❤️

I looked into Sprig today because it implements many functions my own hand-rolled library was, in most cases with the same name/signature! Thank you for creating such a cool project.

One minor difference between my own ternary and the one provided by Sprig is that mine accepts interface{} instead of bool and uses the official template.IsTrue function that controls the semantics of the built-in if (and friends) to convert interface{} into bool (for further context, template.IsTrue has been officially exported since Go 1.6: golang/go@a326c3e).

Given that this would make ternary match the semantics of if, it felt like an appropriate change, and it was simple enough that I thought discussing over a concrete PR might be the easiest way to see if this is something that you'd be interested in. 😄

Just to be explicit, I'm happy to adjust, rebase, amend, discuss, close, etc! ❤️

This adjusts `ternary` to use the same `template.IsTrue` that controls the semantics of the built-in `if` (and friends).

For further context, `template.IsTrue` has been officially exported since Go 1.6 (golang/go@a326c3e).
func ternary(vt interface{}, vf interface{}, v bool) interface{} {
if v {
func ternary(vt interface{}, vf interface{}, v interface{}) interface{} {
if truth, ok := template.IsTrue(v); ok && truth {
Copy link
Author

Choose a reason for hiding this comment

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

In my own library, I'd implemented this with a panic for !ok (which I'm happy to adjust this to), but I wasn't sure if panic was appropriate here. 😅

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.

1 participant