represent

package module
v0.0.0-...-b072a59 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2015 License: BSD-3-Clause Imports: 9 Imported by: 0

README

GoDoc TravisCI

Documentation

Overview

Package represent provides a plug-in mechanism for serialization formats and matches against the Accept and Content-Type headers of http.Requests to make it easy to support a variety of representations of HTTP resources.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoMatch = errors.New("no protocol matched the request")

ErrNoMatch is for when no suitable protocol is found

Functions

func DecodeRequest

func DecodeRequest(container interface{}, r *http.Request) (int, error)

DecodeRequest uses an appropriate protocol to decode an http request body into a container. It returns a suggested HTTP response code and an error (if the error is nil the response code will be StatusOK, 200).

func EncodeResponse

func EncodeResponse(data interface{}, r *http.Request, w http.ResponseWriter) (int, error)

EncodeResponse will serialize data with a format acceptable by the HTTP client (using the Accept header of the request). If there is an error it will NOT send the response but will instead return a suggested HTTP response code and the error. If it returns (200, nil) then the data has already been written to the response.

func Register

func Register(p Protocol)

Register sets a Protocol implementation as the handler for its content type.

func SetDefault

func SetDefault(contentType string)

SetDefault sets the content type to prefer in the event of match ties (especially because the Accept header contained */*).

Types

type Protocol

type Protocol interface {
	// ContentType returns the content-type this Protocol handles
	ContentType() string

	// Decode reads content from a Reader and deserializes it into a container
	Decode(interface{}, io.Reader) error

	// Encode serializes an object to a Writer
	Encode(interface{}, io.Writer) error
}

Protocol is the plug-in interface for a specific serialization format implementation. These can be created and registered with the global registry to make them eligible for selection based on request Accept headers.

func Match

func Match(header string) (Protocol, error)

Match parses a mimetype header (like Accept) and selects the most suitable registered Protocol. It will return errors resulting from a malformed header or a nil Protocol if nothing matches.

type Registry

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

Registry is a container for Protocols

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new, empty Registry

func (*Registry) DecodeRequest

func (reg *Registry) DecodeRequest(container interface{}, r *http.Request) (int, error)

DecodeRequest as a Registry method performs the same job as the global function, but using the protocols registered with a specific registry.

func (*Registry) EncodeResponse

func (reg *Registry) EncodeResponse(data interface{}, r *http.Request, w http.ResponseWriter) (int, error)

EncodeResponse performs the same job as the global function, but matches against the set of Protocols registered with the specific registry.

func (*Registry) Match

func (reg *Registry) Match(header string) (Protocol, error)

Match on a registry performs the same operation as the Match function, just matches against the set of Protocols registered on the specific registry.

func (*Registry) Register

func (reg *Registry) Register(p Protocol)

Register stores a Protocol as a content-type handler on a registry instance.

func (*Registry) SetDefault

func (reg *Registry) SetDefault(contentType string)

SetDefault sets the default content type for a specific registry.

Directories

Path Synopsis
Package json implements the registry.Protocol interface, and registers it with the default registry.
Package json implements the registry.Protocol interface, and registers it with the default registry.
Package msgpack implements the registry.Protocol interface, and registers it with the default registry.
Package msgpack implements the registry.Protocol interface, and registers it with the default registry.
Package xml implements the registry.Protocol interface, and registers it with the default registry.
Package xml implements the registry.Protocol interface, and registers it with the default registry.
Package yaml implements the registry.Protocol interface, and registers it with the default registry.
Package yaml implements the registry.Protocol interface, and registers it with the default registry.

Jump to

Keyboard shortcuts

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