Skip to content

Commit

Permalink
Encode JSON value
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Jan 21, 2021
1 parent 2ec7c9f commit 1b8dbdd
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"encoding/json"
"fmt"
"io"
"strings"
Expand All @@ -13,9 +14,19 @@ import (
type Client interface {
io.Closer

// Get fetches a value from the specified path.
Get(path ...string) Value

// GetBulk fetches multiple values from the store simultaneously.
GetBulk(paths [][]string) ([]Value, error)
Set(val string, path ...string) error

// Set converts a value into a JSON string and stores it.
Set(val interface{}, path ...string) error

// SetRaw stores a JSON-encoded value.
SetRaw(val string, path ...string) error

// Del deletes a value.
Del(path ...string) error
}

Expand Down Expand Up @@ -75,7 +86,16 @@ func (c *client) GetBulk(paths [][]string) ([]Value, error) {
return values, nil
}

func (c *client) Set(val string, path ...string) error {
func (c *client) Set(val interface{}, path ...string) error {
str, err := json.Marshal(val)
if err != nil {
return fmt.Errorf("json encode: %w", err)
}

return c.SetRaw(string(str), path...)
}

func (c *client) SetRaw(val string, path ...string) error {
_, err := c.cl.Set(context.Background(), &proto.SetRequest{
Path: &proto.Path{Parts: path},
Value: &proto.Data{Json: val},
Expand Down

0 comments on commit 1b8dbdd

Please sign in to comment.