codecs

package
v0.0.0-...-86643de Latest Latest
Warning

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

Go to latest
Published: May 10, 2019 License: BSD-2-Clause Imports: 4 Imported by: 4

README

#codecs

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

Documentation

You can get stuck into the API documentation by checking out these API docs:

How to use the codecs package

// make a codec service
codecService := new(codecs.WebCodecService)

// get the content type (probably from the request)
var contentType string = "application/json"

// get the codec
codec, err := codecService.GetCodec(contentType)

if err != nil {
	// handle errors - specifially ErrorContentTypeNotSupported
}

/*
	[]bytes to object
*/

// get the raw data
dataBytes := []byte(`{"somedata": true}`)

// use the codec to unmarshal the dataBytes
var obj interface{}
unmarshalErr := codecService.UnmarshalWithCodec(codec, dataBytes, obj) error

if unmarshalErr != nil {
	// handle this error
}

// obj will now be an object built from the dataBytes

// get some details about the kind of response we're going to make
// (probably from the request headers again)
var accept string = "application/json"
var extension string = ".json"
var hasCallback bool = false

// get the codec to respond with (it could be different)
responseCodec, responseCodecErr := codecService.GetCodecForResponding(accept, extension, hasCallback)

if responseCodecErr != nil {
    // handle this error
}

/*
	object to []bytes
*/

// get the data object
dataObject := map[string]interface{}{"name": "Mat"}

// use the codec service to marshal to bytes
bytes, marshalErr := codecService.MarshalWithCodec(responseCodecErr, dataObject, nil)

if marshalErr != nil {
	// handle marshalErr
}

// bytes would now be a representation of the data object

Contributing

Please feel free to submit issues, fork the repository and send pull requests!

When submitting an issue, we ask that you please include steps to reproduce the issue so we can see it on our end also!

Licence

Copyright (c) 2012 - 2013 Mat Ryer and Tyler Bunnell

Please consider promoting this project if you find it useful.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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]interface{}) (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]interface{}) (objx.Map, error)

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

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.
xml
A codec for handling simple XML encoding and decoding.
A codec for handling simple XML encoding and decoding.

Jump to

Keyboard shortcuts

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