-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobs.go
36 lines (29 loc) · 1.17 KB
/
gobs.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
// Package gobs implements the Encoder and Decoders for use with the gop format.
package gobs
import (
"context"
"encoding/gob"
"io"
)
var (
// DefaultGOBSEncoder provides a package-level json encoder for use.
DefaultGOBSEncoder GOBSEncoder
// DefaultGOBSTargetDecoder provides a package-level json target decoder for use.
DefaultGOBSTargetDecoder GOBSUnmarshalDecoder
)
// GOBSEncoder implements a wrapper over the encoding/json GOBSEncoder to
// match the Encoder interface. This allow us use GOBS has a encoder for
// any method encoding as needed.
type GOBSEncoder struct{}
// Encode implements the necessary logic to use json for encoding.
func (GOBSEncoder) Encode(ctx context.Context, w io.Writer, payload interface{}) error {
return gob.NewEncoder(w).Encode(payload)
}
// GOBSUnmarshalDecoder implements a wrapper over the encoding/json GOBSEncoder to
// all incoming data into a interface{} type.
type GOBSUnmarshalDecoder struct{}
// Encode implements the necessary logic to use json for encoding to provided
// target of type interface{}.
func (GOBSUnmarshalDecoder) Decode(ctx context.Context, r io.Reader, data interface{}) error {
return gob.NewDecoder(r).Decode(data)
}