Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hgetall returns a hash #77

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions spec/redis_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/redis/commands.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down