daokoder / dao-modules

Dao Standard Modules
http://daovm.net
12 stars 5 forks source link

Missing support for enum in web.json.serialize() #37

Closed dumblob closed 9 years ago

dumblob commented 9 years ago
load web.json
y = {(enum<a,b>)$a -> 5}
io.writeln(json.serialize(y) {
  switch (X) type {
    default: return "some_enum_symbol"
  }
})
[[Error::Type]] --- Invalid type:
Non-string key in map/object
Raised by:  serialize(), from namespace "/usr/lib/dao/modules/web/libdao_json.so::json";
In code snippet:
     12 :  MOVE_PP     :     5 ,     0 ,    10 ;     1;   y
>>   13 :  MCALL       :     8 ,  2050 ,    11 ;     1;   json. serial...(y)
     14 :  GOTO        :     0 ,    22 ,    69 ;     1;   {switch(X)type{default: ...
Called by:  __main__(), at instruction 13 in line 1 in file "command line codes";

Any map is considered as map<string, any> and if the key is not string, the whole map is considered unserializable, which is a wrong assumption.

dumblob commented 9 years ago

IMHO, any key type should be allowed and if it's not one of the out-of-box serializable types, again, the code section should be called.

Btw, the error Non-string key in map/object should be more descriptive - at least say which type was the non-string key of.

Night-walker commented 9 years ago

Supporting arbitrary keys is problematic because the code section for json::serialize() is not fitted for handling object keys (the return type is required to be different for this case).

For now, I just changed it so that a map with non-string keys is passed to the code section as a whole. You can then .associate() it to anything appropriate. And thus those error message should no longer pop up.

Night-walker commented 9 years ago

Unless there are other suggestions, I think there is nothing else to do on this issue.

dumblob commented 9 years ago
load web.json
y = {
  (enum<a,b>)$a -> 5,
             $b -> 7,
}
io.writeln(
  json.serialize(y) { [obj]
    switch (obj) type {
      #case map<enum, @V>:  # why doesn't this match the object?
      case map<@K, @V>:
        return (json.Data)obj.associate { ( ((string)X)[1:], (int)Y ) }
      default:
        return (json.Data)"some mistake!"
    }
  }
)
{
{
                "a": 5,
                "b": 7
        }

I'd expect only one opening curly bracket and the $pretty indentation should be probably the same for opening and closing bracket.

Night-walker commented 9 years ago

Fixed.