enkodo

package module
v0.0.0-...-748d8f8 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: MIT Imports: 7 Imported by: 0

README

Enkodo (Marshal, unmarshal) GoDoc Status Go Report Card

Enkodo is a no-frills encoder/decoder. It is focused around speed and simplicity.

Note: Enkodo is still in development and the API could potentially change. Usage is still safe as SEMVER will be respected

Benchmarks

% go test --bench=.
goos: darwin
goarch: amd64
pkg: github.com/mojura/enkodo

# Enkodo
EnkodoEncoding-4                          8989959    126 ns/op      0 B/op    0 allocs/op
EnkodoDecoding-4                          3464836    335 ns/op     16 B/op    1 allocs/op
EnkodoDecoding_no_string-4                3806563    308 ns/op      0 B/op    0 allocs/op
EnkodoEncoding_new_encoder-4              2792838    438 ns/op    296 B/op    6 allocs/op
EnkodoDecoding_new_decoder-4              3024243    393 ns/op     32 B/op    2 allocs/op
EnkodoDecoding_new_decoder_no_string-4    3174868    370 ns/op     16 B/op    1 allocs/op

# JSON
JSONEncoding-4                          843171    1355 ns/op       0 B/op     0 allocs/op
JSONDecoding-4                          201477    5679 ns/op     192 B/op    12 allocs/op
JSONDecoding_no_string-4                207198    5612 ns/op     176 B/op    11 allocs/op
JSONEncoding_new_encoder-4              869494    1379 ns/op       0 B/op     0 allocs/op
JSONDecoding_new_decoder-4              175564    6123 ns/op    1088 B/op    17 allocs/op
JSONDecoding_new_decoder_no_string-4    207910    6001 ns/op    1072 B/op    16 allocs/op

# Gob
# Note: Gob cannot compete in the decoder re-use benchmarks due to it's 
# requirement of initializing a new decoder for every test iteration
GOBEncoding-4                        1395951      827 ns/op       0 B/op      0 allocs/op
GOBEncoding_new_encoder-4             158697     7077 ns/op    1584 B/op     42 allocs/op
GOBDecoding_new_decoder-4              33736    33379 ns/op    8784 B/op    244 allocs/op
GOBDecoding_new_decoder_no_string-4    35460    33105 ns/op    8768 B/op    243 allocs/op

# Specific function benchmarks
EncodeInt64-4       132754987    8.85 ns/op    0 B/op    0 allocs/op
Encoder_Uint64-4    100000000    10.6 ns/op    0 B/op    0 allocs/op
EncodeUint64-4      209906836    5.67 ns/op    0 B/op    0 allocs/op

Usage

package main

import (
	"bytes"
	"log"

	"github.com/mojura/enkodo"
)

func main() {
	var (
		// Original user struct
		u User
		// New user struct (will be used to copy values to)
		nu  User
		err error
	)

	u.Email = "[email protected]"
	u.Age = 46
	u.Twitter = "@johndoe"

	buffer := bytes.NewBuffer(nil)
	// Create a writer
	w := enkodo.NewWriter(buffer)
	// Encode user to buffer
	if err = w.Encode(&u); err != nil {
		log.Fatalf("Error encoding: %v", err)
	}

	// Decode new user from buffer
	if err = enkodo.Unmarshal(buffer.Bytes(), &nu); err != nil {
		log.Fatalf("Error decoding: %v", err)
	}

	log.Printf("New user: %v", nu)
}

// User holds the basic information for a user
type User struct {
	Email   string
	Age     uint8
	Twitter string
}

// MarshalEnkodo will marshal a User
func (u *User) MarshalEnkodo(enc *enkodo.Encoder) (err error) {
	enc.String(u.Email)
	enc.Uint8(u.Age)
	enc.String(u.Twitter)
	return
}

// UnmarshalEnkodo will unmarshal a User
func (u *User) UnmarshalEnkodo(dec *enkodo.Decoder) (err error) {
	if u.Email, err = dec.String(); err != nil {
		return
	}

	if u.Age, err = dec.Uint8(); err != nil {
		return
	}

	if u.Twitter, err = dec.String(); err != nil {
		return
	}

	return
}

Features

  • Support for basic primitives
  • Encoding via helper funcs
  • Decoding via helper funcs
  • Encoding helper funcs created by reflection (Next release)
  • Decoding helper funcs created by reflection (Next release)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyBytes are returned when inbound bytes are empty during decode
	ErrEmptyBytes = errors.New("cannot decode, inbound bytes are empty")
	// ErrInvalidLength is returned when a byteslice has an invalid length for it's desired primitive
	ErrInvalidLength = errors.New("invalid length")
	// ErrIsClosed is returned when an action is attempted on a closed instance
	ErrIsClosed = errors.New("cannot perform action on closed instance")
)

Functions

func Marshal

func Marshal(v Encodee) (bs []byte, err error)

Marshal will encode a value

func MarshalAppend

func MarshalAppend(v Encodee, buffer []byte) (bs []byte, err error)

MarshalAppend will encode a value to a provided slice

func Unmarshal

func Unmarshal(bs []byte, v Decodee) (err error)

Unmarshal will decode a value

Types

type Decodee

type Decodee interface {
	UnmarshalEnkodo(*Decoder) error
}

Decodee is a data structure to be dedoded

type Decoder

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

Decoder helps to Marshal data

func (*Decoder) Bool

func (d *Decoder) Bool() (v bool, err error)

Bool will return a decoded boolean value

func (*Decoder) Bytes

func (d *Decoder) Bytes(in *[]byte) (err error)

Bytes will append bytes to the inbound byteslice

func (*Decoder) Decode

func (d *Decoder) Decode(v Decodee) (err error)

Decode will decode a decodee

func (*Decoder) Float32

func (d *Decoder) Float32() (v float32, err error)

Float32 decodes a float64 type

func (*Decoder) Float64

func (d *Decoder) Float64() (v float64, err error)

Float64 decodes a float64 type

func (*Decoder) Int

func (d *Decoder) Int() (v int, err error)

Int decodes an int type

func (*Decoder) Int16

func (d *Decoder) Int16() (v int16, err error)

Int16 decodes an int16 type

func (*Decoder) Int32

func (d *Decoder) Int32() (v int32, err error)

Int32 decodes an int32 type

func (*Decoder) Int64

func (d *Decoder) Int64() (v int64, err error)

Int64 decodes an int64 type

func (*Decoder) Int8

func (d *Decoder) Int8() (v int8, err error)

Int8 decodes an int8 type

func (*Decoder) String

func (d *Decoder) String() (str string, err error)

String will return a decoded string

func (*Decoder) Uint

func (d *Decoder) Uint() (v uint, err error)

Uint decodes a uint type

func (*Decoder) Uint16

func (d *Decoder) Uint16() (v uint16, err error)

Uint16 decodes a uint16 type

func (*Decoder) Uint32

func (d *Decoder) Uint32() (v uint32, err error)

Uint32 decodes a uint32 type

func (*Decoder) Uint64

func (d *Decoder) Uint64() (v uint64, err error)

Uint64 decodes a uint64 type

func (*Decoder) Uint8

func (d *Decoder) Uint8() (v uint8, err error)

Uint8 decodes a uint8 type

type Encodee

type Encodee interface {
	MarshalEnkodo(*Encoder) error
}

Encodee is a data structure to be encoded

type Encoder

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

Encoder helps to Marshal data

func (*Encoder) Bool

func (e *Encoder) Bool(v bool) (err error)

Bool will encode a boolean value to the writer

func (*Encoder) Bytes

func (e *Encoder) Bytes(v []byte) (err error)

Bytes will encode a byteslice to the writer

func (*Encoder) Encode

func (e *Encoder) Encode(v Encodee) (err error)

Encode will encode an encodee

func (*Encoder) Float32

func (e *Encoder) Float32(v float32) (err error)

Float32 encodes an float32 type

func (*Encoder) Float64

func (e *Encoder) Float64(v float64) (err error)

Float64 encodes an float64 type

func (*Encoder) Int

func (e *Encoder) Int(v int) (err error)

Int encodes an int type

func (*Encoder) Int16

func (e *Encoder) Int16(v int16) (err error)

Int16 encodes an int16 type

func (*Encoder) Int32

func (e *Encoder) Int32(v int32) (err error)

Int32 encodes an int32 type

func (*Encoder) Int64

func (e *Encoder) Int64(v int64) (err error)

Int64 encodes an int64 type

func (*Encoder) Int8

func (e *Encoder) Int8(v int8) (err error)

Int8 encodes an int8 type

func (*Encoder) String

func (e *Encoder) String(v string) (err error)

String will encode a string to the writer

func (*Encoder) Uint

func (e *Encoder) Uint(v uint) (err error)

Uint encodes a uint type

func (*Encoder) Uint16

func (e *Encoder) Uint16(v uint16) (err error)

Uint16 encodes a uint16 type

func (*Encoder) Uint32

func (e *Encoder) Uint32(v uint32) (err error)

Uint32 encodes a uint32 type

func (*Encoder) Uint64

func (e *Encoder) Uint64(v uint64) (err error)

Uint64 encodes a uint64 type

func (*Encoder) Uint8

func (e *Encoder) Uint8(v uint8) (err error)

Uint8 encodes a uint8 type

type Reader

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

Reader manages the writing of enkodo output

func NewReader

func NewReader(in io.Reader) *Reader

NewReader will initialize a new instance of writer

func (*Reader) Close

func (r *Reader) Close() (err error)

Close will close the reader

func (*Reader) Decode

func (r *Reader) Decode(v Decodee) (err error)

Decode will decode an decodee

type Writer

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

Writer manages the writing of enkodo output

func NewWriter

func NewWriter(in io.Writer) *Writer

NewWriter will initialize a new instance of writer

func (*Writer) Bytes

func (w *Writer) Bytes() []byte

Bytes will expose the underlying bytes

func (*Writer) Close

func (w *Writer) Close() (err error)

Close will close the writer

func (*Writer) Encode

func (w *Writer) Encode(v Encodee) (err error)

Encode will encode an encodee

func (*Writer) Reset

func (w *Writer) Reset()

Reset will reset the underlying bytes of the Encoder

func (*Writer) WriteTo

func (w *Writer) WriteTo(dest io.Writer) (n int64, err error)

WriteTo will write to an io.Writer

func (*Writer) Written

func (w *Writer) Written() int64

Written will return the total number of bytes written

Directories

Path Synopsis
cmd
example

Jump to

Keyboard shortcuts

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