From efbcf0b44918c0a56edf112e26bddb95df2ecbcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20B=C3=B6rjesson?= Date: Wed, 27 Sep 2023 09:50:26 +0200 Subject: [PATCH] Add table merge with table (#15) --- CHANGELOG.md | 2 ++ spec/table_spec.cr | 25 ++++++++++++++++++++----- src/amq/protocol/table.cr | 4 ++-- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d295730..2873424 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/spec/table_spec.cr b/spec/table_spec.cr index 983b915..9a763fb 100644 --- a/spec/table_spec.cr +++ b/spec/table_spec.cr @@ -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 diff --git a/src/amq/protocol/table.cr b/src/amq/protocol/table.cr index 527f171..fd3181c 100644 --- a/src/amq/protocol/table.cr +++ b/src/amq/protocol/table.cr @@ -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))