marshaler.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package runtime
  2. import (
  3. "io"
  4. )
  5. // Marshaler defines a conversion between byte sequence and gRPC payloads / fields.
  6. type Marshaler interface {
  7. // Marshal marshals "v" into byte sequence.
  8. Marshal(v interface{}) ([]byte, error)
  9. // Unmarshal unmarshals "data" into "v".
  10. // "v" must be a pointer value.
  11. Unmarshal(data []byte, v interface{}) error
  12. // NewDecoder returns a Decoder which reads byte sequence from "r".
  13. NewDecoder(r io.Reader) Decoder
  14. // NewEncoder returns an Encoder which writes bytes sequence into "w".
  15. NewEncoder(w io.Writer) Encoder
  16. // ContentType returns the Content-Type which this marshaler is responsible for.
  17. // The parameter describes the type which is being marshalled, which can sometimes
  18. // affect the content type returned.
  19. ContentType(v interface{}) string
  20. }
  21. // Decoder decodes a byte sequence
  22. type Decoder interface {
  23. Decode(v interface{}) error
  24. }
  25. // Encoder encodes gRPC payloads / fields into byte sequence.
  26. type Encoder interface {
  27. Encode(v interface{}) error
  28. }
  29. // DecoderFunc adapts an decoder function into Decoder.
  30. type DecoderFunc func(v interface{}) error
  31. // Decode delegates invocations to the underlying function itself.
  32. func (f DecoderFunc) Decode(v interface{}) error { return f(v) }
  33. // EncoderFunc adapts an encoder function into Encoder
  34. type EncoderFunc func(v interface{}) error
  35. // Encode delegates invocations to the underlying function itself.
  36. func (f EncoderFunc) Encode(v interface{}) error { return f(v) }
  37. // Delimited defines the streaming delimiter.
  38. type Delimited interface {
  39. // Delimiter returns the record separator for the stream.
  40. Delimiter() []byte
  41. }