From 650576200853f1efc0bd8ff6af64e7c0a6c92597 Mon Sep 17 00:00:00 2001 From: Adrian Esteban Madrid Date: Fri, 25 Jan 2019 16:33:10 -0700 Subject: [PATCH] Now hgetall returns a Hash(String => String) instead of an array of keys/values --- spec/redis_spec.cr | 9 +++++++-- src/redis/commands.cr | 11 ++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/spec/redis_spec.cr b/spec/redis_spec.cr index 3989f6d..fda3cdc 100644 --- a/spec/redis_spec.cr +++ b/spec/redis_spec.cr @@ -760,11 +760,16 @@ describe Redis do redis.hget("myhash", "a").should eq("434") end - it "#hgetall" do + it "#hgetall (found)" do redis.del("myhash") redis.hset("myhash", "a", "123") redis.hset("myhash", "b", "456") - redis.hgetall("myhash").should eq(["a", "123", "b", "456"]) + redis.hgetall("myhash").should eq({"a" => "123", "b" => "456"}) + end + + it "#hgetall (missing)" do + redis.del("myhash") + redis.hgetall("myhash").should eq({} of String => String) end it "#hdel" do diff --git a/src/redis/commands.cr b/src/redis/commands.cr index e00e1c5..69d07c2 100644 --- a/src/redis/commands.cr +++ b/src/redis/commands.cr @@ -934,10 +934,15 @@ class Redis # Returns all fields and values of the hash stored at key. # - # **Return value**: Array(String) of fields and their values stored in the hash, - # or an empty array when key does not exist. + # **Return value**: Hash(String => String) of fields and their values stored in the hash, + # or an empty hash when key does not exist. def hgetall(key) - string_array_command(["HGETALL", key.to_s]) + hsh = {} of String => String + result = string_array_command(["HGETALL", key.to_s]) + result.in_groups_of(2) do |(k, v)| + hsh[k.as(String)] = v.as(String) + end + hsh end # Removes the specified fields from the hash stored at key.