Skip to content

Commit b00b6d3

Browse files
authored
Merge pull request #72 from jgaskins/fix-named-tuple-from-msgpack
Fix NamedTuple.from_msgpack with complex keys
2 parents 8de3341 + 38a57a1 commit b00b6d3

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

spec/serialization_spec.cr

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ describe "MessagePack serialization" do
6363
tuple.should be_a(Tuple(Int32, String))
6464
end
6565

66+
it "does for NamedTuple" do
67+
data = Bytes[131, 161, 97, 1, 172, 64, 99, 111, 109, 112, 108, 101, 120, 95, 107, 101, 121, 165, 119, 111, 114, 107, 115, 177, 107, 101, 121, 95, 119, 105, 116, 104, 95, 34, 113, 117, 111, 116, 101, 115, 34, 170, 97, 108, 115, 111, 32, 119, 111, 114, 107, 122]
68+
69+
named_tuple = NamedTuple(
70+
a: Int32,
71+
"@complex_key": String,
72+
%[key_with_"quotes"]: String,
73+
).from_msgpack(data)
74+
75+
named_tuple.should eq({
76+
a: 1,
77+
"@complex_key": "works",
78+
%[key_with_"quotes"]: "also workz",
79+
})
80+
named_tuple.should be_a NamedTuple(
81+
a: Int32,
82+
"@complex_key": String,
83+
%[key_with_"quotes"]: String,
84+
)
85+
end
86+
6687
it "does for Bytes" do
6788
data = UInt8[196, 3, 1, 2, 3]
6889
binary = Bytes.from_msgpack(data)

src/message_pack/from_msgpack.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,13 @@ def NamedTuple.new(pull : MessagePack::Unpacker)
180180

181181
{% for key, type in T %}
182182
if %var{key.id}.nil? && !::Union({{type}}).nilable?
183-
raise MessagePack::TypeCastError.new("Missing msgpack attribute: {{key}}", token.byte_number)
183+
raise MessagePack::TypeCastError.new("Missing msgpack attribute: #{ {{key.stringify}} }", token.byte_number)
184184
end
185185
{% end %}
186186

187187
{
188188
{% for key, type in T %}
189-
{{key}}: %var{key.id}.as({{type}}),
189+
{{key.stringify}}: %var{key.id}.as({{type}}),
190190
{% end %}
191191
}
192192
{% end %}

0 commit comments

Comments
 (0)