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

Collection key type in YAML breaks map range order #4083

Closed
dmgawel opened this issue Nov 15, 2017 · 4 comments
Closed

Collection key type in YAML breaks map range order #4083

dmgawel opened this issue Nov 15, 2017 · 4 comments
Labels
Milestone

Comments

@dmgawel
Copy link
Contributor

dmgawel commented Nov 15, 2017

When defining a collection in Hugo data file using YAML format it is then parsed as map[interface {}]interface {}. This fundamentally differs from TOML and JSON which parses collections to map[string]interface {}.

Example (YAML):

test:
  a: 1
  b: 2

is parsed to:

map[interface {}]interface {}{"a":1, "b":2}

Example (TOML):

[test]
a = 1
b = 2

is parsed to:

map[string]interface {}{"a":1, "b":2}

Example (JSON):

{
  "test": {
    "a": 1,
    "b": 2
  }
}

is parsed to:

map[string]interface {}{"a":1, "b":2}

As YAML is a superset of JSON one can suppose the same key type in both formats.

The following have a great effect on ranging over maps. From the range docs:

If the value is a map and the keys are of basic type with a defined order ("comparable"), the elements will be visited in sorted key order.

Apparently string is comparable, unlike interface{}.

@dmgawel dmgawel changed the title Collection key type in YAML breaks map range Collection key type in YAML breaks map range order Nov 15, 2017
@dmgawel
Copy link
Contributor Author

dmgawel commented Nov 15, 2017

I came across another issue with this behaviour. map[interface {}]interface {} is not 'jsonifiable'. Given data in test.yaml executing: {{ .Site.Data.test | jsonify }} outputs:

ERROR 2017/11/15 14:43:43 Error while rendering "home": template: index.html:8:23: executing "index.html" at <jsonify>: error calling jsonify: json: unsupported type: map[interface {}]interface {}

@bep
Copy link
Member

bep commented Nov 15, 2017

I assume this is a go-yaml upstream issue -- and I think I have seen it before?

@moorereason
Copy link
Contributor

Duplicate of #2441

@moorereason moorereason marked this as a duplicate of #2441 Nov 15, 2017
dmgawel added a commit to dmgawel/hugo that referenced this issue Dec 1, 2017
Recurse through result of yaml package parsing and change all
maps from map[interface{}]interface{} to map[string]interface{}
making them jsonable and sortable.

Fixes gohugoio#2441, gohugoio#4083
dmgawel added a commit to dmgawel/hugo that referenced this issue Dec 6, 2017
Recurse through result of yaml package parsing and change all
maps from map[interface{}]interface{} to map[string]interface{}
making them jsonable and sortable.

Fixes gohugoio#2441, gohugoio#4083
dmgawel added a commit to dmgawel/hugo that referenced this issue Dec 6, 2017
Recurse through result of yaml package parsing and change all
maps from map[interface{}]interface{} to map[string]interface{}
making them jsonable and sortable.

Fixes gohugoio#2441, gohugoio#4083
@bep bep modified the milestone: v0.37 Feb 9, 2018
@bep bep removed the Duplicate label Feb 9, 2018
bep pushed a commit that referenced this issue Feb 9, 2018
Recurse through result of yaml package parsing and change all
maps from map[interface{}]interface{} to map[string]interface{}
making them jsonable and sortable.

Fixes #2441, #4083
@github-actions
Copy link

github-actions bot commented Mar 8, 2022

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 Mar 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants