-
Notifications
You must be signed in to change notification settings - Fork 11
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 JS Map support for encoding & decoding #5
Conversation
src/Decoder.ts
Outdated
return typeof key === "string" || typeof key === "number" || typeof key === "bigint" || key instanceof Uint8Array; | ||
} | ||
// Plain objects support a more limited set of key types | ||
return typeof key === "string"; |
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.
This is a breaking change, should we keep the existing logic looking for number
too?
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 pretty strange to me that the upstream library was getting away with just coercing number keys to strings on objects, so I wanted to do away with that behavior now that there's a better alternative.
Although, since it is a breaking change, I think I'd be willing to add another decoding option that could control that behavior if it's really what you wanted, since this has been an established practice in the upstream library for a while.
src/Decoder.ts
Outdated
@@ -650,7 +675,7 @@ export class Decoder<ContextType = undefined> { | |||
} | |||
|
|||
private decodeString(byteLength: number, headerOffset: number): string | Uint8Array { | |||
if (!this.useRawBinaryStrings || this.stateIsMapKey()) { | |||
if (!this.useRawBinaryStrings || (!this.useMap && this.stateIsMapKey())) { |
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.
If I read this right, does it mean if you are using maps and you set useRawBinaryStrings
then it will use a uint8array for the map keys? I wonder if we should allow that to be switchable?
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.
That's correct, and your suggestion makes sense
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.
Added two options for this in 7342b02
The way I see it, if you have number keys it *works* with in-built objects so fair to support…On 5 Mar 2024, at 11:22 pm, Jason Paulos ***@***.***> wrote:
@jasonpaulos commented on this pull request.
In src/Decoder.ts:
-const isValidMapKeyType = (key: unknown): key is MapKeyType => {
- return typeof key === "string" || typeof key === "number";
-};
+function isValidMapKeyType(key: unknown, useMap: boolean): key is MapKeyType {
+ if (useMap) {
+ return typeof key === "string" || typeof key === "number" || typeof key === "bigint" || key instanceof Uint8Array;
+ }
+ // Plain objects support a more limited set of key types
+ return typeof key === "string";
It's pretty strange to me that the upstream library was getting away with just coercing number keys to strings on objects, so I wanted to do away with that behavior now that there's a better alternative.
Although, since it is a breaking change, I think I'd be willing to add another decoding option that could control that behavior if it's really what you wanted, since this has been an established practice in the upstream library for a while.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: ***@***.***>
|
…coding raw string keys/values
Here's why I don't like decoding number keys on plain objects:
Since it does happen to work with plain objects, it was a good way to offer partial support for number map keys. But now that this PR introduces full support, I don't think the advantages for enabling it by default outweigh the cons I listed above. For backwards compatibility, I did add the |
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.
I haven't spent much time in the tests yet.
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.
I didn't see an floating point tests, those could be useful in the abstract, but of course Algorand doesn't use them.
Adds support for encoding and decoding
Map
objects, as an alternative to plain javascript objects.The primary advantage is that this adds much better non-string type support for map keys.