Skip to content

Commit

Permalink
Table#delete and Table#has_key? argument must be string (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
spuun authored Dec 6, 2023
1 parent 4d56de5 commit 515fd9e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 7 additions & 2 deletions spec/table_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ describe AMQ::Protocol::Table do
it "supports #delete" do
t1 = AMQ::Protocol::Table.new({a: 1, b: "foo"})
t1.delete("a").should eq 1
t1["a"]?.should be_nil
t1.to_h.should eq({"b" => "foo"})
end

Expand All @@ -135,8 +136,12 @@ describe AMQ::Protocol::Table do

it "supports NamedTuple" do
t1 = AMQ::Protocol::Table.new({a: 1, b: "foo"})
t1.merge!({c: nil})
t1.to_h.should eq({"a" => 1, "b" => "foo", "c" => nil})
t1.merge!({c: nil, b: "bar"})
t1["a"].should eq 1
t1["b"].should eq "bar"
t1["c"].should eq nil
t1.size.should eq 3
t1.to_h.should eq({"a" => 1, "b" => "bar", "c" => nil})
end

it "supports Hash(String, Field)" do
Expand Down
16 changes: 13 additions & 3 deletions src/amq/protocol/table.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module AMQ
yield
end

def has_key?(key) : Bool
def has_key?(key : String) : Bool
@io.rewind
while @io.pos < @io.bytesize
if key == ShortString.from_io(@io)
Expand All @@ -67,6 +67,11 @@ module AMQ
false
end

@[Deprecated("key must be string")]
def has_key?(key)
has_key?(key.to_s)
end

def each(& : (String, Field) -> Nil)
@io.rewind
while @io.pos < @io.bytesize
Expand Down Expand Up @@ -162,7 +167,7 @@ module AMQ
true
end

def delete(key)
def delete(key : String)
ensure_writeable
@io.rewind
while @io.pos < @io.bytesize
Expand All @@ -179,6 +184,11 @@ module AMQ
nil
end

@[Deprecated("key must be string")]
def delete(key)
delete(key.to_s)
end

def to_io(io, format) : Nil
io.write_bytes(@io.bytesize.to_u32, format)
io.write @io.to_slice
Expand Down Expand Up @@ -235,7 +245,7 @@ module AMQ
ensure_writeable
@io.rewind
other.each do |key, value|
delete(key)
delete(key.to_s)
@io.skip_to_end
@io.write_bytes(ShortString.new(key.to_s))
write_field(value)
Expand Down

0 comments on commit 515fd9e

Please sign in to comment.