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 ¶
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 ¶
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.
Types ¶
type Codec ¶
type Codec interface { // Marshal converts an object to a []byte representation. // You can optionally pass additional arguments to further customize this call. Marshal(object interface{}, options map[string]interface{}) ([]byte, error) // Unmarshal converts a []byte representation into an object. Unmarshal(data []byte, obj interface{}) error // ContentType gets the default content type for this codec. ContentType() string // FileExtension returns the file extension by which the codec is represented. FileExtension() string // CanMarshalWithCallback gets whether the codec is capable of marshalling a response with // a callback parameter. CanMarshalWithCallback() bool }
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 }
ContentTypeMatcherCodec is a Codec that has its own logic for determining whether or not it can handle a content type. This is particularly useful for codecs that can handle more than one content type.
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]interface{}) (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.
Directories ¶
Path | Synopsis |
---|---|
A codec for handling BSON encoding and decoding
|
A codec for handling BSON encoding and decoding |
The csv package contains a codec for talking CSV (comma separated values).
|
The csv package contains a codec for talking CSV (comma separated values). |
A codec for handling JSON encoding and decoding.
|
A codec for handling JSON encoding and decoding. |
A codec for handling JSONP encoding.
|
A codec for handling JSONP encoding. |
A codec for handling msgpack encoding and decoding.
|
A codec for handling msgpack encoding and decoding. |
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. |
A codec for handling simple XML encoding and decoding.
|
A codec for handling simple XML encoding and decoding. |