mediatype

package module
v0.0.0-...-0a7f972 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2014 License: MIT Imports: 6 Imported by: 0

README

mediatype

GoDoc Build Status

An [Internet] Media Type and MIME string parser and modeler for Go

This library expands on the builtin "mime" package to provide a model for an "Internet Media Type" normally specified by Content-Type HTTP and MIME header fields, and as specified in RFC 2045.

Install

go get github.com/Rican7/mediatype

Example

contentTypeString := "application/vnd.google-earth.kml+xml; charset=utf-8"

mediaType, _ := mediatype.Parse(contentTypeString)

mediaType.MainType()   // "application"
mediaType.SubType()    // "kml"
mediaType.Trees()      // ["vnd", "google-earth"]
mediaType.Prefix()     // "vnd"
mediaType.Suffix()     // "xml"
mediaType.Parameters() // ["charset": "utf-8"]

mediaType.FullType()   // "application/vnd.google-earth.kml+xml"
mediaType.String()     // "application/vnd.google-earth.kml+xml; charset=utf-8"

Mutability and Immutability

mutable := &mediatype.Mutable{
    Main: "application",
    Sub:  "json",
}

mutable.String()     // "application/json"

mutable.Sub    = "xhtml"
mutable.Suffix = "xml"
mutable.String()     // "application/xhtml+xml"


immutable := mutable.Immutable()
mutable.Main = "image"
immutable.String()     // "application/xhtml+xml"

Documentation

Overview

Package mediatype provides an [Internet] Media Type model and MIME type string parser/formatter

Index

Constants

View Source
const (
	// MainSubSplitCharacter - The character used to split the main and sub-types from a full type string
	MainSubSplitCharacter = "/"

	// SuffixCharacter - The character used to denote a suffix declaration
	SuffixCharacter = "+"

	// TreeSeparatorCharacter - The character used to separate trees
	TreeSeparatorCharacter = "."
)
View Source
const ContentTypeHeader = "Content-Type"

ContentTypeHeader - The standard key of a MIMEHeader's content type

Variables

This section is empty.

Functions

This section is empty.

Types

type Immutable

type Immutable struct {
	// contains filtered or unexported fields
}

Immutable is an immutable encapsulation of a Media Type

func (*Immutable) FullType

func (m *Immutable) FullType() string

FullType returns the normalized full type as a string

func (*Immutable) MainType

func (m *Immutable) MainType() string

MainType returns the "main" (top-level) type as a string

func (*Immutable) Mutable

func (m *Immutable) Mutable() Mutable

Mutable returns a mutable version of this structure

func (*Immutable) Parameters

func (m *Immutable) Parameters() map[string]string

Parameters returns the defined parameters of the media type

func (*Immutable) Prefix

func (m *Immutable) Prefix() string

Prefix returns the prefix of the type's trees

func (*Immutable) String

func (m *Immutable) String() string

Get a string representation conforming to RFC 2045 and RFC 2616

func (*Immutable) SubType

func (m *Immutable) SubType() string

SubType returns the "sub" type as a string

func (*Immutable) Suffix

func (m *Immutable) Suffix() string

Suffix returns the "suffix" of the type as a string

func (*Immutable) Trees

func (m *Immutable) Trees() []string

Trees returns the split "sub" type as an array of strings split by the namespace separator

type MediaType

type MediaType interface {
	fmt.Stringer

	MainType() string
	SubType() string
	Trees() []string
	Prefix() string
	Suffix() string
	Parameters() map[string]string

	FullType() string
}

MediaType is the interface for an [Internet] Media Type model

Each method is a getter for each piece of a Media Type's structure

The `String()` method implements the fmt.Stringer interface to easily print out the reconstructed parts of the MediaType model in a standards compliant manner, following the `mime.FormatMediaType()` output

func DetectFromBytes

func DetectFromBytes(data []byte) (MediaType, error)

DetectFromBytes - Build a MediaType from the detected content type of a byte array

func DetectFromFileHeader

func DetectFromFileHeader(fileHeader multipart.FileHeader) (MediaType, error)

DetectFromFileHeader - Build a MediaType from a FileHeader

func DetectFromHeader

func DetectFromHeader(header textproto.MIMEHeader) (MediaType, error)

DetectFromHeader - Build a MediaType from a MIMEHeader map

func NewImmutable

func NewImmutable() MediaType

NewImmutable returns a new empty instance of a Immutable struct

func NewImmutableAsContainer

func NewImmutableAsContainer(mutable Mutable) MediaType

NewImmutableAsContainer returns a new instance of a Immutable struct with the values matching the values of the passed Mutable struct

func NewMutable

func NewMutable() MediaType

NewMutable returns a new empty instance of a Mutable struct

func Parse

func Parse(raw string) (MediaType, error)

Parse a raw media type string into a MediaType interface compatible struct

type Mutable

type Mutable struct {
	Main   string
	Tree   []string
	Sub    string
	Suf    string
	Params map[string]string
}

Mutable is a struct defining the components of a Media Type

func (*Mutable) FullType

func (m *Mutable) FullType() string

FullType returns the normalized full type as a string

func (Mutable) Immutable

func (m Mutable) Immutable() *Immutable

Immutable returns an immutable version of this structure

func (*Mutable) MainType

func (m *Mutable) MainType() string

MainType returns the "main" (top-level) type as a string

func (*Mutable) Parameters

func (m *Mutable) Parameters() map[string]string

Parameters returns the defined parameters of the media type

func (*Mutable) Prefix

func (m *Mutable) Prefix() string

Prefix returns the prefix of the type's trees

func (*Mutable) String

func (m *Mutable) String() string

Get a string representation conforming to RFC 2045 and RFC 2616

func (*Mutable) SubType

func (m *Mutable) SubType() string

SubType returns the "sub" type as a string

func (*Mutable) Suffix

func (m *Mutable) Suffix() string

Suffix returns the "suffix" of the type as a string

func (*Mutable) Trees

func (m *Mutable) Trees() []string

Trees returns the split "sub" type as an array of strings split by the namespace separator

Jump to

Keyboard shortcuts

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