-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenc.go
94 lines (75 loc) · 2.06 KB
/
enc.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2020 The RediGo Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package redigo
import (
"bytes"
"encoding/gob"
"encoding/json"
gojson "github.com/goccy/go-json"
"github.com/vmihailenco/msgpack/v5"
)
// Encoder defines methods for encoding and decoding
// buffers into the cache store.
type Encoder interface {
Encode(value any) ([]byte, error)
Decode([]byte, any) error
}
// NewGobEncoder returns a new Gob encoder for RediGo.
func NewGobEncoder() Encoder {
return &gobEnc{}
}
// gobEnc implements the encoder interface.
type gobEnc struct{}
func (g gobEnc) Encode(value any) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(value)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (g gobEnc) Decode(data []byte, value any) error {
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
return dec.Decode(value)
}
// NewJSONEncoder returns a new Gob encoder for RediGo.
func NewJSONEncoder() Encoder {
return &jsonEnc{}
}
// jsonEnc implements the encoder interface.
type jsonEnc struct{}
func (j jsonEnc) Encode(value any) ([]byte, error) {
return json.Marshal(value)
}
func (j jsonEnc) Decode(data []byte, value any) error {
return json.Unmarshal(data, value)
}
// NewMessagePackEncoder returns a new Message Pack
// encoder for RediGo.
func NewMessagePackEncoder() Encoder {
return &msgEnc{}
}
// msgEnc implements the encoder interface.
type msgEnc struct{}
func (m msgEnc) Encode(value any) ([]byte, error) {
return msgpack.Marshal(value)
}
func (m msgEnc) Decode(data []byte, value any) error {
return msgpack.Unmarshal(data, value)
}
// NewGoJSONEncoder returns a new Go JSON
// encoder for RediGo.
func NewGoJSONEncoder() Encoder {
return &goJSONEnc{}
}
// goJSONEnc implements the encoder interface.
type goJSONEnc struct{}
func (g goJSONEnc) Encode(value any) ([]byte, error) {
return gojson.Marshal(value)
}
func (g goJSONEnc) Decode(data []byte, value any) error {
return gojson.Unmarshal(data, value)
}