encs

package module
v0.0.0-...-be37369 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2020 License: MIT Imports: 6 Imported by: 0

README

Encs

A type-safe and modular encoding library
GoDoc pipeline status coverage report

Encs aims to provide a type-strict, modular and feature-full encoding library with as little overhead as possible.

Goals include: Type-safe: The type is encoded along with the value, and decoders will decode only into the same type that was sent, or in the case of interface encoding, fill the interface with the same type as was sent. All types to be received must be Registered with Register()

Stream-promiscuious: Encoded messages are completely self-contained, and encoded streams can be picked up by a Decoder mid-stream and decoded sucessfully, allowing a static Encoder to write to a dynamic number of receiving clients, and a dynamic number of sending clients to be decoded by a single Decoder.

Modular and Open: Methods for encoding are exposed in sub-packages, allowing their low-level encoding methods to be used to create custom encoding systems for a given use case, without the overhead or added complexity of an Encoder or Decoder. The simple payload structure also allows easy re-implementation of the encs protocol.

encs/encodable provides encoders for specific types, and methods for encoding reflect.Type values.

encs/encio provides io and error types for encoding and related tasks

Example:

buff := new(bytes.Buffer)

// This would typically go in an init() function.
encs.Register(ExampleStruct{})

birthday, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")

example := ExampleStruct{
	Name: "John Doe",
	Likes: []string{
		"Computers",
		"Music",
	},
	Birthday: birthday,
}

enc := encs.NewEncoder(buff, nil)

err := enc.Encode(&example)
if err != nil {
	fmt.Println(err)
	return
}

dec := encs.NewDecoder(buff, nil)

var decodedExample ExampleStruct
err = dec.Decode(&decodedExample)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("Name: %v, Likes: %v, Birthday: %v", decodedExample.Name, decodedExample.Likes, decodedExample.Birthday)

// Output:
// Name: John Doe, Likes: [Computers Music], Birthday: 2006-01-02 15:04:05 +0000 UTC

Documentation

Overview

Package encs aims to provide a type-strict, modular and feature-full encoding library with as little overhead as possible.

Goals include: Type-safe: The type is encoded along with the value, and decoders will decode only into the same type that was sent, or in the case of interface encoding, fill the interface with the same type as was sent. All types to be received must be Registered with Register()

Stream-promiscuious: Encoded messages are completely self-contained, and encoded streams can be picked up by a Decoder mid-stream and decoded sucessfully, allowing a static Encoder to write to a dynamic number of receiving clients, and a dynamic number of sending clients to be decoded by a single Decoder.

Modular and Open: Methods for encoding are exposed in sub-packages, allowing their low-level encoding methods to be used to create custom encoding systems for a given use case, without the overhead or added complexity of an Encoder or Decoder. The simple payload structure also allows easy re-implementation of the encs protocol.

encs/encodable provides encoders for specific types, and methods for encoding reflect.Type values.

encs/encio provides io and error types for encoding and related tasks

Index

Constants

This section is empty.

Variables

View Source
var DefaultResolver = encodable.NewRegisterResolver(nil)

DefaultResolver is the default Resolver used when Config.Resolver is nil. Types must be registered with Register()

Functions

func Register

func Register(t interface{}) error

Register registers the type of t. Types must be registered before encoding. It is a shortcut for DefaultResolver.Register()

Types

type Config

type Config struct {
	// Resolver is the encoder for reflect.Types.
	// If nil, the default resolver will be used, and Encoded types must be registered with encs.Register()
	Resolver encodable.Resolver
}

Config defines configuration for Encoders and Decoders

type Decoder

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

func NewDecoder

func NewDecoder(r io.Reader, config *Config) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

type Encoder

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

func NewEncoder

func NewEncoder(w io.Writer, config *Config) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) error

Directories

Path Synopsis
Package encio provides simple methods for encoding-relavent input and output, as well as error types.
Package encio provides simple methods for encoding-relavent input and output, as well as error types.
Package encodable provides low-level methods for seralising golang data structures.
Package encodable provides low-level methods for seralising golang data structures.

Jump to

Keyboard shortcuts

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