-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.go
More file actions
49 lines (37 loc) · 918 Bytes
/
codec.go
File metadata and controls
49 lines (37 loc) · 918 Bytes
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
package sokv
import (
"encoding/binary"
"encoding/json"
)
var (
_ Codec[[]byte] = new(BytesCodec)
_ Codec[string] = new(JsonTypeCodec[string])
)
type Codec[T any] interface {
Unmarshal(data []byte, v *T) error
Marshal(v *T) ([]byte, error)
}
type BytesCodec struct{}
func (b BytesCodec) Unmarshal(data []byte, v *[]byte) error {
*v = data
return nil
}
func (b BytesCodec) Marshal(v *[]byte) ([]byte, error) {
return *v, nil
}
type Uint64Codec struct{}
func (u Uint64Codec) Unmarshal(data []byte, v *uint64) error {
*v = binary.BigEndian.Uint64(data)
return nil
}
func (u Uint64Codec) Marshal(v *uint64) (b []byte, err error) {
b = binary.BigEndian.AppendUint64(b, *v)
return
}
type JsonTypeCodec[T any] struct{}
func (j JsonTypeCodec[T]) Unmarshal(data []byte, v *T) error {
return json.Unmarshal(data, v)
}
func (j JsonTypeCodec[T]) Marshal(v *T) ([]byte, error) {
return json.Marshal(v)
}