mediatype

package
v0.0.0-...-0b59bd9 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2014 License: MIT Imports: 6 Imported by: 25

Documentation

Overview

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

Index

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

type DecoderFunc func(r io.Reader) Decoder

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

type EncoderFunc func(w io.Writer) Encoder

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

type MediaType

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

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

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

func (*MediaType) Decode

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

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

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

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

func (m *MediaType) IsVendor() bool

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

func (*MediaType) String

func (m *MediaType) String() string

String returns the full string representation of the MediaType.

Jump to

Keyboard shortcuts

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