Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1 | package flatbuffers |
| 2 | |
| 3 | // Codec implements gRPC-go Codec which is used to encode and decode messages. |
| 4 | var Codec = "flatbuffers" |
| 5 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 6 | // 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 9 | type FlatbuffersCodec struct{} |
| 10 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 11 | // Marshal returns the wire format of v. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 12 | func (FlatbuffersCodec) Marshal(v interface{}) ([]byte, error) { |
| 13 | return v.(*Builder).FinishedBytes(), nil |
| 14 | } |
| 15 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 16 | // Unmarshal parses the wire format into v. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 17 | func (FlatbuffersCodec) Unmarshal(data []byte, v interface{}) error { |
| 18 | v.(flatbuffersInit).Init(data, GetUOffsetT(data)) |
| 19 | return nil |
| 20 | } |
| 21 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 22 | // String old gRPC Codec interface func |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 23 | func (FlatbuffersCodec) String() string { |
| 24 | return Codec |
| 25 | } |
| 26 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 27 | // 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 |
| 32 | func (FlatbuffersCodec) Name() string { |
| 33 | return Codec |
| 34 | } |
| 35 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 36 | type flatbuffersInit interface { |
| 37 | Init(data []byte, i UOffsetT) |
| 38 | } |