wapc / as-msgpack

MessagePack package for AssemblyScript
Apache License 2.0
13 stars 7 forks source link

Auto convert numeric types when decoding #8

Closed ifyouseewendy closed 3 years ago

ifyouseewendy commented 3 years ago

👋 Hey guys, thanks for making the as-msgpack package. I'm running into a small issue and I created this PR to propose a fix.

Context

In a dynamic type language, which doesn't have proper type annotation over i8/i16/i32/i64, u8/u16/u32/u64 or f32/f64, it encodes a numeric value simply for what it fits. Or in the case when we transform a numeric value from JSON to MsgPack, there is no way to specify the exact type the value is. In these cases, the current decoder fails to parse the value.

For example,

{ "value": 128 }

When transforming the JSON value to Msgpack, it becomes

[ ..., 204, 128 ]
       ----
         |
         |-> 0xcc which is flag byte for u8

If the value is tagged as i32 in AS code, it fails to properly decode when using decoder.readInt32(), which results in a bad prefix error.

This commit

This commit updates the decoder to auto converts compatible values and throws an error on value overflow and underflow.

ifyouseewendy commented 3 years ago

@pkedy Hey Phil, thanks for the review. Would you mind publishing a new version for this change as well? So that we can reference it from npm.

For the tinygo, sure thing, if you are not in a hurry, I can take a stab next week

pkedy commented 3 years ago

@ifyouseewendy Published @wapc/as-msgpack as 0.1.12.

Thanks again for the PR!