crystal-community / msgpack-crystal

MessagePack implementation in Crystal msgpack.org[Crystal]
136 stars 18 forks source link

Packing array of hashes #23

Closed waghanza closed 8 years ago

waghanza commented 8 years ago

Hello,

I want to write in a MessagePack::Packer.new a bunch of hashes to do this I use

array_of_hash.map {|h| packer.write(h)

but I have this error

Error in ./src/zaqar.cr:15: no overload matches 'MessagePack::Packer#write' with type Hash(String, Nil | String | Float32 | Float64 | Time | JSON::Any | Bool | Int32 | Int64 | Int16 | Slice(UInt8))
Overloads are:
 - MessagePack::Packer#write(value : Nil)
 - MessagePack::Packer#write(value : Bool)
 - MessagePack::Packer#write(value : String)
 - MessagePack::Packer#write(value : Symbol)
 - MessagePack::Packer#write(value : Float32 | Float64)
 - MessagePack::Packer#write(value : Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64)
 - MessagePack::Packer#write(value : Hash(Type, Type))
 - MessagePack::Packer#write(value : Array(Type))
 - MessagePack::Packer#write(value : Tuple)

However

array_of_hash.map {|h| packer.write(h : Hash(Type, Type)) }

is not yet supported by crystal

DO you know when this will be available ?

Regards,

kostya commented 8 years ago

array_of_hash.to_msgpack ?

or by hands

[1, {1 => 2}]

packer.write_array_start(2)
packer.write(1)
packer.write_hash_start(1)
packer.write(1)
packer.write(2)
benoist commented 8 years ago

I think the JSON::Any is throwing the error

benoist commented 8 years ago

And the slice

waghanza commented 8 years ago

@kostya seems not to work since I have to overloead matches @benoist new to crystal and compiled language, could you explain plz

benoist commented 8 years ago

Welcome to crystal :-). The array of hashes has got values of type JSON::Any and Slice(UInt8) which msgpack does not know how to convert. You can see the type of array_of_hashes by calling .class on it. It should fit the MessagePack::Type.

waghanza commented 8 years ago

@benoist type is Array(Hash(String, Nil | String | Float32 | Float64 | Time | JSON::Any | Bool | Int32 | Int64 | Int16 | Slice(UInt8))) array_of_hashes contain array of hash (String,String)

but type overload seem not te be available

benoist commented 8 years ago

Yes that's what I thought. There are two types that can't be MessagePacked. So either remove them from the hash or convert them to a supported type in the MessagePack::Type alias

waghanza commented 8 years ago

@benoist and what message pack could convert ? String an Int ?

benoist commented 8 years ago

MessagePack::Type = Nil | Bool | Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64 | Float32 | Float64 | String | Array(Type) | Hash(Type, Type)

benoist commented 8 years ago

Did you manage to do what you wanted?

waghanza commented 8 years ago

no, I have temporarily left msgpack (I' use json as my app is unstable)