4 "google.golang.org/genproto/googleapis/api/httpbody"
7 // SetHTTPBodyMarshaler overwrite the default marshaler with the HTTPBodyMarshaler
8 func SetHTTPBodyMarshaler(serveMux *ServeMux) {
9 serveMux.marshalers.mimeMap[MIMEWildcard] = &HTTPBodyMarshaler{
10 Marshaler: &JSONPb{OrigName: true},
14 // HTTPBodyMarshaler is a Marshaler which supports marshaling of a
15 // google.api.HttpBody message as the full response body if it is
16 // the actual message used as the response. If not, then this will
17 // simply fallback to the Marshaler specified as its default Marshaler.
18 type HTTPBodyMarshaler struct {
22 // ContentType implementation to keep backwards compatability with marshal interface
23 func (h *HTTPBodyMarshaler) ContentType() string {
24 return h.ContentTypeFromMessage(nil)
27 // ContentTypeFromMessage in case v is a google.api.HttpBody message it returns
28 // its specified content type otherwise fall back to the default Marshaler.
29 func (h *HTTPBodyMarshaler) ContentTypeFromMessage(v interface{}) string {
30 if httpBody, ok := v.(*httpbody.HttpBody); ok {
31 return httpBody.GetContentType()
33 return h.Marshaler.ContentType()
36 // Marshal marshals "v" by returning the body bytes if v is a
37 // google.api.HttpBody message, otherwise it falls back to the default Marshaler.
38 func (h *HTTPBodyMarshaler) Marshal(v interface{}) ([]byte, error) {
39 if httpBody, ok := v.(*httpbody.HttpBody); ok {
40 return httpBody.Data, nil
42 return h.Marshaler.Marshal(v)