~ashkeel/kilovolt-client-go

d69b0f4a6aee154052e21f7493d21a2edc0f5ea3 — Ash Keel 3 years ago edc8d46 v2.0.0
Implement all v4 methods
3 files changed, 143 insertions(+), 4 deletions(-)

M client.go
M client_test.go
M cmd/kvcli/main.go
M client.go => client.go +71 -0
@@ 166,6 166,44 @@ func (s *Client) GetKey(key string) (string, error) {
	return resp.Data.(string), nil
}

func (s *Client) GetKeys(keys []string) (map[string]string, error) {
	resp, err := s.makeRequest(kv.Request{
		CmdName: kv.CmdReadBulk,
		Data: map[string]interface{}{
			"keys": keys,
		},
	})
	if err != nil {
		return nil, err
	}

	vals := resp.Data.(map[string]interface{})
	toReturn := make(map[string]string)
	for k, v := range vals {
		toReturn[k] = v.(string)
	}
	return toReturn, nil
}

func (s *Client) GetByPrefix(prefix string) (map[string]string, error) {
	resp, err := s.makeRequest(kv.Request{
		CmdName: kv.CmdReadPrefix,
		Data: map[string]interface{}{
			"prefix": prefix,
		},
	})
	if err != nil {
		return nil, err
	}

	vals := resp.Data.(map[string]interface{})
	toReturn := make(map[string]string)
	for k, v := range vals {
		toReturn[k] = v.(string)
	}
	return toReturn, nil
}

func (s *Client) GetJSON(key string, dst interface{}) error {
	resp, err := s.makeRequest(kv.Request{
		CmdName: kv.CmdReadKey,


@@ 196,6 234,21 @@ func (s *Client) SetKey(key string, data string) error {
	return err
}

func (s *Client) SetKeys(data map[string]string) error {
	// This is so dumb
	toSet := make(map[string]interface{})
	for k, v := range data {
		toSet[k] = v
	}

	_, err := s.makeRequest(kv.Request{
		CmdName: kv.CmdWriteBulk,
		Data:    toSet,
	})

	return err
}

func (s *Client) SetJSON(key string, data interface{}) error {
	serialized, err := jsoniter.ConfigFastest.MarshalToString(data)
	if err != nil {


@@ 213,6 266,24 @@ func (s *Client) SetJSON(key string, data interface{}) error {
	return err
}

func (s *Client) SetJSONs(data map[string]interface{}) error {
	toSet := make(map[string]interface{})
	for k, v := range data {
		serialized, err := jsoniter.ConfigFastest.MarshalToString(v)
		if err != nil {
			return err
		}
		toSet[k] = serialized
	}

	_, err := s.makeRequest(kv.Request{
		CmdName: kv.CmdWriteBulk,
		Data:    toSet,
	})

	return err
}

func (s *Client) SubscribeKey(key string) (chan KeyValuePair, error) {
	chn := make(chan KeyValuePair, 10)


M client_test.go => client_test.go +71 -3
@@ 39,13 39,12 @@ func TestCommands(t *testing.T) {
			t.Fatalf("returned value is different than expected, expected=%s got=%s", "test1234", val)
		}
	})

	type RandomStruct struct {
		Value int64
		Other string
	}
	t.Run("SetJSON", func(t *testing.T) {
		if err := client.SetJSON("test", RandomStruct{
		if err := client.SetJSON("testjson", RandomStruct{
			Value: 1234,
			Other: "wow!",
		}); err != nil {


@@ 54,7 53,7 @@ func TestCommands(t *testing.T) {
	})
	t.Run("GetJSON", func(t *testing.T) {
		var rnd RandomStruct
		if err := client.GetJSON("test", &rnd); err != nil {
		if err := client.GetJSON("testjson", &rnd); err != nil {
			t.Fatal("error getting JSON key", err.Error())
		}
		if rnd.Value != 1234 || rnd.Other != "wow!" {


@@ 62,6 61,75 @@ func TestCommands(t *testing.T) {
		}
	})

	t.Run("SetKeys", func(t *testing.T) {
		if err := client.SetKeys(map[string]string{
			"multi1": "value1",
			"multi2": "1234",
		}); err != nil {
			t.Fatal("error setting multiple keys", err.Error())
		}
	})

	t.Run("SetJSONs", func(t *testing.T) {
		if err := client.SetJSONs(map[string]interface{}{
			"multijson1": RandomStruct{
				Value: 1234,
				Other: "wow!",
			},
			"multijson2": RandomStruct{
				Value: 9999,
				Other: "AAAAA",
			},
		}); err != nil {
			t.Fatal("error setting multiple keys to JSON values", err.Error())
		}
	})

	t.Run("GetKeys", func(t *testing.T) {
		val, err := client.GetKeys([]string{"test", "multi2"})
		if err != nil {
			t.Fatal("error getting key list", err.Error())
		}
		testKey, ok := val["test"]
		if !ok {
			t.Fatal("expected response to contain test key but it doesn't")
		}
		if testKey != "test1234" {
			t.Fatal("test key has different value than expected")
		}
		testKey, ok = val["multi2"]
		if !ok {
			t.Fatal("expected response to contain multi2 key but it doesn't")
		}
		if testKey != "1234" {
			t.Fatal("multi2 key has different value than expected")
		}
	})

	t.Run("GetByPrefix", func(t *testing.T) {
		val, err := client.GetByPrefix("multi")
		if err != nil {
			t.Fatal("error getting keys by prefix", err.Error())
		}
		if len(val) < 4 {
			t.Fatal("returned less keys than expected")
		}
		testKey, ok := val["multi1"]
		if !ok {
			t.Fatal("expected response to contain multi1 key but it doesn't")
		}
		if testKey != "value1" {
			t.Fatal("multi1 key has different value than expected")
		}
		testKey, ok = val["multi2"]
		if !ok {
			t.Fatal("expected response to contain multi2 key but it doesn't")
		}
		if testKey != "1234" {
			t.Fatal("multi2 key has different value than expected")
		}
	})

	var chn chan KeyValuePair
	t.Run("SubscribeKey", func(t *testing.T) {
		var err error

M cmd/kvcli/main.go => cmd/kvcli/main.go +1 -1
@@ 7,7 7,7 @@ import (
	"os"
	"strings"

	kvclient "github.com/strimertul/kilovolt-client-go"
	kvclient "github.com/strimertul/kilovolt-client-go/v2"
)

func check(err error) {