transfer

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2022 License: Apache-2.0 Imports: 7 Imported by: 13

Documentation

Index

Examples

Constants

View Source
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

View Source
var (
	ErrUnknownEncodingType = errors.New("transfer: unknown encoding type")
	ErrNotEncodable        = errors.New("transfer: the given value cannot be encoded for this transfer mechanism")
)

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

func DecodeFromTransfer(value string) ([]byte, error)

DecodeFromTransfer uses ParseEncodedValue to find the right encoder then decodes value with it.

func EncodeForTransfer

func EncodeForTransfer(value []byte) (string, error)

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 Decoder

type Decoder interface {
	DecodeFromTransfer(string) ([]byte, error)
}

Decoder takes a string and decodes it, returning a byte slice or an error

type EncodeDecoder

type EncodeDecoder interface {
	Encoder
	Decoder
}

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.

func (JSON) Decode

func (t JSON) Decode() ([]byte, error)

Decode finds the given encoder for this JSON data and decodes the data using it.

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

func EncodeJSON(value []byte) (JSONOrStr, error)

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 (tos JSONOrStr) MarshalJSON() ([]byte, error)

func (*JSONOrStr) UnmarshalJSON

func (tos *JSONOrStr) UnmarshalJSON(data []byte) error

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.

Jump to

Keyboard shortcuts

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