Open bep opened 5 years ago
@bep I would love to give it a try :clap: I'm new here :beginner: It would be great if you could point me to the right files to work with :+1:
@jamesgeorge007 this is a particulary hard problem. I don't know how to do it myself.
One line is always better than 4, especially in Go Template.
{{ $n := .Name }}
{{ with .File }}
{{ $n = .Filename }}
{{ end }}
@bep @regisphilibert if that's the case, can you point me to something that is good to start with :clap:
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?
@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:
cond
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 {
}
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.
What's needed to get this done? If you sketch out the likely changes that need to be made, and where to make them, I can give it a shot.
There are two items that I want to address:
Basically I want to be able to do something ala:
Maybe not the best example, but imagine a nilpointer scenario etc.