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

Different missing key zero behaviour with data.CSVByRow vs coll.Slice (coll.Dict ...) (coll.Dict ...) #2261

Closed
safferyj opened this issue Nov 12, 2024 · 2 comments
Labels

Comments

@safferyj
Copy link

With missingkey.tmpl:

{{$csv := 
`lang,keywords
C,32
Go,25
COBOL,357` -}}

{{- $c := ($csv | data.CSVByRow)}}
$c: {{$c}}

{{- $d := coll.Slice (coll.Dict "lang" "C" "keywords" "32") (coll.Dict "lang" "Go" "keywords" "25") (coll.Dict "lang" "COBOL" "keywords" "357")}}
$d: {{$d}}

{{$testTemplate := `{{range . -}}
{{.lang }} has {{ .keywords }} keywords.
[{{.missingKey}}] does not exist.
{{if .missingKey}}bad{{else}}good{{end}}
{{if .missingKey | strings.TrimSpace}}bad{{else}}good{{end}}
{{if $x := .missingKey | strings.TrimSpace}}bad{{else}}good{{end}}
{{end}}` -}}

Render $c:
{{tmpl.Inline $testTemplate $c}}
Render $d:
{{tmpl.Inline $testTemplate $d}}

Execution of gomplate --missing-key zero -f missingkey.tmpl with 4.1.0 generates the following output:


$c: [map[keywords:32 lang:C] map[keywords:25 lang:Go] map[keywords:357 lang:COBOL]]
$d: [map[keywords:32 lang:C] map[keywords:25 lang:Go] map[keywords:357 lang:COBOL]]

Render $c:
C has 32 keywords.
[] does not exist.
good
good
good
Go has 25 keywords.
[] does not exist.
good
good
good
COBOL has 357 keywords.
[] does not exist.
good
good
good

Render $d:
C has 32 keywords.
[<no value>] does not exist.
good
bad
bad
Go has 25 keywords.
[<no value>] does not exist.
good
bad
bad
COBOL has 357 keywords.
[<no value>] does not exist.
good
bad
bad

See also discussion #2215.

@safferyj
Copy link
Author

I notice that the types of $c and $d are different. Adding

$c: {{printf "%T" $c}}
$d: {{printf "%T" $d}}

to the template results in the following additional output

$c: []map[string]string
$d: []interface {}

@hairyhenderson
Copy link
Owner

I thought I'd commented on this already, but - in some ways the behaviour is expected:

For $c (the []map[string]string from data.CSVByRow), the operation results in a failed lookup from the map[string]string, and since the type of value is string, the zero value that's returned is an empty string "".

$d however is populated by coll.Slice and coll.Dict. coll.Dict produces a map[string]interface{}, and since the zero value of interface{} is nil, that's what's returned.

In Go templates, nil is rendered as <no value>.

If you choose the other missing-key behaviour (--missing-key invalid), rather than the zero values both simply get nil (and are rendered as <no value>).

I'm really not sure if there's anything reasonable that can be done here - given the different types, I believe this is working as well as can be expected.

One workaround could be to use the default function:

[{{.missingKey | default ""}}] does not exist.

This'll work with both missing-key=zero and missing-key=invalid (but not with the default error behaviour.

I'm going to close this issue as I don't think there's anything that can be done. If you have ideas, feel free to re-open and we can discuss!

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

No branches or pull requests

2 participants