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 Data.Aeson.Key.length #1123

Closed
sjshuck opened this issue Dec 29, 2024 · 3 comments
Closed

Add Data.Aeson.Key.length #1123

sjshuck opened this issue Dec 29, 2024 · 3 comments

Comments

@sjshuck
Copy link

sjshuck commented Dec 29, 2024

The JSON spec requires UTF-8 encoding (source). So the notion of the length of a key, defined as number of code points, seems well-defined.

The best way I've found is Data.Text.length . Data.Aeson.Key.toText which requires keeping up with current (unspecified) implementation to avoid extra work, plus adding text to explicit dependencies if not already there.

Happy to toss in a PR if deemed reasonable.

@phadej
Copy link
Collaborator

phadej commented Jan 3, 2025

Why you need to know the key length.

If you do something text-y with keys, then IMO conversion to Text is a way to go. Generally keys are "opaque" values...

EDIT: also length is O(n) operation, and I feel uncomfortable adding "slow" functions. I bet someone will then ask whether it can be made O(1) etc, a development I don't want to engage with.

@sjshuck
Copy link
Author

sjshuck commented Jan 3, 2025

Why you need to know the key length.

For example, I want to validate labels to add to a resource, and label keys are capped at 63 characters, but I don't want to do any more analysis than that, and I'm only passing ByteStrings around and don't want to mess with Text.

Generally keys are "opaque" values

which are specified as UTF-8, that is a ceiling to our opacity

I feel uncomfortable adding "slow" functions

Aeson is already generating intermediate data Value = ... | String !Text | .... And length of UTF-8 is inherently O(n).

I dont want to engage with

You are already engaging with this small PR, I imagine you will have no problem saying "no" to that person.

@phadej
Copy link
Collaborator

phadej commented Jan 3, 2025

String !Text 

that is textual value. Those are not keys.

SOrry. I think the way to do it is to explicitly convert to Text.

@phadej phadej closed this as completed Jan 3, 2025
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.

2 participants