codecs

package
v0.0.0-...-b231bc8 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2015 License: Unlicense Imports: 7 Imported by: 0

Documentation

Overview

Provides interfaces, functions and codecs that can be used to encode and decode data to various formats.

Use services/web_codec_service to easily manage and retrieve appropriate codecs for handling data in a web scenario.

To write a custom codec service, simply create a type that conforms to the CodecService interface.

To write a custom codec, simply create a type that conforms to the Codec interface.

If you wish to customize what is encoded, also conform to the Facade interface.

This interface allows you to provide custom data to be encoded, rather than having your object encoded directly.

Index

Constants

This section is empty.

Variables

View Source
var (
	// PublicDataDidNotFindMap is returned when the PublicData func fails to discover an appropriate
	// public data object, which must end up being a map[string]interface{}.
	PublicDataDidNotFindMap = errors.New("codecs: Object doesn't implement Facade interface and is not a Data object. PublicData(object) failed.")

	// PublicDataTooMuchRecursion is returned when there is too much recursion when
	// calling the Facade interfaces PublicData function.  The PublicData must return either another
	// object that implements Facade, or a map[string]interface{} that will be used for
	// public data.
	PublicDataTooMuchRecursion = errors.New("codecs: Facade object's PublicData() method caused too much recursion.  Does one of your PublicData funcs return itself?")
)

Functions

func PublicData

func PublicData(object interface{}, options map[string]string) (interface{}, error)

PublicData gets the data that is considered public for the specified object. If the object implements the Facade interface, its PublicData method is called until the returning object no longer implements the Facade interface at which point it is considered to have returned the public data.

If the object passed in is an array or slice, PublicData is called on each object to build up an array of public versions of the objects, and an array will be returned.

If the resulting object is not of the appropriate type, the PublicDataDidNotFindMap error will be returned.

If one of the PublicData methods returns itself (or another object already in the path) thus resulting in too much recursion, the PublicDataTooMuchRecursion error is returned.

If any of the objects' PublicData() method returns an error, that is directly returned.

func PublicDataMap

func PublicDataMap(object interface{}, options map[string]string) (objx.Map, error)

PublicDataMap calls PublicData and returns the result after type asserting to objx.Map

Types

type BSON

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

BSON converts objects to and from BSON.

func (*BSON) CanMarshalWithCallback

func (b *BSON) CanMarshalWithCallback() bool

CanMarshalWithCallback returns whether this codec is capable of marshalling a response containing a callback.

func (*BSON) ContentType

func (b *BSON) ContentType() string

ContentType returns the content type for this codec.

func (*BSON) FileExtension

func (b *BSON) FileExtension() string

FileExtension returns the file extension for this codec.

func (*BSON) Marshal

func (b *BSON) Marshal(target interface{}) (output interface{}, err error)

Marshal converts an object to BSON.

func (*BSON) Options

func (b *BSON) Options(o map[string]string) Codec

Options returns a copy of itself with the input options set.

func (*BSON) Unmarshal

func (b *BSON) Unmarshal(input, target interface{}) error

Unmarshal converts BSON into an object.

type Codec

type Codec interface {

	// Marshals the target to output of this codec's type.
	Marshal(target interface{}) (output interface{}, err error)

	// Unmarshals input of this codec's type to target.
	Unmarshal(input, target interface{}) error

	// Takes a map of Content-Type options and returns a copy of this codec with
	// those options applied.
	Options(map[string]string) Codec

	// ContentType returns the content type for this codec.
	ContentType() string

	// CanMarshalWithCallback returns whether this codec is capable of
	// marshalling a response containing a callback.
	CanMarshalWithCallback() bool

	// FileExtention returns the file extension for this codec.
	FileExtension() string
}

Codec is the interface to which a codec must conform.

type ContentTypeMatcherCodec

type ContentTypeMatcherCodec interface {
	Codec

	// ContentTypeSupported returns true if the passed in content type can be
	// handled by this codec, false otherwise
	ContentTypeSupported(contentType string) bool
}

type Facade

type Facade interface {

	// PublicData should return an object containing the
	// data to be marshalled.  If the method returns an error, the codecs
	// will send this error back to the calling code.
	//
	// The method may return either a final map[string]interface{} object,
	// or else another object that implements the Facade interface.
	//
	// The PublicData method should return a new object, and not the original
	// object, as it is possible that the response from PublicData will be modified
	// before being used, and it is bad practice for these methods to alter the
	// original data object.
	PublicData(options map[string]string) (publicData interface{}, err error)
}

Facade is the interface objects should implement if they want to be responsible for providing an alternative data object to the codecs. Without this interface, the codecs will attempt to work on the object itself, whereas if an object implements this interface, the PublicData() method will be called instead, and the resulting object will instead be marshalled.

type JSON

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

JSON converts objects to and from JSON.

func (*JSON) CanMarshalWithCallback

func (j *JSON) CanMarshalWithCallback() bool

CanMarshalWithCallback returns whether this codec is capable of marshalling a response containing a callback.

func (*JSON) ContentType

func (j *JSON) ContentType() string

ContentType returns the content type for this codec.

func (*JSON) FileExtension

func (j *JSON) FileExtension() string

FileExtension returns the file extension for this codec.

func (*JSON) Marshal

func (j *JSON) Marshal(target interface{}) (output interface{}, err error)

Marshal converts an object to JSON.

func (*JSON) Options

func (j *JSON) Options(o map[string]string) Codec

Options returns a copy of itself with the input options set.

func (*JSON) Unmarshal

func (j *JSON) Unmarshal(input, target interface{}) error

Unmarshal converts JSON into an object.

Directories

Path Synopsis
Provides services for working with codecs.
Provides services for working with codecs.
test package provides object useful for testing code that relies on Codecs.
test package provides object useful for testing code that relies on Codecs.

Jump to

Keyboard shortcuts

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