| 1 | package framing |
| 2 | |
| 3 | type ( |
| 4 | // UnmarshalFunc translates bytes to objects |
| 5 | UnmarshalFunc func([]byte, interface{}) error |
| 6 | |
| 7 | // Decoder reads and decodes Protobuf messages from an io.Reader. |
| 8 | Decoder interface { |
| 9 | // Decode reads the next encoded message from its input and stores it |
| 10 | // in the value pointed to by m. If m isn't a proto.Message, Decode will panic. |
| 11 | Decode(interface{}) error |
| 12 | } |
| 13 | |
| 14 | // DecoderFunc is the functional adaptation of Decoder |
| 15 | DecoderFunc func(interface{}) error |
| 16 | ) |
| 17 | |
| 18 | func (f DecoderFunc) Decode(m interface{}) error { return f(m) } |
| 19 | |
| 20 | var _ = Decoder(DecoderFunc(nil)) |
| 21 | |
| 22 | // NewDecoder returns a new Decoder that reads from the given frame Reader. |
| 23 | func NewDecoder(r Reader, uf UnmarshalFunc) DecoderFunc { |
| 24 | return func(m interface{}) error { |
| 25 | // Note: the buf returned by ReadFrame will change over time, it can't be sub-sliced |
| 26 | // and then those sub-slices retained. Examination of generated proto code seems to indicate |
| 27 | // that byte buffers are copied vs. referenced by sub-slice (gogo protoc). |
| 28 | frame, err := r.ReadFrame() |
| 29 | if err != nil { |
| 30 | return err |
| 31 | } |
| 32 | return uf(frame, m) |
| 33 | } |
| 34 | } |