Created
July 23, 2020 19:06
-
-
Save SegFaultAX/4c4e3fc2a273c4e881503417314bd96b to your computer and use it in GitHub Desktop.
Handling SliceCmd in go-redis [Go]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"github.com/go-redis/redis/v8" | |
) | |
type ( | |
Sentinel struct { | |
Name string | |
Addr string | |
} | |
KeyVal struct { | |
Key string | |
Val string | |
} | |
) | |
func main() { | |
ctx := context.Background() | |
r := redis.NewSentinelClient(&redis.Options{ | |
Addr: "dbatest-db-redis-pp-rs-02.qasql.opentable.com:26379", | |
}) | |
res, err := r.Ping(ctx).Result() | |
fmt.Println(res, err) | |
//ms, err := r.Masters(ctx).Result() | |
cmd := r.Sentinels(ctx, "dbatest") | |
kvs, err := ToKeyVal(cmd) | |
if err != nil { | |
log.Fatal(err) | |
} | |
var ss []Sentinel | |
for _, kv := range kvs { | |
s := Sentinel{} | |
for _, v := range kv { | |
switch v.Key { | |
case "ip": | |
s.Addr = v.Val | |
case "name": | |
s.Name = v.Val | |
} | |
} | |
ss = append(ss, s) | |
} | |
fmt.Println(ss) | |
} | |
func ToKeyVal(cmd *redis.SliceCmd) ([][]KeyVal, error) { | |
var res [][]KeyVal | |
xss, err := cmd.Result() | |
if err != nil { | |
return nil, err | |
} | |
for _, xs := range xss { | |
xs := xs.([]interface{}) | |
var kvs []KeyVal | |
for i := 0; i < len(xs)/2; i += 2 { | |
key := xs[i].(string) | |
val := xs[i+1].(string) | |
kvs = append(kvs, KeyVal{ | |
Key: key, | |
Val: val, | |
}) | |
} | |
res = append(res, kvs) | |
} | |
return res, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment