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

Not possible to render mapping with Int keys in YAML #878

Closed
artemijan opened this issue Jan 10, 2025 · 5 comments · Fixed by #879
Closed

Not possible to render mapping with Int keys in YAML #878

artemijan opened this issue Jan 10, 2025 · 5 comments · Fixed by #879

Comments

@artemijan
Copy link

ll = new Mapping<Int, String>{
  [1] = ""
}

when trying to render it, I get an error:

–– Pkl Error ––
Cannot render object with non-string key as XML.
Object: new Mapping { [1] = "" }
Key   : 1
@StefMa
Copy link
Contributor

StefMa commented Jan 10, 2025

Seems it is not allowed to use numbers only in xml as a tag name.
See also https://www.w3.org/TR/xml/#NT-Name

However, you should be able to use _1 or a1... Which is, in return, in pkl a string 🙃

@artemijan
Copy link
Author

I actually tried it also for a json and yaml. Output is the same. I need this for yaml file.

@HT154
Copy link
Contributor

HT154 commented Jan 10, 2025

JSON requires string keys. I was less sure about YAML (1.2) and looked up the spec: non-string keys (including seq or map) are explicitly allowed, but many YAML implementations do not support them.

This includes Pkl's currently, which I think should be considered a bug. This construct is legal in both Pkl and YAML, so the renderer should accept it (at least for scalar types).

If you can accept numeric string keys, as a workaround you may be able to handle this at render time with a converter, e.g.:

output {
  renderer = new YamlRenderer {
    converters {
      [Mapping] = (it) -> new Mapping {
        for (k, v in it) {
          [k.toString()] = v
        }
      }
    }
  }
}

@HT154
Copy link
Contributor

HT154 commented Jan 10, 2025

Okay, PR open with the fix. You can also use render directives to get the desired output until this fix is released:

output {
  renderer = new YamlRenderer {
    converters {
      [Mapping] = (it) -> new Mapping {
        for (k, v in it) {
          [if (k is Number || k is Boolean || k is Null) new RenderDirective { text = k.toString() } else k] = v
        }
      }
    }
  }
}

@bioball bioball changed the title Not possible to create mapping with Int keys Not possible to create mapping with Int keys in YAML Jan 11, 2025
@bioball bioball changed the title Not possible to create mapping with Int keys in YAML Not possible to render mapping with Int keys in YAML Jan 11, 2025
@bioball
Copy link
Contributor

bioball commented Jan 11, 2025

Changing the title (this behavior is correct in XML and JSON).

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

Successfully merging a pull request may close this issue.

4 participants