-
Notifications
You must be signed in to change notification settings - Fork 5
/
codec.go
46 lines (35 loc) · 1017 Bytes
/
codec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package redis
import (
"encoding/json"
"github.com/kvtools/valkeyrie/store"
)
// Codec KVPair persistence interface.
type Codec interface {
Encode(kv *store.KVPair) (string, error)
Decode(b []byte, kv *store.KVPair) error
}
// RawCodec is a simple codec to read and write string.
type RawCodec struct{}
// Encode a KVPair to a string.
func (c RawCodec) Encode(kv *store.KVPair) (string, error) {
if kv == nil {
return "", nil
}
return string(kv.Value), nil
}
// Decode a byte slice to a KVPair.
func (c RawCodec) Decode(b []byte, kv *store.KVPair) error {
kv.Value = b
return nil
}
// JSONCodec is a simple codec to read and write valkeyrie JSON object.
type JSONCodec struct{}
// Encode a KVPair to a valkeyrie JSON object.
func (c JSONCodec) Encode(kv *store.KVPair) (string, error) {
b, err := json.Marshal(kv)
return string(b), err
}
// Decode a byte slice of valkeyrie JSON object to a KVPair.
func (c JSONCodec) Decode(b []byte, kv *store.KVPair) error {
return json.Unmarshal(b, kv)
}