represent: github.com/teepark/represent Index | Files | Directories

package represent

import "github.com/teepark/represent"

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

Package Files

doc.go http.go match.go registry.go

Variables

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

ErrNoMatch is for when no suitable protocol is found

func DecodeRequest Uses

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 Uses

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 Uses

func Register(p Protocol)

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

func SetDefault Uses

func SetDefault(contentType string)

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

type Protocol Uses

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 Uses

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 Uses

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

Registry is a container for Protocols

func NewRegistry Uses

func NewRegistry() *Registry

NewRegistry creates a new, empty Registry

func (*Registry) DecodeRequest Uses

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 Uses

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 Uses

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 Uses

func (reg *Registry) Register(p Protocol)

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

func (*Registry) SetDefault Uses

func (reg *Registry) SetDefault(contentType string)

SetDefault sets the default content type for a specific registry.

Directories

PathSynopsis
jsonPackage json implements the registry.Protocol interface, and registers it with the default registry.
msgpackPackage msgpack implements the registry.Protocol interface, and registers it with the default registry.
xmlPackage xml implements the registry.Protocol interface, and registers it with the default registry.
yamlPackage yaml implements the registry.Protocol interface, and registers it with the default registry.

Package represent imports 9 packages (graph) and is imported by 4 packages. Updated 2016-07-21. Refresh now. Tools for package owners.