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

Implement the final 72 commands of Cmdable #26

Merged
merged 4 commits into from
Apr 29, 2022
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
35 changes: 35 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ func (r RedisResult) AsStrMap() (map[string]string, error) {
return r.val.AsStrMap()
}

// AsIntMap delegates to RedisMessage.AsIntMap
func (r RedisResult) AsIntMap() (map[string]int64, error) {
if err := r.Error(); err != nil {
return nil, err
}
return r.val.AsIntMap()
}

// ToMap delegates to RedisMessage.ToMap
func (r RedisResult) ToMap() (map[string]RedisMessage, error) {
if err := r.Error(); err != nil {
Expand Down Expand Up @@ -526,6 +534,33 @@ func (m *RedisMessage) AsStrMap() (map[string]string, error) {
panic(fmt.Sprintf("redis message type %c is not a map/array/set", typ))
}

// AsStrMap check if message is a redis map/array/set response, and convert to map[string]int64.
// Non int or string value will be ignored, including nil value.
func (m *RedisMessage) AsIntMap() (map[string]int64, error) {
if err := m.Error(); err != nil {
return nil, err
}
if m.typ == '%' || m.typ == '*' || m.typ == '~' {
var err error
r := make(map[string]int64, 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], err = strconv.ParseInt(v.string, 0, 64)
if err != nil {
return nil, err
}
} else if k.typ == ':' {
r[k.string] = v.integer
}
}
return r, nil
}
typ := m.typ
panic(fmt.Sprintf("redis message type %c is not a map/array/set", typ))
}

// ToMap check if message is a redis map response, and return it
func (m *RedisMessage) ToMap() (map[string]RedisMessage, error) {
if m.typ == '%' {
Expand Down
Loading