grpc.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package flatbuffers
  2. // Codec implements gRPC-go Codec which is used to encode and decode messages.
  3. var Codec = "flatbuffers"
  4. // FlatbuffersCodec defines the interface gRPC uses to encode and decode messages. Note
  5. // that implementations of this interface must be thread safe; a Codec's
  6. // methods can be called from concurrent goroutines.
  7. type FlatbuffersCodec struct{}
  8. // Marshal returns the wire format of v.
  9. func (FlatbuffersCodec) Marshal(v interface{}) ([]byte, error) {
  10. return v.(*Builder).FinishedBytes(), nil
  11. }
  12. // Unmarshal parses the wire format into v.
  13. func (FlatbuffersCodec) Unmarshal(data []byte, v interface{}) error {
  14. v.(flatbuffersInit).Init(data, GetUOffsetT(data))
  15. return nil
  16. }
  17. // String old gRPC Codec interface func
  18. func (FlatbuffersCodec) String() string {
  19. return Codec
  20. }
  21. // Name returns the name of the Codec implementation. The returned string
  22. // will be used as part of content type in transmission. The result must be
  23. // static; the result cannot change between calls.
  24. //
  25. // add Name() for ForceCodec interface
  26. func (FlatbuffersCodec) Name() string {
  27. return Codec
  28. }
  29. type flatbuffersInit interface {
  30. Init(data []byte, i UOffsetT)
  31. }