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 ¶
- Variables
- func PublicData(object interface{}, options map[string]string) (interface{}, error)
- func PublicDataMap(object interface{}, options map[string]string) (objx.Map, error)
- type BSON
- func (b *BSON) CanMarshalWithCallback() bool
- func (b *BSON) ContentType() string
- func (b *BSON) FileExtension() string
- func (b *BSON) Marshal(target interface{}) (output interface{}, err error)
- func (b *BSON) Options(o map[string]string) Codec
- func (b *BSON) Unmarshal(input, target interface{}) error
- type Codec
- type ContentTypeMatcherCodec
- type Facade
- type JSON
- func (j *JSON) CanMarshalWithCallback() bool
- func (j *JSON) ContentType() string
- func (j *JSON) FileExtension() string
- func (j *JSON) Marshal(target interface{}) (output interface{}, err error)
- func (j *JSON) Options(o map[string]string) Codec
- func (j *JSON) Unmarshal(input, target interface{}) error
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 BSON ¶
type BSON struct {
// contains filtered or unexported fields
}
BSON converts objects to and from BSON.
func (*BSON) CanMarshalWithCallback ¶
CanMarshalWithCallback returns whether this codec is capable of marshalling a response containing a callback.
func (*BSON) ContentType ¶
ContentType returns the content type for this codec.
func (*BSON) FileExtension ¶
FileExtension returns the file extension for this codec.
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 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 ¶
CanMarshalWithCallback returns whether this codec is capable of marshalling a response containing a callback.
func (*JSON) ContentType ¶
ContentType returns the content type for this codec.
func (*JSON) FileExtension ¶
FileExtension returns the file extension for this codec.