Repository navigation
Expand file tree
/
Copy pathserializers.go
More file actions
75 lines (63 loc) · 1.97 KB
/
Copy pathserializers.go
File metadata and controls
75 lines (63 loc) · 1.97 KB
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
package securecookie
import (
"bytes"
"encoding/gob"
"encoding/json"
)
// Serializer provides an interface for providing custom serializers for cookie
// values.
type Serializer interface {
Serialize(src any) ([]byte, error)
Deserialize(src []byte, dst any) error
}
// GobEncoder encodes cookie values using encoding/gob. This is the simplest
// encoder and can handle complex types via gob.Register.
type GobEncoder struct{}
// JSONEncoder encodes cookie values using encoding/json. Users who wish to
// encode complex types need to satisfy the json.Marshaller and
// json.Unmarshaller interfaces.
type JSONEncoder struct{}
// NopEncoder does not encode cookie values, and instead simply accepts a []byte
// (as an any) and returns a []byte. This is particularly useful when
// you encoding an object upstream and do not wish to re-encode it.
type NopEncoder struct{}
// Serialize encodes a value using gob.
func (e GobEncoder) Serialize(src any) ([]byte, error) {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
if err := enc.Encode(src); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// Deserialize decodes a value using gob.
func (e GobEncoder) Deserialize(src []byte, dst any) error {
dec := gob.NewDecoder(bytes.NewBuffer(src))
if err := dec.Decode(dst); err != nil {
return err
}
return nil
}
// Serialize encodes a value using encoding/json.
func (e JSONEncoder) Serialize(src any) ([]byte, error) {
return json.Marshal(src)
}
// Deserialize decodes a value using encoding/json.
func (e JSONEncoder) Deserialize(src []byte, dst any) error {
return json.Unmarshal(src, dst)
}
// Serialize passes a []byte through as-is.
func (e NopEncoder) Serialize(src any) ([]byte, error) {
if b, ok := src.([]byte); ok {
return b, nil
}
return nil, ErrValueNotByte
}
// Deserialize passes a []byte through as-is.
func (e NopEncoder) Deserialize(src []byte, dst any) error {
if dat, ok := dst.(*[]byte); ok {
*dat = src
return nil
}
return ErrValueNotBytePtr
}