-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James DeFelice
committed
Sep 11, 2017
1 parent
c5d4a73
commit 93a0891
Showing
9 changed files
with
110 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package codecs | ||
|
||
import ( | ||
"github.com/mesos/mesos-go/api/v1/lib/encoding" | ||
"github.com/mesos/mesos-go/api/v1/lib/encoding/json" | ||
"github.com/mesos/mesos-go/api/v1/lib/encoding/proto" | ||
) | ||
|
||
const ( | ||
// MediaTypeProtobuf is the Protobuf serialization format media type. | ||
MediaTypeProtobuf = encoding.MediaType("application/x-protobuf") | ||
// MediaTypeJSON is the JSON serialiation format media type. | ||
MediaTypeJSON = encoding.MediaType("application/json") | ||
|
||
NameProtobuf = "protobuf" | ||
NameJSON = "json" | ||
) | ||
|
||
// ByMediaType are pre-configured default Codecs, ready to use OOTB | ||
var ByMediaType = map[encoding.MediaType]encoding.Codec{ | ||
MediaTypeProtobuf: encoding.Codec{ | ||
Name: NameProtobuf, | ||
Type: MediaTypeProtobuf, | ||
NewEncoder: proto.NewEncoder, | ||
NewDecoder: proto.NewDecoder, | ||
}, | ||
MediaTypeJSON: encoding.Codec{ | ||
Name: NameJSON, | ||
Type: MediaTypeJSON, | ||
NewEncoder: json.NewEncoder, | ||
NewDecoder: json.NewDecoder, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package json | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"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 JSON 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 := json.Marshal(m) | ||
if err != nil { | ||
return err | ||
} | ||
return w.WriteFrame(b) | ||
}) | ||
} | ||
|
||
// NewDecoder returns a new Decoder of JSON messages read from the given source. | ||
func NewDecoder(s encoding.Source) encoding.Decoder { | ||
r := s() | ||
dec := framing.NewDecoder(r, json.Unmarshal) | ||
return encoding.DecoderFunc(func(u encoding.Unmarshaler) error { return dec.Decode(u) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,30 @@ | ||
package proto | ||
|
||
import ( | ||
"io" | ||
|
||
"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 that writes to the given io.Writer. | ||
func NewEncoder(w io.Writer) *Encoder { | ||
return &Encoder{w: w} | ||
// 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) | ||
}) | ||
} | ||
|
||
// An Encoder encodes and writes Protobuf messages to an io.Writer. | ||
type Encoder struct{ w io.Writer } | ||
|
||
// Encode writes the given Protobuf-encoded message m to its io.Writer. If m | ||
// isn't a proto.Message, Encode will panic. | ||
func (e *Encoder) Encode(m interface{}) error { | ||
bs, err := proto.Marshal(m.(proto.Message)) | ||
if err == nil { | ||
_, err = e.w.Write(bs) | ||
} | ||
return err | ||
// 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) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters