Documentation ¶
Overview ¶
Provides services for working with codecs.
The CodecService interface defines various functions that make it much easier to obtain a codec object that is appropriate for the data you wish to handle.
To write a new codec service, simply confrom to the CodecService interface, and install it by doing:
// get a service codecService := NewWebCodecService() // make your own codec myCodec := new(MyCodec) // install the codec codecService.AddCodec(myCodec)
Index ¶
- Variables
- type AcceptEntry
- type AcceptTree
- type CodecService
- type ContentType
- type ContentTypeNotSupportedError
- type WebCodecService
- func (s *WebCodecService) AddCodec(codec codecs.Codec)
- func (s *WebCodecService) Codecs() []codecs.Codec
- func (s *WebCodecService) GetCodec(contentType string) (codecs.Codec, error)
- func (s *WebCodecService) GetCodecForResponding(accept, extension string, hasCallback bool) (codecs.Codec, error)
- func (s *WebCodecService) MarshalWithCodec(codec codecs.Codec, object interface{}, options map[string]interface{}) ([]byte, error)
- func (s *WebCodecService) RemoveCodec(contentType string)
- func (s *WebCodecService) UnmarshalWithCodec(codec codecs.Codec, data []byte, object interface{}) error
Constants ¶
This section is empty.
Variables ¶
var DefaultCodecs = []codecs.Codec{new(json.JsonCodec), new(jsonp.JsonPCodec), new(msgpack.MsgpackCodec), new(bson.BsonCodec), new(csv.CsvCodec), new(xml.SimpleXmlCodec)}
DefaultCodecs represents the list of Codecs that get added automatically by a call to NewWebCodecService.
Functions ¶
This section is empty.
Types ¶
type AcceptEntry ¶
type AcceptEntry struct { ContentType *ContentType // Quality is the parsed q value from a ContentType's parameters. Quality float32 // contains filtered or unexported fields }
AcceptEntry represents a single entry within an Accept header. It includes both the ContentType and a Quality, parsed from the ContentType's parameters.
func NewAcceptEntry ¶
func NewAcceptEntry() *AcceptEntry
NewAcceptEntry returns a new *AcceptEntry with default values.
func OrderAcceptHeader ¶
func OrderAcceptHeader(accept string) ([]*AcceptEntry, error)
OrderAcceptHeader reads an Accept header and pulls out the various MIME types in the order of preference, returning the types in that order.
The HTTP spec for the Accept header states that multiple MIME types can be specified in the Accept header, and that preferred MIME types are chosen based on the following criteria:
1. The q variable for a MIME type in the Accept header defines a 'quality', and higher qualities should be chosen over lower qualities. The default quality is 1.0 for any type that doesn't state the quality explicitly. 2. More specific MIME types should be chosen over less specific MIME types, excepting the presence of a q parameter that counters this guideline. For example, assuming equal qualities, application/xml should trump application/*. 3. Barring the previous two guidelines, MIME types should be chosen based on the order that they appear in the Accept header.
For more information, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
func ParseAcceptEntry ¶
func ParseAcceptEntry(accept string) (*AcceptEntry, error)
ParseAcceptEntry parses a single entry within an Accept header into a *AcceptEntry value.
func (*AcceptEntry) CompareTo ¶
func (entry *AcceptEntry) CompareTo(otherEntry *AcceptEntry) int
CompareTo compares two *AcceptEntries and returns an integer representing which of the two entries is preferred. Negative return values mean that the passed in entry is preferred, positive values mean that the target entry is preferred, and zero values mean that there is no preference.
type AcceptTree ¶
type AcceptTree struct { Value *AcceptEntry Size int Left *AcceptTree Right *AcceptTree }
AcceptTree is a binary tree that handles Accept header entries. The left-most node will always be the most preferred entry and preference will decrease from left to right.
func (*AcceptTree) Add ¶
func (tree *AcceptTree) Add(next *AcceptEntry)
Add adds an *AcceptEntry to the AcceptTree, putting it in proper order of preference.
func (*AcceptTree) Flatten ¶
func (tree *AcceptTree) Flatten() []*AcceptEntry
Flatten returns the AcceptTree's values in proper order of preference as a []*AcceptEntry value.
type CodecService ¶
type CodecService interface { // GetCodecForResponding gets the codec to use to respond based on the // given accept string, the extension provided and whether it has a callback // or not. GetCodecForResponding(accept, extension string, hasCallback bool) (codecs.Codec, error) // GetCodec gets the codec to use to interpret the request based on the // content type. GetCodec(contentType string) (codecs.Codec, error) // MarshalWithCodec marshals the specified object with the specified codec and options. // If the object implements the Facade interface, the PublicData object should be // marshalled instead. MarshalWithCodec(codec codecs.Codec, object interface{}, options map[string]interface{}) ([]byte, error) // UnmarshalWithCodec unmarshals the specified data into the object with the specified codec. UnmarshalWithCodec(codec codecs.Codec, data []byte, object interface{}) error // Codecs gets all currently installed codecs. Codecs() []codecs.Codec // AddCodec adds the specified codec to the installed codecs list. AddCodec(codecs.Codec) // RemoveCodec removes a codec from the list of codecs by content type RemoveCodec(contentType string) }
CodecService is the interface for a service responsible for providing Codecs.
type ContentType ¶
ContentType represents a single content type, complete with parameters, such as that passed in an HTTP Accept or Content-Type header.
func ParseContentType ¶
func ParseContentType(rawType string) (*ContentType, error)
ParseContentType takes a content-type string and parses it into a mimetype and parameters, returning the ContentType representing the string.
func (*ContentType) AddParam ¶
func (contentType *ContentType) AddParam(param string)
type ContentTypeNotSupportedError ¶
type ContentTypeNotSupportedError struct {
ContentType string
}
func (*ContentTypeNotSupportedError) Error ¶
func (e *ContentTypeNotSupportedError) Error() string
type WebCodecService ¶
type WebCodecService struct {
// contains filtered or unexported fields
}
WebCodecService represents the default implementation for providing access to the currently installed web codecs.
func NewWebCodecService ¶
func NewWebCodecService() *WebCodecService
NewWebCodecService makes a new WebCodecService with the default codecs added.
func (*WebCodecService) AddCodec ¶
func (s *WebCodecService) AddCodec(codec codecs.Codec)
AddCodec adds the specified codec to the installed codecs list.
func (*WebCodecService) Codecs ¶
func (s *WebCodecService) Codecs() []codecs.Codec
Codecs gets all currently installed codecs.
func (*WebCodecService) GetCodec ¶
func (s *WebCodecService) GetCodec(contentType string) (codecs.Codec, error)
GetCodec gets the codec to use to interpret the request based on the content type.
func (*WebCodecService) GetCodecForResponding ¶
func (s *WebCodecService) GetCodecForResponding(accept, extension string, hasCallback bool) (codecs.Codec, error)
GetCodecForResponding gets the codec to use to respond based on the given accept string, the extension provided and whether it has a callback or not.
As of now, if hasCallback is true, the JSONP codec will be returned. This may be changed if additional callback capable codecs are added.
func (*WebCodecService) MarshalWithCodec ¶
func (s *WebCodecService) MarshalWithCodec(codec codecs.Codec, object interface{}, options map[string]interface{}) ([]byte, error)
MarshalWithCodec marshals the specified object with the specified codec and options. If the object implements the Facade interface, the PublicData object should be marshalled instead.
func (*WebCodecService) RemoveCodec ¶
func (s *WebCodecService) RemoveCodec(contentType string)
RemoveCodec removes a codec from the list of codecs by content type
func (*WebCodecService) UnmarshalWithCodec ¶
func (s *WebCodecService) UnmarshalWithCodec(codec codecs.Codec, data []byte, object interface{}) error
UnmarshalWithCodec unmarshals the specified data into the object with the specified codec.