Skip to content

Commit 38a57a1

Browse files
committed
Handle NamedTuple keys with quotes
1 parent d593217 commit 38a57a1

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

spec/serialization_spec.cr

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,24 @@ describe "MessagePack serialization" do
6464
end
6565

6666
it "does for NamedTuple" do
67-
data = Bytes[130, 161, 97, 1, 172, 64, 99, 111, 109, 112, 108, 101, 120, 95, 107, 101, 121, 165, 119, 111, 114, 107, 115]
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]
6868

69-
named_tuple = NamedTuple(a: Int32, "@complex_key": String).from_msgpack(data)
69+
named_tuple = NamedTuple(
70+
a: Int32,
71+
"@complex_key": String,
72+
%[key_with_"quotes"]: String,
73+
).from_msgpack(data)
7074

7175
named_tuple.should eq({
72-
a: 1,
73-
"@complex_key": "works",
76+
a: 1,
77+
"@complex_key": "works",
78+
%[key_with_"quotes"]: "also workz",
7479
})
75-
named_tuple.should be_a(NamedTuple(a: Int32, "@complex_key": String))
80+
named_tuple.should be_a NamedTuple(
81+
a: Int32,
82+
"@complex_key": String,
83+
%[key_with_"quotes"]: String,
84+
)
7685
end
7786

7887
it "does for Bytes" do

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)