From 7dcf6bfc8c650468f76efe66eab45ee88f386da5 Mon Sep 17 00:00:00 2001 From: guzeng Date: Tue, 8 Jun 2021 10:16:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0hgetall=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hash.go | 38 ++++++++++++++++++++++++++++++++++++++ hash_test.go | 37 ++++++++++++++++++++----------------- 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/hash.go b/hash.go index 0d1ef67..fe54af0 100644 --- a/hash.go +++ b/hash.go @@ -1,6 +1,9 @@ package redisrpc import ( + "encoding/json" + "log" + "github.com/golang/protobuf/proto" ) @@ -75,3 +78,38 @@ func HDel(key string, field string, url ...string) (int64, error) { return res.GetRet(), nil } + +/** + * 全部 + */ +func HGetAll(key string, url ...string) ([]map[string]string, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return []map[string]string{}, err + } + defer conn.Close() + + req := &GetRequest{proto.String(key), nil} + + res := &HGetListResponse{} + + err = conn.HGetAll(req, res) + + if err != nil { + return []map[string]string{}, err + } + + value := res.GetList() + + var list []map[string]string + + err = json.Unmarshal(value, &list) + + if err != nil { + log.Println("json unmarshal error:", err) + return []map[string]string{}, err + } + return list, nil +} diff --git a/hash_test.go b/hash_test.go index ad5adec..b95d5da 100644 --- a/hash_test.go +++ b/hash_test.go @@ -10,28 +10,28 @@ func Test_HSet(t *testing.T) { // val := map[string]interface{}{"id": "123", "name": "这是一个测试", "dis": "xxx"} - reply, err := HSet("testing", "test", "test1") + // reply, err := HSet("testing", "test", "test1") - t.Log(reply) - t.Log(err) + // t.Log(reply) + // t.Log(err) - ret, err := HGetString("testing", "test") + // ret, err := HGetString("testing", "test") - t.Log(ret) - if err == nil { - t.Log("nil", err) - } else { - t.Log("error:", err) - } + // t.Log(ret) + // if err == nil { + // t.Log("nil", err) + // } else { + // t.Log("error:", err) + // } - del_ret, err := HDel("testing", "test") + // del_ret, err := HDel("testing", "test") - t.Log(del_ret) - if err == nil { - t.Log("nil", err) - } else { - t.Log("error:", err) - } + // t.Log(del_ret) + // if err == nil { + // t.Log("nil", err) + // } else { + // t.Log("error:", err) + // } // ret2, err := HSetExpire("testing2", 200) // t.Log(ret2) // t.Log(err) @@ -49,4 +49,7 @@ func Test_HSet(t *testing.T) { // t.Log(string(val)) // } // t.Log(err) + + ret, err := HGetAll("testing2") + t.Log(ret, err) }