Skip to content

Commit

Permalink
feat: allow AsStrMap to convert redis map response
Browse files Browse the repository at this point in the history
  • Loading branch information
rueian committed Jan 22, 2022
1 parent aa43808 commit 6cd2aab
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ The Opt-In mode of server-assisted client side caching is always enabled, and ca
an explicit client side TTL.

```golang
c.DoCache(ctx, c.B().Hmget().Key("myhash").Field("1", "2").Cache(), time.Minute).AsStrMap()
c.DoCache(ctx, c.B().Hmget().Key("myhash").Field("1", "2").Cache(), time.Minute).ToArray()
```

An explicit client side TTL is required because redis server may not send invalidation message in time when
Expand Down
22 changes: 12 additions & 10 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,22 +316,24 @@ func (m *RedisMessage) AsMap() (map[string]RedisMessage, error) {
return toMap(values), nil
}

// AsStrMap check if message is a redis array/set response, and convert to map[string]string.
// AsStrMap check if message is a redis map/array/set response, and convert to map[string]string.
// Non string value will be ignored, including nil value.
func (m *RedisMessage) AsStrMap() (map[string]string, error) {
values, err := m.ToArray()
if err != nil {
if err := m.Error(); err != nil {
return nil, err
}
r := make(map[string]string, len(values)/2)
for i := 0; i < len(values); i += 2 {
k := values[i]
v := values[i+1]
if (k.typ == '$' || k.typ == '+') && (v.typ == '$' || v.typ == '+' || len(v.string) != 0) {
r[k.string] = v.string
if m.typ == '%' || m.typ == '*' || m.typ == '~' {
r := make(map[string]string, len(m.values)/2)
for i := 0; i < len(m.values); i += 2 {
k := m.values[i]
v := m.values[i+1]
if (k.typ == '$' || k.typ == '+') && (v.typ == '$' || v.typ == '+' || len(v.string) != 0) {
r[k.string] = v.string
}
}
return r, nil
}
return r, nil
panic(fmt.Sprintf("redis message type %c is not a map/array/set", m.typ))
}

// ToMap check if message is a redis map response, and return it
Expand Down
2 changes: 1 addition & 1 deletion message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func TestRedisMessage(t *testing.T) {
}

defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a array") {
if !strings.Contains(recover().(string), "redis message type t is not a map/array/set") {
t.Fatal("AsMap not panic as expected")
}
}()
Expand Down
4 changes: 2 additions & 2 deletions rueidis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func ExampleClient_do() {

client.Do(ctx, client.B().Get().Key("k").Build()).AsInt64()

client.Do(ctx, client.B().Hmget().Key("h").Field("a", "b").Build()).AsStrMap()
client.Do(ctx, client.B().Hmget().Key("h").Field("a", "b").Build()).ToArray()

client.Do(ctx, client.B().Scard().Key("s").Build()).ToInt64()

Expand All @@ -139,7 +139,7 @@ func ExampleClient_doCache() {

client.DoCache(ctx, client.B().Get().Key("k").Cache(), time.Minute).AsInt64()

client.DoCache(ctx, client.B().Hmget().Key("h").Field("a", "b").Cache(), time.Minute).AsStrMap()
client.DoCache(ctx, client.B().Hmget().Key("h").Field("a", "b").Cache(), time.Minute).ToArray()

client.DoCache(ctx, client.B().Scard().Key("s").Cache(), time.Minute).ToInt64()

Expand Down

0 comments on commit 6cd2aab

Please sign in to comment.