-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.go
38 lines (29 loc) · 1.41 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
package codec
import "io"
// Encoder is responsible for data encoding.
type Encoder interface{ Encode(v interface{}) error }
// EncoderFunc is a wrapper that allows using regular function as Encoder interface.
type EncoderFunc func(interface{}) error
// Encode encodes provided interface to the writer.
func (fn EncoderFunc) Encode(src interface{}) error { return fn(src) }
// Decoder is responsible for data decoding.
type Decoder interface{ Decode(v interface{}) error }
// DecoderFunc is a wrapper that allows using regular function as Decoder interface.
type DecoderFunc func(interface{}) error
// Decode decodes data from the reader to provided receiver.
func (fn DecoderFunc) Decode(recv interface{}) error { return fn(recv) }
// Codec can create Encoder(s) and Decoder(s) with provided io.Reader/io.Writer.
type Codec interface {
// Ecoder instantiates the ecnoder part of the codec with provided writer.
Encoder(w io.Writer) Encoder
// Decoder instantiates the decoder part of the codec with provided reader.
Decoder(r io.Reader) Decoder
// MimeType returns the (main) mime type of the codec.
MimeType() string
}
// Registry represents any kind of codec registry, thus it can be global registry
// or a custom list of codecs that is supposed to be used for a particular case.
type Registry interface {
// Lookup should find appropriate Codec by MimeType or return nil if not found.
Lookup(mimeType string) Codec
}