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

Improve the cond func #5792

Open
bep opened this issue Mar 25, 2019 · 10 comments
Open

Improve the cond func #5792

bep opened this issue Mar 25, 2019 · 10 comments
Milestone

Comments

@bep
Copy link
Member

bep commented Mar 25, 2019

There are two items that I want to address:

  1. Make it use the same "truth" function as if/with/not/and -- but we should think a little if this would break stuff
  2. Lazily invocation of passed funcs/methods

Basically I want to be able to do something ala:

n := cond .File .File.Filename .Name

Maybe not the best example, but imagine a nilpointer scenario etc.

@bep bep modified the milestones: v0.55, v0.56 Mar 25, 2019
@jamesgeorge007
Copy link

@bep I would love to give it a try 👏 I'm new here 🔰
It would be great if you could point me to the right files to work with 👍

@bep
Copy link
Member Author

bep commented Mar 26, 2019

@jamesgeorge007 this is a particulary hard problem. I don't know how to do it myself.

@regisphilibert
Copy link
Member

One line is always better than 4, especially in Go Template.

{{ $n := .Name }}
{{ with .File }}
  {{ $n = .Filename }}
{{ end }}

@jamesgeorge007
Copy link

@bep @regisphilibert if that's the case, can you point me to something that is good to start with 👏

@moorereason
Copy link
Contributor

Given:

n := cond .File .File.Filename .Name

@bep, as I under the existing implementation, it would blow up if .File is nil because the text/template engine would resolve .File.Filename before sending it to cond. Correct?

@bep
Copy link
Member Author

bep commented Mar 27, 2019

@moorereason It would 2 days ago ... I changed it from a pointer to a struct (which is how was in 0.54) for a very similar reason.

But you are right, there are (at least) 2 reasons why I want to fix this:

  • To avoid nilpointers (a comon use case for cond
  • To avoid costly initialization for values I don't use

The problem, I have no idea how to do this without making modifications to Go's stdlib, so I might bring it up with them (would not be too hopeful). Because now:

{{ if and .Foo .Bar }}{{ end }}

(same with or)

Is very different from:

if .Foo && .Bar {
}

@bep
Copy link
Member Author

bep commented Mar 28, 2019

#5792

@bep
Copy link
Member Author

bep commented Mar 29, 2019

I think any help from the Go stdlib in this area is long down the road. But for cond I think we could solve it by handling it as (another) special case in the AST transformer:

{{ $n := cond .File .File.Filename .Name }}

Becomes

{{ $_tmp := "" }}
{{ if .File }}
{{ $_tmp = .File.Filename }}
{{ else }}
{{ $_tmp = .Name }}
{{ end }}
{{ $n := $_tmp }}

Or similar.

@bep bep modified the milestones: v0.56, v0.57 Jun 14, 2019
@bep bep modified the milestones: v0.57, v0.58 Jul 31, 2019
@bep bep modified the milestones: v0.58, v0.59 Aug 13, 2019
@bep bep modified the milestones: v0.59, v0.60 Sep 6, 2019
@bep bep modified the milestones: v0.60, v0.61 Oct 21, 2019
@bep bep modified the milestones: v0.61, v0.62, v0.63 Nov 25, 2019
@bep bep modified the milestones: v0.63, v0.64 Dec 11, 2019
@bep bep added this to the v0.115.0 milestone Jun 13, 2023
@bep bep modified the milestones: v0.115.0, v0.116.0 Jun 30, 2023
@bep bep modified the milestones: v0.116.0, v0.117.0 Aug 1, 2023
@bep bep modified the milestones: v0.117.0, v0.118.0 Aug 30, 2023
@bep bep modified the milestones: v0.118.0, v0.119.0 Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 4, 2023
khayyamsaleem added a commit to khayyamsaleem/hugo that referenced this issue Oct 18, 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.

Addresses gohugoio#9160 and (indirectly) gohugoio#5792
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@bep bep modified the milestones: v0.121.0, v0.122.0 Dec 6, 2023
@bep bep modified the milestones: v0.122.0, v0.123.0, v0.124.0 Jan 27, 2024
@bep bep modified the milestones: v0.124.0, v0.125.0 Mar 4, 2024
@bep bep modified the milestones: v0.125.0, v0.126.0 Apr 23, 2024
@bep bep modified the milestones: v0.126.0, v0.127.0 May 15, 2024
@bep bep modified the milestones: v0.127.0, v0.128.0 Jun 8, 2024
@bep bep modified the milestones: v0.128.0, v0.129.0 Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants