Debian Codegraph @

api/v1/lib/encoding/types.go

raw symbols SCIP
1 package encoding
2
3 import (
4 "encoding/json"
5 "io"
6
7 pb "github.com/gogo/protobuf/proto"
8 "github.com/mesos/mesos-go/api/v1/lib/encoding/framing"
9 )
10
11 type MediaType string
12
13 // ContentType returns the HTTP Content-Type associated with the MediaType
14 func (m MediaType) ContentType() string { return string(m) }
15
16 type (
17 Source func() framing.Reader
18 Sink func() framing.Writer
19
20 // A Codec composes encoding and decoding of a serialization format.
21 Codec struct {
22 Name string
23 Type MediaType
24 NewEncoder func(Sink) Encoder
25 NewDecoder func(Source) Decoder
26 }
27
28 SourceFactory interface {
29 NewSource(r io.Reader) Source
30 }
31 SourceFactoryFunc func(r io.Reader) Source
32
33 SinkFactory interface {
34 NewSink(w io.Writer) Sink
35 }
36 SinkFactoryFunc func(w io.Writer) Sink
37 )
38
39 func (f SourceFactoryFunc) NewSource(r io.Reader) Source { return f(r) }
40
41 func (f SinkFactoryFunc) NewSink(w io.Writer) Sink { return f(w) }
42
43 var (
44 _ = SourceFactory(SourceFactoryFunc(nil))
45 _ = SinkFactory(SinkFactoryFunc(nil))
46 )
47
48 // SourceReader returns a Source that buffers all input from the given io.Reader
49 // and returns the contents in a single frame.
50 func SourceReader(r io.Reader) Source {
51 ch := make(chan framing.ReaderFunc, 1)
52 ch <- framing.ReadAll(r)
53 return func() framing.Reader {
54 select {
55 case f := <-ch:
56 return f
57 default:
58 return framing.ReaderFunc(framing.EOFReaderFunc)
59 }
60 }
61 }
62
63 // SinkWriter returns a Sink that sends a frame to an io.Writer with no decoration.
64 func SinkWriter(w io.Writer) Sink { return func() framing.Writer { return framing.WriterFor(w) } }
65
66 // String implements the fmt.Stringer interface.
67 func (c *Codec) String() string {
68 if c == nil {
69 return ""
70 }
71 return c.Name
72 }
73
74 type (
75 // Marshaler composes the supported marshaling formats.
76 Marshaler interface {
77 pb.Marshaler
78 json.Marshaler
79 }
80 // Unmarshaler composes the supporter unmarshaling formats.
81 Unmarshaler interface {
82 pb.Unmarshaler
83 json.Unmarshaler
84 }
85 // An Encoder encodes a given Marshaler or returns an error in case of failure.
86 Encoder interface {
87 Encode(Marshaler) error
88 }
89
90 // EncoderFunc is the functional adapter for Encoder
91 EncoderFunc func(Marshaler) error
92
93 // A Decoder decodes a given Unmarshaler or returns an error in case of failure.
94 Decoder interface {
95 Decode(Unmarshaler) error
96 }
97
98 // DecoderFunc is the functional adapter for Decoder
99 DecoderFunc func(Unmarshaler) error
100 )
101
102 // Decode implements the Decoder interface
103 func (f DecoderFunc) Decode(u Unmarshaler) error { return f(u) }
104
105 // Encode implements the Encoder interface
106 func (f EncoderFunc) Encode(m Marshaler) error { return f(m) }
107
108 var (
109 _ = Encoder(EncoderFunc(nil))
110 _ = Decoder(DecoderFunc(nil))
111 )