go-sawyer: github.com/lostisland/go-sawyer/mediatype Index | Files

package mediatype

import "github.com/lostisland/go-sawyer/mediatype"

Package mediatype contains helpers for parsing media type strings. Uses RFC4288 as a guide.


Package Files

decode.go encode.go mediatype.go

func AddDecoder Uses

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 Uses

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)

type Decoder Uses

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

A Decoder will decode the given value to the Decoder's io.Reader.

type DecoderFunc Uses

type DecoderFunc func(r io.Reader) Decoder

DecoderFunc is a function that creates a Decoder from an io.Reader.

type Encoder Uses

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

An Encoder will encode the given value to the Encoder's io.Writer.

type EncoderFunc Uses

type EncoderFunc func(w io.Writer) Encoder

EncoderFunc is a function that creates an Encoder from an io.Writer.

type MediaType Uses

type MediaType struct {
    Full     string
    Type     string
    MainType string
    SubType  string
    Suffix   string
    Vendor   string
    Version  string
    Format   string
    Params   map[string]string

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 Parse Uses

func Parse(v string) (*MediaType, error)

Parse builds a *MediaType from a given media type string.

func (*MediaType) Decode Uses

func (m *MediaType) Decode(v interface{}, body io.Reader) error

Encode uses this MediaType's Decoder to decode the io.Reader into the given value.

func (*MediaType) Decoder Uses

func (m *MediaType) Decoder(body io.Reader) (Decoder, error)

Decoder finds a decoder based on this MediaType's Format field. An error is returned if a decoder cannot be found.

func (*MediaType) Encode Uses

func (m *MediaType) Encode(v interface{}) (*bytes.Buffer, error)

Encode uses this MediaType's Encoder to encode the given value into a bytes.Buffer.

func (*MediaType) Encoder Uses

func (m *MediaType) Encoder(w io.Writer) (Encoder, error)

Encoder finds an encoder based on this MediaType's Format field. An error is returned if an encoder cannot be found.

func (*MediaType) IsVendor Uses

func (m *MediaType) IsVendor() bool

IsVendor determines if this MediaType is associated with commercially available products.

func (*MediaType) String Uses

func (m *MediaType) String() string

String returns the full string representation of the MediaType.

Package mediatype imports 6 packages (graph) and is imported by 5 packages. Updated 2016-08-02. Refresh now. Tools for package owners.