-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec.go
50 lines (38 loc) · 1.12 KB
/
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
47
48
49
50
package gcache
import (
"fmt"
"google.golang.org/protobuf/proto"
)
// RawBytesCodec sets the received bytes as-is to the target,
// whether it is a byte slice or a proto.Message.
// For proto.Message, it uses proto.Marshal and proto.Unmarshal.
type RawBytesCodec struct{}
// Marshal returns the received byte slice as is.
func (RawBytesCodec) Marshal(v any) ([]byte, error) {
if v == nil {
return nil, nil
}
if msg, ok := v.(proto.Message); ok {
return proto.Marshal(msg)
}
if bts, ok := v.(*[]byte); ok {
return *bts, nil
}
return nil, fmt.Errorf("failed to marshal: %v is not type of *[]byte, nor proto.Message", v)
}
// Unmarshal sets the received bytes as is to the target.
func (RawBytesCodec) Unmarshal(data []byte, v any) error {
if data == nil || v == nil {
return nil
}
if bts, ok := v.(proto.Message); ok {
return proto.Unmarshal(data, bts)
}
if bts, ok := v.(*[]byte); ok {
*bts = data
return nil
}
return fmt.Errorf("failed to unmarshal: %v is not type of *[]byte, nor proto.Message", v)
}
// Name returns the name of the codec.
func (RawBytesCodec) Name() string { return "gcache-raw-bytes" }