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

add support for numeric keys in map literal #934

Closed
wants to merge 1 commit into from

Conversation

chrisirhc
Copy link
Contributor

@chrisirhc chrisirhc commented Oct 25, 2022

Hit this use case in our usage of jinja.
Putting up a PR in case there's interest in merging this functionality.

This is a hack to support numeric keys declared in map literals.
Most of such usages typically use the keys as if they were strings, so this
hack is likely safe.

A hack is used as opposed to overhauling all maps used since full support would
require overhauling all maps types to Map<Object, Object> which seems
overkill.

Behavior is also undefined for when defining same key of same number value and number value {'1': '1', 1: '2'}.

@chrisirhc chrisirhc changed the title add support for numeric keys in dictionary Draft: add support for numeric keys in dictionary Oct 26, 2022
@chrisirhc
Copy link
Contributor Author

This is a draft, and I intend to make some changes.

@chrisirhc chrisirhc marked this pull request as draft October 26, 2022 08:43
@chrisirhc chrisirhc marked this pull request as ready for review October 26, 2022 08:47
@chrisirhc chrisirhc changed the title Draft: add support for numeric keys in dictionary add support for numeric keys in dictionary Oct 26, 2022
@chrisirhc chrisirhc changed the title add support for numeric keys in dictionary add support for numeric keys in map literal Oct 26, 2022
This is a hack to support numeric keys declared in map literals.
Most of such usages typically use the keys as if they were strings, so this
hack is likely safe.

A hack is used as opposed to overhauling all maps used since full support would
require overhauling all maps types to `Map<Object, Object>` which seems
overkill.
@chrisirhc
Copy link
Contributor Author

Ok, changes are done and ready for review.

// This is a hack to treat numeric keys as string keys in the dictionary.
// In most cases this is adequate since the keys are typically treated as
// strings.
key = entryKey.eval(bindings, context).toString();
} else {
throw new TemplateStateException(
"Dict key must be a string or identifier, was: " + entryKey
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good to update the error message here to include the number as a key possibility, what do you think?

Suggested change
"Dict key must be a string or identifier, was: " + entryKey
"Dict key must be a string, or identifier, or a number, was: " + entryKey

@boulter
Copy link
Contributor

boulter commented Mar 25, 2024

superseded by #1152

@boulter boulter closed this Mar 25, 2024
@chrisirhc chrisirhc deleted the fix-numeric-key branch March 26, 2024 00:27
@chrisirhc
Copy link
Contributor Author

chrisirhc commented Mar 26, 2024

Ah apologies I forgot to get back to this.
Glad someone took the fix and addressed the feedback. 👍

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 this pull request may close these issues.

3 participants