Skip to content

Commit

Permalink
Table#delete and Table#has_key? argument must be string
Browse files Browse the repository at this point in the history
In public methods `key` is typed as `String`, except for `delete` and
`has_key?`. This will type `key` as `String` and adds deprecated method
overloads that supports "any" key.
  • Loading branch information
spuun committed Nov 27, 2023
1 parent 4d56de5 commit 801167f
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,12 @@ module AMQ
yield
end

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

def has_key?(key : String) : Bool
@io.rewind
while @io.pos < @io.bytesize
if key == ShortString.from_io(@io)
Expand Down Expand Up @@ -162,7 +167,12 @@ module AMQ
true
end

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

def delete(key : String)
ensure_writeable
@io.rewind
while @io.pos < @io.bytesize
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 801167f

Please sign in to comment.