Skip to content

add support for numeric keys in map literal - #934

Closed
chrisirhc wants to merge 1 commit into
HubSpot:masterfrom
chrisirhc:fix-numeric-key
Closed

add support for numeric keys in map literal#934
chrisirhc wants to merge 1 commit into
HubSpot:masterfrom
chrisirhc:fix-numeric-key

Conversation

@chrisirhc

@chrisirhcchrisirhc commented Oct 25, 2022

Copy link
Copy Markdown
Contributor

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'}.

@chrisirhcchrisirhc changed the title add support for numeric keys in dictionaryDraft: add support for numeric keys in dictionaryOct 26, 2022
@chrisirhc

Copy link
Copy Markdown
ContributorAuthor

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
@chrisirhcchrisirhc changed the title Draft: add support for numeric keys in dictionaryadd support for numeric keys in dictionaryOct 26, 2022
@chrisirhcchrisirhc changed the title add support for numeric keys in dictionaryadd support for numeric keys in map literalOct 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
Copy Markdown
ContributorAuthor

Ok, changes are done and ready for review.

key = entryKey.eval(bindings, context).toString();
} else {
throw new TemplateStateException(
"Dict key must be a string or identifier, was: " + entryKey

Copy link
Copy Markdown
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
Copy Markdown
Contributor

superseded by #1152

@boulterboulter closed this Mar 25, 2024
@chrisirhc
chrisirhc deleted the fix-numeric-key branch March 26, 2024 00:27
@chrisirhc

chrisirhc commented Mar 26, 2024

Copy link
Copy Markdown
ContributorAuthor

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

Sign up for freeto 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

@chrisirhc@boulter@samukce