Skip to content

Commit

Permalink
Add RedisResult.AsBoolSlice()
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-assar committed Mar 17, 2024
1 parent 1aa8e70 commit 060dcbb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 37 deletions.
16 changes: 3 additions & 13 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,26 +753,16 @@ func (m *RedisMessage) AsFloatSlice() ([]float64, error) {
return s, nil
}

// AsBoolSlice checks if the message is a Redis array/set response, and converts it to []bool.
// Redis nil elements and other non-boolean elements will be represented as false.
// AsBoolSlice checks if the message is a redis array/set response, and converts it to []bool.
// Redis nil elements and other non-boolean elements will be present as false.
func (m *RedisMessage) AsBoolSlice() ([]bool, error) {
if m.typ != '*' {
panic(fmt.Sprintf("redis message type %c is not an array", m.typ))
}
values, err := m.ToArray()
if err != nil {
return nil, err
}
s := make([]bool, len(values))
for i, v := range values {
if len(v.string) != 0 {
s[i], err = strconv.ParseBool(v.string)
if err != nil {
s[i] = false
}
} else {
s[i] = v.integer != 0
}
s[i] = v.string == "1"
}
return s, nil
}
Expand Down
33 changes: 9 additions & 24 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,10 @@ func TestRedisResult(t *testing.T) {
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsBoolSlice(); err == nil {
t.Fatal("AsBoolSlice not failed as expected")
}
values := []RedisMessage{{string: "true", typ: '+'}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsBoolSlice(); !reflect.DeepEqual(ret, []bool{true}) {
values := []RedisMessage{{string: "1", typ: '+'}, {string: "0", typ: '+'}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsBoolSlice(); !reflect.DeepEqual(ret, []bool{true, false}) {
t.Fatal("AsBoolSlice not get value as expected")
}
values = []RedisMessage{{string: "false", typ: '+'}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsBoolSlice(); !reflect.DeepEqual(ret, []bool{false}) {
t.Fatal("AsBoolSlice not get value as expected")
}
values = []RedisMessage{{string: "notabool", typ: '+'}}
if _, err := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsBoolSlice(); err == nil {
t.Fatal("AsBoolSlice not failed as expected")
}
})

t.Run("AsMap", func(t *testing.T) {
Expand Down Expand Up @@ -1406,25 +1398,18 @@ func TestRedisMessage(t *testing.T) {
})

t.Run("AsBoolSlice", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsBoolSlice(); err == nil {
t.Fatal("AsBoolSlice not failed as expected")
}

defer func() {
if r := recover(); r == nil {
t.Fatal("AsBoolSlice did not panic as expected")
} else if !strings.Contains(r.(string), "redis message type _ is not an array") {
t.Fatal("AsBoolSlice did not panic with the expected message")
if !strings.Contains(recover().(string), "redis message type t is not a array") {
t.Fatal("AsBoolSlice not panic as expected")
}
}()
(&RedisMessage{typ: '_'}).AsBoolSlice()
(&RedisMessage{typ: 't'}).AsBoolSlice()
})

defer func() {
if r := recover(); r == nil {
t.Fatal("AsBoolSlice did not panic as expected")
} else if !strings.Contains(r.(string), "redis message type t is not an array") {
t.Fatal("AsBoolSlice did not panic with the expected message")
}
}()
(&RedisMessage{typ: 't'}).AsBoolSlice()

t.Run("AsMap", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsMap(); err == nil {
t.Fatal("AsMap not failed as expected")
Expand Down

0 comments on commit 060dcbb

Please sign in to comment.