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

fix: [maps.Flatten] better handle keys with delimiter character via escape sequence #328

Closed
wants to merge 2 commits into from

Conversation

crandles
Copy link

@crandles crandles commented Sep 30, 2024

If a map key contains the given delimiter, it is not possible to unflatten it back to its original state.

To fix, escape the keys for the delimiter and an escape sequence character. Additionally, extend unflatten to handle the equivalent un-escape.

@knadh
Copy link
Owner

knadh commented Oct 1, 2024

Thanks for the PR. But if . is a delimiter, it shouldn't be used as a part of a key to begin with.

In these two cases, say, a, b, c and a, b.c, in the API when one does ko.String("a.b.c"), how does one distinguish the hierarchy?

@crandles
Copy link
Author

crandles commented Oct 1, 2024

I'll add a test for that to verify (I haven't used those types much).

I think the goal would be to have that accessible via the escaped character sequence: a.b.c would target a,b,c, a.b~1c would target a, b.c

But if . is a delimiter, it shouldn't be used as a part of a key to begin with.

I'm working with user-supplied content which may have a wide array of input -- it isn't easy to tell what delimiter would be safe to use, which is why I thought to escape the keys.

If this isn't something you're interested in, I can close this.

@crandles
Copy link
Author

crandles commented Oct 1, 2024

Looks like my test is failing, so I'll move this back to draft. Let me know if this is something that could merge, else I'll just keep this as a fork.


as a note, the escape sequence is similar to how JSON Pointers work with special characters:

The characters ~ and / have special meanings in JSON Pointer, so if a key in a JSON object has these characters, ~ needs to be escaped as ~0, and / needs to be escaped as ~1.

Except instead of /, we care about delim

@crandles crandles marked this pull request as draft October 1, 2024 13:01
@knadh
Copy link
Owner

knadh commented Oct 1, 2024

I think this is a niche usecase, escaping delimiter in config keys in this manner. I don’t think this is ideal for the core.

@crandles crandles closed this Oct 1, 2024
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.

2 participants