Documentation ¶
Overview ¶
Package mediatype contains helpers for parsing media type strings. Uses RFC4288 as a guide.
Index ¶
- func AddDecoder(format string, decfunc DecoderFunc)
- func AddEncoder(format string, encfunc EncoderFunc)
- type Decoder
- type DecoderFunc
- type Encoder
- type EncoderFunc
- type MediaType
- func (m *MediaType) Decode(v interface{}, body io.Reader) error
- func (m *MediaType) Decoder(body io.Reader) (Decoder, error)
- func (m *MediaType) Encode(v interface{}) (*bytes.Buffer, error)
- func (m *MediaType) Encoder(w io.Writer) (Encoder, error)
- func (m *MediaType) IsVendor() bool
- func (m *MediaType) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddDecoder ¶
func AddDecoder(format string, decfunc DecoderFunc)
AddDecoder installs a decoder for a given format.
AddDecoder("json", func(r io.Reader) Encoder { return json.NewDecoder(r) }) mt, err := Parse("application/json") decoder, err := mt.Decoder(someReader)
func AddEncoder ¶
func AddEncoder(format string, encfunc EncoderFunc)
AddEncoder installs an encoder for a given format.
AddEncoder("json", func(w io.Writer) Encoder { return json.NewEncoder(w) }) mt, err := Parse("application/json") encoder, err := mt.Encoder(someWriter)
Types ¶
type Decoder ¶
type Decoder interface {
Decode(v interface{}) error
}
A Decoder will decode the given value to the Decoder's io.Reader.
type DecoderFunc ¶
DecoderFunc is a function that creates a Decoder from an io.Reader.
type Encoder ¶
type Encoder interface {
Encode(v interface{}) error
}
An Encoder will encode the given value to the Encoder's io.Writer.
type EncoderFunc ¶
EncoderFunc is a function that creates an Encoder from an io.Writer.
type MediaType ¶
type MediaType struct { Type string MainType string SubType string Suffix string Vendor string Version string Format string Params map[string]string // contains filtered or unexported fields }
A MediaType is a parsed representation of a media type string.
application/vnd.github.raw+json; version=3; charset=utf-8
This gets broken up into the various fields:
- Type: application/vnd.github.raw+json
- MainType: application
- SubType: vnd.github.raw
- Suffix: json
- Vendor: github
- Version: raw
- Format: json
- Params: version: 3 charset: utf-8
There are a few special behaviors that prioritize custom media types for APIs:
If an API identifies with an "application/vnd" type, the Vendor and Version fields are parsed from the remainder. The Version's semantic meaning depends on the application.
If it's not an "application/vnd" type, the Version field is taken from the "version" parameter.
The Format is taken from the Suffix by default. If not available, it is guessed by looking for common strings anywhere in the media type. For instance, "application/json" will identify as the "json" Format.
The Format is used to get an Encoder and a Decoder.
func (*MediaType) Decode ¶
Encode uses this MediaType's Decoder to decode the io.Reader into the given value.
func (*MediaType) Decoder ¶
Decoder finds a decoder based on this MediaType's Format field. An error is returned if a decoder cannot be found.
func (*MediaType) Encode ¶
Encode uses this MediaType's Encoder to encode the given value into a bytes.Buffer.
func (*MediaType) Encoder ¶
Encoder finds an encoder based on this MediaType's Format field. An error is returned if an encoder cannot be found.