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

Unmarshaling of front matter produces incorrect data types for some slices #10624

Closed
jmooring opened this issue Jan 16, 2023 · 6 comments · Fixed by #10625
Closed

Unmarshaling of front matter produces incorrect data types for some slices #10624

jmooring opened this issue Jan 16, 2023 · 6 comments · Fixed by #10625

Comments

@jmooring
Copy link
Member

jmooring commented Jan 16, 2023

This is not a new issue. It has been present since v0.54.0 (and perhaps earlier).

Front matter:

+++
s = [1, 2, 3]
+++

Template:

{{ range .Params.s }}
  {{ fmt.Printf "%[1]v (%[1]T)" . }}
{{ end }}
Actual:   1 (string) 2 (string) 3 (string)
Expected: 1 (int) 2 (int) 3 (int)

The front matter format (JSON, TOML, or YAML) is irrelevant.

The transform.Unmarshal template produces the expected results:

{{ $t := `s: [1,2,3]` }}
{{ range (transform.Unmarshal $t).s }}
  {{ fmt.Printf "%[1]v (%[1]T)" . }}
{{ end }}

--> 1 (int) 2 (int) 3 (int)

Reference:
https://discourse.gohugo.io/t/42141/2

@bep bep removed the NeedsTriage label Jan 16, 2023
@bep bep added this to the v0.109.0 milestone Jan 16, 2023
@bep bep changed the title Unmarshaling of front matter produces incorrect data types Unmarshaling of front matter produces incorrect data types for some slices Jan 16, 2023
@bep
Copy link
Member

bep commented Jan 16, 2023

As I read the code, this happens with non-string slice types (e.g. ints in the example above). This has probably been the case since Hugo 0.10 or something ... I have no clue why this behaves the way it does; I will create a PR and see if some tests blow up.

@willfaught
Copy link

@bep Regarding the original context of that thread, please also ensure that mixed types (like strings and maps, regardless of the type of the first element) in a list result in a []any type. 🙏

bep added a commit to bep/hugo that referenced this issue Jan 16, 2023
@bep
Copy link
Member

bep commented Jan 16, 2023

@willfaught
Copy link

@bep Thanks for adding that. Assuming the mixed int/string case covers the map/string cases, where the type is []any if the first element is map and the rest of the elements are strings, or vice versa, then it LGTM. It looks like @jmooring tested that specifically separately and it works.

@bep
Copy link
Member

bep commented Jan 17, 2023

@willfaught yea, the new logic is that if []any and all elements are strings the convert to []string, else keep as is.

bep added a commit that referenced this issue Jan 17, 2023
bep added a commit that referenced this issue Jan 17, 2023
@github-actions
Copy link

github-actions bot commented Feb 8, 2023

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants