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

Return empty string instead of nil in HGETALL, HVALS, HRANGEBYLEX responses #1315

Merged
merged 2 commits into from
Mar 12, 2023
Merged
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
6 changes: 3 additions & 3 deletions src/commands/cmd_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class CommandHVals : public Commander {
for (const auto &p : field_values) {
values.emplace_back(p.value);
}
*output = MultiBulkString(values);
*output = MultiBulkString(values, false);

return Status::OK();
}
Expand All @@ -306,7 +306,7 @@ class CommandHGetAll : public Commander {
kv_pairs.emplace_back(p.field);
kv_pairs.emplace_back(p.value);
}
*output = MultiBulkString(kv_pairs);
*output = MultiBulkString(kv_pairs, false);

return Status::OK();
}
Expand Down Expand Up @@ -350,7 +350,7 @@ class CommandHRangeByLex : public Commander {
kv_pairs.emplace_back(p.field);
kv_pairs.emplace_back(p.value);
}
*output = MultiBulkString(kv_pairs);
*output = MultiBulkString(kv_pairs, false);

return Status::OK();
}
Expand Down
27 changes: 27 additions & 0 deletions tests/gocase/unit/type/hash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,15 @@ func TestHash(t *testing.T) {
require.Equal(t, expect, actual)
})

t.Run("HVALS - field with empty string as a value", func(t *testing.T) {
require.NoError(t, rdb.HSet(ctx, "test-hash-1", "field1", "some-value").Err())
require.NoError(t, rdb.HSet(ctx, "test-hash-1", "field2", "").Err())

require.Equal(t, []string{"some-value", ""}, rdb.HVals(ctx, "test-hash-1").Val())

require.NoError(t, rdb.Del(ctx, "test-hash-1").Err())
})

t.Run("HGETALL - small hash}", func(t *testing.T) {
res := rdb.Do(ctx, "hgetall", "smallhash").Val().([]interface{})
mid := make(map[string]string)
Expand All @@ -365,6 +374,15 @@ func TestHash(t *testing.T) {
require.Equal(t, bighash, mid)
})

t.Run("HGETALL - field with empty string as a value", func(t *testing.T) {
require.NoError(t, rdb.HSet(ctx, "test-hash-1", "field1", "some-value").Err())
require.NoError(t, rdb.HSet(ctx, "test-hash-1", "field2", "").Err())

require.Equal(t, map[string]string{"field1": "some-value", "field2": ""}, rdb.HGetAll(ctx, "test-hash-1").Val())

require.NoError(t, rdb.Del(ctx, "test-hash-1").Err())
})

t.Run("HDEL and return value", func(t *testing.T) {
var rv []string
rv = append(rv, fmt.Sprintf("%d", rdb.HDel(ctx, "smallhash", "nokey").Val()))
Expand Down Expand Up @@ -742,5 +760,14 @@ func TestHash(t *testing.T) {
require.ErrorContains(t, rdb.Do(ctx, "HrangeByLex", "hashkey").Err(), "wrong number of arguments")
require.ErrorContains(t, rdb.Do(ctx, "HrangeByLex").Err(), "wrong number of arguments")
})

t.Run("HrangeByLex - field with empty string as a value", func(t *testing.T) {
require.NoError(t, rdb.HSet(ctx, "test-hash-1", "field1", "some-value").Err())
require.NoError(t, rdb.HSet(ctx, "test-hash-1", "field2", "").Err())

require.Equal(t, []interface{}{"field1", "some-value", "field2", ""}, rdb.Do(ctx, "HrangeByLex", "test-hash-1", "[a", "[z").Val())

require.NoError(t, rdb.Del(ctx, "test-hash-1").Err())
})
}
}