From 3990f4f2c498d833b483996cf684429627cb879d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20H=C3=B6rberg?= Date: Tue, 30 Jul 2024 14:46:22 +0200 Subject: [PATCH] Etcd#get_prefix returns a hash --- spec/etcd_spec.cr | 4 ++-- src/lavinmq/etcd.cr | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/spec/etcd_spec.cr b/spec/etcd_spec.cr index 6c22c54001..fe69c513dd 100644 --- a/spec/etcd_spec.cr +++ b/spec/etcd_spec.cr @@ -21,7 +21,7 @@ describe LavinMQ::Etcd do etcd.put("foo/a", "bar") etcd.put("foo/b", "bar") etcd.put("fou/c", "bar") - etcd.get_prefix("foo").should eq %w(bar bar) + etcd.get_prefix("foo").should eq Hash{"foo/a" => "bar", "foo/b" => "bar"} end end @@ -33,7 +33,7 @@ describe LavinMQ::Etcd do etcd.put("foo/b", "bar") etcd.put("fou/c", "bar") etcd.del_prefix("foo").should eq 2 - etcd.get_prefix("foo").should eq %w() + etcd.get_prefix("foo").empty?.should be_true etcd.get("foo/a").should be_nil end end diff --git a/src/lavinmq/etcd.cr b/src/lavinmq/etcd.cr index 57b6116a8c..d8cab4ec85 100644 --- a/src/lavinmq/etcd.cr +++ b/src/lavinmq/etcd.cr @@ -19,7 +19,7 @@ module LavinMQ end # Get all keys with a prefix - def get_prefix(prefix) : Array(String) + def get_prefix(prefix) : Hash(String, String) range_end = prefix.to_slice.dup len = range_end.bytesize (len - 1).downto(0) do |i| @@ -28,13 +28,15 @@ module LavinMQ end range_end = len.zero? ? Bytes[0] : range_end[0, len] # drop ending null values json = post("/v3/kv/range", %({"key":"#{Base64.strict_encode prefix}","range_end":"#{Base64.strict_encode range_end}","limit":0,"serializable":true})) + result = Hash(String, String).new if kvs = json["kvs"]? - kvs.as_a.map do |kv| - Base64.decode_string kv["value"].as_s + kvs.as_a.each do |kv| + key = Base64.decode_string kv["key"].as_s + value = Base64.decode_string kv["value"].as_s + result[key] = value end - else - Array(String).new(0) end + result end def put(key, value) : String?