File: encoding.go

package info (click to toggle)
golang-github-mesos-mesos-go 0.0.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 11,724 kB
  • sloc: makefile: 163
file content (30 lines) | stat: -rw-r--r-- 885 bytes parent folder | download
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
package proto

import (
	"github.com/gogo/protobuf/proto"
	"github.com/mesos/mesos-go/api/v1/lib/encoding"
	"github.com/mesos/mesos-go/api/v1/lib/encoding/framing"
)

// NewEncoder returns a new Encoder of Calls to Protobuf messages written to
// the given io.Writer.
func NewEncoder(s encoding.Sink) encoding.Encoder {
	w := s()
	return encoding.EncoderFunc(func(m encoding.Marshaler) error {
		b, err := proto.Marshal(m.(proto.Message))
		if err != nil {
			return err
		}
		return w.WriteFrame(b)
	})
}

// NewDecoder returns a new Decoder of Protobuf messages read from the given Source.
func NewDecoder(s encoding.Source) encoding.Decoder {
	r := s()
	var (
		uf  = func(b []byte, m interface{}) error { return proto.Unmarshal(b, m.(proto.Message)) }
		dec = framing.NewDecoder(r, uf)
	)
	return encoding.DecoderFunc(func(u encoding.Unmarshaler) error { return dec.Decode(u) })
}