encoding

package
v0.11.0-beta4 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package encoding provides safe, atomic utility methods for encoding/saving to disk and loading from disk/decoding of data in common formats.

Index

Constants

View Source
const (
	// Base62Alphabet is the alphabet used for Base62 encoding.
	Base62Alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)

Variables

This section is empty.

Functions

func DecodeBase62 added in v0.10.2

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

DecodeBase62 performs Base62 decoding.

func DecodeBase64 added in v0.11.0

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

DecodeBase64 is short-hand for base64.RawURLEncoding.DecodeString.

func DecodeProtobuf added in v0.11.0

func DecodeProtobuf(reader io.Reader, message proto.Message) error

DecodeProtobuf reads and decodes a single Protocol Buffers message as transmitted by ProtobufEncoder or WriteProtobuf. It is useful in cases where a ProtobufDecoder cannot be used because the underlying stream will send data after the Protocol Buffers message that could be gobbled up by the decoder's buffered reads, but it is not an efficient implementation, and thus should only be used for a fixed number of messages (e.g. a single initialization message).

func EncodeBase62 added in v0.10.2

func EncodeBase62(value []byte) string

EncodeBase62 performs Base62 encoding.

func EncodeBase64 added in v0.11.0

func EncodeBase64(value []byte) string

EncodeBase64 is short-hand for base64.RawURLEncoding.EncodeToString

func EncodeProtobuf added in v0.11.0

func EncodeProtobuf(writer io.Writer, message proto.Message) error

EncodeProtobuf writes a single Protocol Buffers messages that can be read by ProtobufDecoder or ReadProtobuf. It is a useful shorthand for creating a ProtobufEncoder and writing a single message. For multiple message sends, it is far more efficient to use a ProtobufEncoder directly.

func LoadAndUnmarshal added in v0.10.0

func LoadAndUnmarshal(path string, unmarshal func([]byte) error) error

LoadAndUnmarshal provides the underlying loading and unmarshaling functionality for the encoding package. It reads the data at the specified path and then invokes the specified unmarshaling callback (usually a closure) to decode the data.

func LoadAndUnmarshalProtobuf

func LoadAndUnmarshalProtobuf(path string, message proto.Message) error

LoadAndUnmarshalProtobuf loads data from the specified path and decodes it into the specified Protocol Buffers message.

func LoadAndUnmarshalTOML

func LoadAndUnmarshalTOML(path string, value interface{}) error

LoadAndUnmarshalTOML loads data from the specified path and decodes it into the specified structure.

func LoadAndUnmarshalYAML added in v0.10.0

func LoadAndUnmarshalYAML(path string, value interface{}) error

LoadAndUnmarshalYAML loads data from the specified path and decodes it into the specified structure.

func MarshalAndSave added in v0.10.0

func MarshalAndSave(path string, marshal func() ([]byte, error)) error

MarshalAndSave provide the underlying marshaling and saving functionality for the encoding package. It invokes the specified marshaling callback (usually a closure) and writes the result atomically to the specified path. The data is saved with read/write permissions for the user only.

func MarshalAndSaveProtobuf

func MarshalAndSaveProtobuf(path string, message proto.Message) error

MarshalAndSaveProtobuf marshals the specified Protocol Buffers message and saves it to the specified path.

Types

type ProtobufDecoder added in v0.10.0

type ProtobufDecoder struct {
	// contains filtered or unexported fields
}

ProtobufDecoder is a stream decoder for Protocol Buffers messages. For performance reasons, this type wraps the underlying stream in a buffering reader, which means that the decoder should persist for the lifetime of the stream (since there's no way to know how much data has been read from the stream). For single-message decoding that won't over-read a stream, use DecodeProtobuf.

func NewProtobufDecoder added in v0.10.0

func NewProtobufDecoder(reader io.Reader) *ProtobufDecoder

NewProtobufDecoder creates a new Protocol Buffers stream decoder.

func (*ProtobufDecoder) Decode added in v0.10.0

func (d *ProtobufDecoder) Decode(message proto.Message) error

Decode decodes a length-prefixed Protocol Buffers message from the underlying stream.

type ProtobufEncoder added in v0.10.0

type ProtobufEncoder struct {
	// contains filtered or unexported fields
}

ProtobufEncoder is a stream encoder for Protocol Buffers messages.

func NewProtobufEncoder added in v0.10.0

func NewProtobufEncoder(writer io.Writer) *ProtobufEncoder

NewProtobufEncoder creates a new Protocol Buffers stream encoder.

func (*ProtobufEncoder) Encode added in v0.10.0

func (e *ProtobufEncoder) Encode(message proto.Message) error

Encode encodes a length-prefixed Protocol Buffers message into the encoder's internal buffer and writes this data to the underlying stream.

func (*ProtobufEncoder) EncodeWithoutFlush added in v0.10.0

func (e *ProtobufEncoder) EncodeWithoutFlush(message proto.Message) error

EncodeWithoutFlush encodes a length-prefixed Protocol Buffers message into the encoder's internal buffer, but does not write this data to the underlying stream.

func (*ProtobufEncoder) Flush added in v0.10.0

func (e *ProtobufEncoder) Flush() error

Flush writes the contents of the encoder's internal buffer, if any, to the underlying stream.

Jump to

Keyboard shortcuts

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