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

Open
safferyj opened this issue Nov 12, 2024 · 1 comment
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 {}

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