-
Notifications
You must be signed in to change notification settings - Fork 170
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
Conversation
This is a draft, and I intend to make some changes. |
9137419
to
6e2f675
Compare
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.
6e2f675
to
29bdbd6
Compare
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 |
There was a problem hiding this comment.
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?
"Dict key must be a string or identifier, was: " + entryKey | |
"Dict key must be a string, or identifier, or a number, was: " + entryKey |
superseded by #1152 |
Ah apologies I forgot to get back to this. |
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 seemsoverkill.
Behavior is also undefined for when defining same key of same number value and number value
{'1': '1', 1: '2'}
.