marshal_httpbodyproto.go 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. package runtime
  2. import (
  3. "google.golang.org/genproto/googleapis/api/httpbody"
  4. )
  5. // HTTPBodyMarshaler is a Marshaler which supports marshaling of a
  6. // google.api.HttpBody message as the full response body if it is
  7. // the actual message used as the response. If not, then this will
  8. // simply fallback to the Marshaler specified as its default Marshaler.
  9. type HTTPBodyMarshaler struct {
  10. Marshaler
  11. }
  12. // ContentType returns its specified content type in case v is a
  13. // google.api.HttpBody message, otherwise it will fall back to the default Marshalers
  14. // content type.
  15. func (h *HTTPBodyMarshaler) ContentType(v interface{}) string {
  16. if httpBody, ok := v.(*httpbody.HttpBody); ok {
  17. return httpBody.GetContentType()
  18. }
  19. return h.Marshaler.ContentType(v)
  20. }
  21. // Marshal marshals "v" by returning the body bytes if v is a
  22. // google.api.HttpBody message, otherwise it falls back to the default Marshaler.
  23. func (h *HTTPBodyMarshaler) Marshal(v interface{}) ([]byte, error) {
  24. if httpBody, ok := v.(*httpbody.HttpBody); ok {
  25. return httpBody.GetData(), nil
  26. }
  27. return h.Marshaler.Marshal(v)
  28. }