Documentation ¶
Index ¶
- Constants
- Variables
- func DecodeFromTransfer(value string) ([]byte, error)
- func EncodeForTransfer(value []byte) (string, error)
- type Base64Encoding
- type Decoder
- type EncodeDecoder
- type EncodeDecoderFactories
- type EncodeDecoderFactoryFunc
- type Encoder
- type EncodingType
- type JSON
- type JSONInterface
- type JSONOrStr
- type NoEncoding
Examples ¶
Constants ¶
const DefaultEncodingType = Base64EncodingType
DefaultEncodingType is the default encodingType. This makes it easier to use this package as the caller won't need to make any desisions around what encoder to use unless they really need to.
Variables ¶
var ( ErrUnknownEncodingType = errors.New("transfer: unknown encoding type") ErrNotEncodable = errors.New("transfer: the given value cannot be encoded for this transfer mechanism") )
var Encoders = EncodeDecoderFactories{ Base64EncodingType: func() EncodeDecoder { return Base64Encoding{} }, NoEncodingType: func() EncodeDecoder { return NoEncoding{} }, }
Encoders maps encoding algorithms to their respective EncodeDecoder types. Example:
ed := transfer.Encoders[Base64EncodingType]() encodedValue, err := ed.EncodeForTransfer("my super secret value")
Functions ¶
func DecodeFromTransfer ¶
DecodeFromTransfer uses ParseEncodedValue to find the right encoder then decodes value with it.
func EncodeForTransfer ¶
EncodeForTransfer uses a UTF-8 transfer-safe encoding to encode value.
Types ¶
type Base64Encoding ¶
type Base64Encoding struct{}
Base64Encoding handles the encoding and decoding of values using base64. All encoded values will be prefixed with "base64:"
func (Base64Encoding) DecodeFromTransfer ¶
func (e Base64Encoding) DecodeFromTransfer(value string) ([]byte, error)
DecodeFromTransfer takes a string and attempts to decode using a base64 decoder. If an error is returned, it will originate from the Go encoding/base64 package.
func (Base64Encoding) EncodeForTransfer ¶
func (e Base64Encoding) EncodeForTransfer(value []byte) (string, error)
EncodeForTransfer takes a byte slice and returns it encoded as a base64 string. No error is ever returned.
func (Base64Encoding) EncodeJSON ¶
func (Base64Encoding) EncodeJSON(value []byte) (JSONOrStr, error)
EncodeJSON encodes the given value as JSON.
type EncodeDecoder ¶
EncodeDecoder groups Encoder and Decoder to form a type that can both encode and decode values.
type EncodeDecoderFactories ¶
type EncodeDecoderFactories map[EncodingType]EncodeDecoderFactoryFunc
EncodeDecoderFactories defines the type that can be used to produce encoder/decoders.
type EncodeDecoderFactoryFunc ¶
type EncodeDecoderFactoryFunc func() EncodeDecoder
EncodeDecoderFactoryFunc is a function that produces an encoder/decoder.
type Encoder ¶
type Encoder interface { EncodeJSON([]byte) (JSONOrStr, error) EncodeForTransfer([]byte) (string, error) }
Encoder encodes a byte slice and returns a string with the encoding type prefixed
type EncodingType ¶
type EncodingType string
const ( Base64EncodingType EncodingType = "base64" NoEncodingType EncodingType = "" )
func ParseEncodedValue ¶
func ParseEncodedValue(value string) (EncodingType, string)
ParseEncodedValue will attempt to split on : and extract an encoding identifer from the prefix of the string. It then returns the discovered encodingType and the value without the encodingType prefixed.
func (EncodingType) String ¶
func (p EncodingType) String() string
type JSON ¶
type JSON struct { EncodingType EncodingType `json:"$encoding"` Data string `json:"data"` // Factories allows this struct to be configured to use a different set of // encoder/decoders than the default. Factories EncodeDecoderFactories `json:"-"` }
JSON is a convenient way to represent an encoding and data tuple in JSON.
type JSONInterface ¶
type JSONInterface struct {
Data interface{}
}
JSONInterface allows arbitrary embedding of encoded data within any JSON type. The accepted interface values for the data correspond to those listed in the Go documentation for encoding/json.Unmarshal.
func (JSONInterface) MarshalJSON ¶
func (ji JSONInterface) MarshalJSON() ([]byte, error)
func (*JSONInterface) UnmarshalJSON ¶
func (ji *JSONInterface) UnmarshalJSON(data []byte) error
type JSONOrStr ¶
type JSONOrStr struct{ JSON }
JSONOrStr is like the JSON type, but also allows NoEncodingType to be represented as a raw JSON string.
func EncodeJSON ¶
EncodeJSON returns a JSON transfer-safe encoding of value.
Example ¶
j, _ := EncodeJSON([]byte("Hello, \x90\xA2\x8A\x45")) b, _ := json.Marshal(j) fmt.Println(string(b))
Output: {"$encoding":"base64","data":"SGVsbG8sIJCiikU="}
Example (Plain) ¶
j, _ := EncodeJSON([]byte("super secret token")) b, _ := json.Marshal(j) fmt.Println(string(b))
Output: "super secret token"
func (JSONOrStr) MarshalJSON ¶
func (*JSONOrStr) UnmarshalJSON ¶
type NoEncoding ¶
type NoEncoding struct{}
NoEncoding just returns the values without encoding them. This is used when there is no encoding type algorithm prefix on the value.
func (NoEncoding) DecodeFromTransfer ¶
func (e NoEncoding) DecodeFromTransfer(value string) ([]byte, error)
DecodeFromTransfer takes a string and casts it to a byte slice. No error is ever returned.
func (NoEncoding) EncodeForTransfer ¶
func (e NoEncoding) EncodeForTransfer(value []byte) (string, error)
EncodeForTransfer takes a byte slice and casts it to a string. No error is ever returned.
func (NoEncoding) EncodeJSON ¶
func (NoEncoding) EncodeJSON(value []byte) (JSONOrStr, error)
EncodeJSON encodes the given value as JSON, possibly as a JSON string.