protomarshal

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const MIMEWildcard = "*"

MIMEWildcard is the fallback MIME type used for requests which do not match a registered MIME type.

Variables

View Source
var (
	AcceptHeader      = http.CanonicalHeaderKey("Accept")
	ContentTypeHeader = http.CanonicalHeaderKey("Content-Type")

	DefaultMarshaler = &HTTPBodyMarshaler{
		Marshaler: &JSONPb{
			MarshalOptions: protojson.MarshalOptions{
				EmitUnpopulated: true,
			},
			UnmarshalOptions: protojson.UnmarshalOptions{
				DiscardUnknown: true,
			},
		},
	}
)

Functions

This section is empty.

Types

type Decoder

type Decoder interface {
	Decode(v interface{}) error
}

Decoder decodes a byte sequence

type DecoderFunc

type DecoderFunc func(v interface{}) error

DecoderFunc adapts an decoder function into Decoder.

func (DecoderFunc) Decode

func (f DecoderFunc) Decode(v interface{}) error

Decode delegates invocations to the underlying function itself.

type DecoderWrapper

type DecoderWrapper struct {
	*json.Decoder
	protojson.UnmarshalOptions
}

DecoderWrapper is a wrapper around a *json.Decoder that adds support for protos to the Decode method.

func (DecoderWrapper) Decode

func (d DecoderWrapper) Decode(v interface{}) error

Decode wraps the embedded decoder's Decode method to support protos using a jsonpb.Unmarshaler.

type Delimited

type Delimited interface {
	// Delimiter returns the record separator for the stream.
	Delimiter() []byte
}

Delimited defines the streaming delimiter.

type Encoder

type Encoder interface {
	Encode(v interface{}) error
}

Encoder encodes gRPC payloads / fields into byte sequence.

type EncoderFunc

type EncoderFunc func(v interface{}) error

EncoderFunc adapts an encoder function into Encoder

func (EncoderFunc) Encode

func (f EncoderFunc) Encode(v interface{}) error

Encode delegates invocations to the underlying function itself.

type HTTPBodyMarshaler

type HTTPBodyMarshaler struct {
	Marshaler
}

HTTPBodyMarshaler is a Marshaler which supports marshaling of a google.api.HttpBody message as the full response body if it is the actual message used as the response. If not, then this will simply fallback to the Marshaler specified as its default Marshaler.

func (*HTTPBodyMarshaler) ContentType

func (h *HTTPBodyMarshaler) ContentType(v interface{}) string

ContentType returns its specified content type in case v is a google.api.HttpBody message, otherwise it will fall back to the default Marshalers content type.

func (*HTTPBodyMarshaler) Marshal

func (h *HTTPBodyMarshaler) Marshal(v interface{}) ([]byte, error)

Marshal marshals "v" by returning the body bytes if v is a google.api.HttpBody message, otherwise it falls back to the default Marshaler.

type JSONBuiltin

type JSONBuiltin struct{}

JSONBuiltin is a Marshaler which marshals/unmarshals into/from JSON with the standard "encoding/json" package of Golang. Although it is generally faster for simple proto messages than JSONPb, it does not support advanced features of protobuf, e.g. map, oneof, ....

The NewEncoder and NewDecoder types return *json.Encoder and *json.Decoder respectively.

func (*JSONBuiltin) ContentType

func (*JSONBuiltin) ContentType(_ interface{}) string

ContentType always Returns "application/json".

func (*JSONBuiltin) Delimiter

func (j *JSONBuiltin) Delimiter() []byte

Delimiter for newline encoded JSON streams.

func (*JSONBuiltin) Marshal

func (j *JSONBuiltin) Marshal(v interface{}) ([]byte, error)

Marshal marshals "v" into JSON

func (*JSONBuiltin) NewDecoder

func (j *JSONBuiltin) NewDecoder(r io.Reader) Decoder

NewDecoder returns a Decoder which reads JSON stream from "r".

func (*JSONBuiltin) NewEncoder

func (j *JSONBuiltin) NewEncoder(w io.Writer) Encoder

NewEncoder returns an Encoder which writes JSON stream into "w".

func (*JSONBuiltin) Unmarshal

func (j *JSONBuiltin) Unmarshal(data []byte, v interface{}) error

Unmarshal unmarshals JSON data into "v".

type JSONPb

JSONPb is a Marshaler which marshals/unmarshals into/from JSON with the "google.golang.org/protobuf/encoding/protojson" marshaler. It supports the full functionality of protobuf unlike JSONBuiltin.

The NewDecoder method returns a DecoderWrapper, so the underlying *json.Decoder methods can be used.

func (*JSONPb) ContentType

func (*JSONPb) ContentType(_ interface{}) string

ContentType always returns "application/json".

func (*JSONPb) Delimiter

func (j *JSONPb) Delimiter() []byte

Delimiter for newline encoded JSON streams.

func (*JSONPb) Marshal

func (j *JSONPb) Marshal(v interface{}) ([]byte, error)

Marshal marshals "v" into JSON.

func (*JSONPb) NewDecoder

func (j *JSONPb) NewDecoder(r io.Reader) Decoder

NewDecoder returns a Decoder which reads JSON stream from "r".

func (*JSONPb) NewEncoder

func (j *JSONPb) NewEncoder(w io.Writer) Encoder

NewEncoder returns an Encoder which writes JSON stream into "w".

func (*JSONPb) Unmarshal

func (j *JSONPb) Unmarshal(data []byte, v interface{}) error

Unmarshal unmarshals JSON "data" into "v"

type Marshaler

type Marshaler interface {
	// Marshal marshals "v" into byte sequence.
	Marshal(v interface{}) ([]byte, error)
	// Unmarshal unmarshals "data" into "v".
	// "v" must be a pointer value.
	Unmarshal(data []byte, v interface{}) error
	// NewDecoder returns a Decoder which reads byte sequence from "r".
	NewDecoder(r io.Reader) Decoder
	// NewEncoder returns an Encoder which writes bytes sequence into "w".
	NewEncoder(w io.Writer) Encoder
	// ContentType returns the Content-Type which this marshaler is responsible for.
	// The parameter describes the type which is being marshalled, which can sometimes
	// affect the content type returned.
	ContentType(v interface{}) string
}

Marshaler defines a conversion between byte sequence and gRPC payloads / fields.

type ProtoMarshaller

type ProtoMarshaller struct{}

ProtoMarshaller is a Marshaller which marshals/unmarshals into/from serialize proto bytes

func (*ProtoMarshaller) ContentType

func (*ProtoMarshaller) ContentType(_ interface{}) string

ContentType always returns "application/octet-stream".

func (*ProtoMarshaller) Marshal

func (*ProtoMarshaller) Marshal(value interface{}) ([]byte, error)

Marshal marshals "value" into Proto

func (*ProtoMarshaller) NewDecoder

func (marshaller *ProtoMarshaller) NewDecoder(reader io.Reader) Decoder

NewDecoder returns a Decoder which reads proto stream from "reader".

func (*ProtoMarshaller) NewEncoder

func (marshaller *ProtoMarshaller) NewEncoder(writer io.Writer) Encoder

NewEncoder returns an Encoder which writes proto stream into "writer".

func (*ProtoMarshaller) Unmarshal

func (*ProtoMarshaller) Unmarshal(data []byte, value interface{}) error

Unmarshal unmarshals proto "data" into "value"

type Registry

type Registry struct {
	MIMEMap map[string]Marshaler
}

Registry is a mapping from MIME types to Marshalers.

func NewMarshalerMIMERegistry

func NewMarshalerMIMERegistry() Registry

NewMarshalerMIMERegistry returns a new registry of marshalers. It allows for a mapping of case-sensitive Content-Type MIME type string to runtime.Marshaler interfaces.

For example, you could allow the client to specify the use of the runtime.JSONPb marshaler with a "application/jsonpb" Content-Type and the use of the runtime.JSONBuiltin marshaler with a "application/json" Content-Type. "*" can be used to match any Content-Type. This can be attached to a ServerMux with the marshaler option.

func (Registry) Add

func (r Registry) Add(mime string, marshaler Marshaler) error

Add adds a marshaler for a case-sensitive MIME type string ("*" to match any MIME type).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL