Decode is missing nil handling / common basic coercions
As per the initial comment on #21 I noticed that Decode will not handle quite a few common cases where there may need to be some basic coercions.
Of particular note in reference to #21 is the fact that a nil in any value coming from mruby will fail at one of the unknown type checks.
For nil specifically I think that using zero types would be most appropriate.
But there are additional cases that it would be nice to handle for completeness.
Some examples:
- decodeString doesn't handle floats or boolean
- decodeFloat doesn't handle fixnum
- decodeInt doesn't handle float (not sure if we should handle this given information loss)
- Nothing handles nil
I propose a table of coercions is produced on this issue and an implementation can stem from that.
Here is a first (fairly arbitrary) stab aiming to keep it simple.
Horizontal types are ruby Vertical types are golang
Array / map conversions will follow key / value type conversion rules for each element. (e.g. map[string]float would follow string conversion rules for keys and float conversion rules for values).
Structs, user types & enums are not covered due to potential complexity of mapping.
I'm not sure if float -> int should be allowed due to precision loss. Dynamic languages typically allow this though.
It's also quite strict and does not allow (for instance) Fixnum -> bool conversion.
| TrueClass | FalseClass | NilClass | String | Fixnum | Bignum | Float | Range | Array | Hash | Symbol | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Float | invalid | invalid | ZeroType | atof | FloatValue | FloatValue | FloatValue | invalid | invalid | invalid | invalid |
| String | "true" | "false" | ZeroType | StringValue | StringValue | StringValue | StringValue | invalid | invalid | invalid | StringValue |
| Array | invalid | invalid | ZeroType | invalid | invalid | invalid | invalid | per-elem | per-elem | invalid | invalid |
| Map | invalid | invalid | ZeroType | invalid | invalid | invalid | invalid | invalid | invalid | per-elem | invalid |
| Int | invalid | invalid | ZeroType | atoi | IntValue | IntValue | ? | invalid | invalid | per-elem | invalid |
| Bool | true | false | ZeroType | invalid | invalid | invalid | invalid | invalid | invalid | invalid | invalid |
Due to the breadth of work implementing this will require I will await feedback before proceeding.