blob: 15f1a510d3b53857847adb0462d990032efb9d3f [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001package flatbuffers
2
3// Codec implements gRPC-go Codec which is used to encode and decode messages.
4var Codec = "flatbuffers"
5
Austin Schuh272c6132020-11-14 16:37:52 -08006// FlatbuffersCodec defines the interface gRPC uses to encode and decode messages. Note
7// that implementations of this interface must be thread safe; a Codec's
8// methods can be called from concurrent goroutines.
Austin Schuhe89fa2d2019-08-14 20:24:23 -07009type FlatbuffersCodec struct{}
10
Austin Schuh272c6132020-11-14 16:37:52 -080011// Marshal returns the wire format of v.
Austin Schuhe89fa2d2019-08-14 20:24:23 -070012func (FlatbuffersCodec) Marshal(v interface{}) ([]byte, error) {
13 return v.(*Builder).FinishedBytes(), nil
14}
15
Austin Schuh272c6132020-11-14 16:37:52 -080016// Unmarshal parses the wire format into v.
Austin Schuhe89fa2d2019-08-14 20:24:23 -070017func (FlatbuffersCodec) Unmarshal(data []byte, v interface{}) error {
18 v.(flatbuffersInit).Init(data, GetUOffsetT(data))
19 return nil
20}
21
Austin Schuh272c6132020-11-14 16:37:52 -080022// String old gRPC Codec interface func
Austin Schuhe89fa2d2019-08-14 20:24:23 -070023func (FlatbuffersCodec) String() string {
24 return Codec
25}
26
Austin Schuh272c6132020-11-14 16:37:52 -080027// Name returns the name of the Codec implementation. The returned string
28// will be used as part of content type in transmission. The result must be
29// static; the result cannot change between calls.
30//
31// add Name() for ForceCodec interface
32func (FlatbuffersCodec) Name() string {
33 return Codec
34}
35
Austin Schuhe89fa2d2019-08-14 20:24:23 -070036type flatbuffersInit interface {
37 Init(data []byte, i UOffsetT)
38}