Skip to content

Commit

Permalink
Add table merge with table (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
spuun authored Sep 27, 2023
1 parent 04d657d commit efbcf0b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Add support for Table to be merged with another Table

## [1.1.10] - 2023-09-22

### Fixed
Expand Down
25 changes: 20 additions & 5 deletions spec/table_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,31 @@ describe AMQ::Protocol::Table do
t1.to_h.should eq({"b" => "foo"})
end

it "supports reject!" do
it "supports #reject!" do
t1 = AMQ::Protocol::Table.new({a: 1, b: "foo"})
t1.reject! { |k, v| k.in?("a") }
t1.to_h.should eq({"b" => "foo"})
end

it "supports merge!" 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})
describe "#merge!" do
it "supports Table" do
t1 = AMQ::Protocol::Table.new({a: 1, b: "foo"})
t2 = AMQ::Protocol::Table.new({c: nil})
t1.merge!(t2)
t1.to_h.should eq({"a" => 1, "b" => "foo", "c" => nil})
end

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})
end

it "supports Hash(String, Field)" do
t1 = AMQ::Protocol::Table.new({a: 1, b: "foo"})
t1.merge!({"c" => nil} of String => AMQ::Protocol::Field)
t1.to_h.should eq({"a" => 1, "b" => "foo", "c" => nil})
end
end

it "can add fields" do
Expand Down
4 changes: 2 additions & 2 deletions src/amq/protocol/table.cr
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ module AMQ
self
end

def merge!(hash : Hash(String, Field) | NamedTuple) : self
def merge!(other : Hash(String, Field) | NamedTuple | self) : self
ensure_writeable
@io.rewind
hash.each do |key, value|
other.each do |key, value|
delete(key)
@io.skip_to_end
@io.write_bytes(ShortString.new(key.to_s))
Expand Down

0 comments on commit efbcf0b

Please sign in to comment.