mum

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2020 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Mum (Marshal, unmarshal) GoDoc Status Go Report Card

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

Note: Mum does not come with training wheels

Benchmarks

# Mum
BenchmarkMumEncoding-4          10000000               141 ns/op               0 B/op          0 allocs/op
BenchmarkMumDecoding-4          10000000               227 ns/op              16 B/op          1 allocs/op

# Binny (github.com/missionMeteora/binny.v2)
BenchmarkBinnyEncoding-4         5000000               343 ns/op              32 B/op          6 allocs/op
BenchmarkBinnyDecoding-4         5000000               390 ns/op              32 B/op          2 allocs/op

# JSON (standard library)
BenchmarkJSONEncoding-4         10000000               164 ns/op               0 B/op          0 allocs/op
BenchmarkJSONDecoding-4          5000000               273 ns/op               0 B/op          0 allocs/op

Usage

package main

import (
	"bytes"
	"log"

	"github.com/itsmontoya/mum"
)

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

	u.Email = "johndoe@gmail.com"
	u.Age = 46
	u.Twitter = "@johndoe"

	// Create a buffer to write to
	buf := bytes.NewBuffer(nil)
	// Create encoder
	enc := mum.NewEncoder(buf)
	// Encode user
	if err = enc.Encode(&u); err != nil {
		log.Fatalf("Error encoding: %v", err)
	}

	// Create decoder
	dec := mum.NewDecoder(buf)
	// Decode new user
	if err = dec.Decode(&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
}

// MarshalMum will marshal a User
func (u *User) MarshalMum(enc *mum.Encoder) (err error) {
	if err = enc.String(u.Email); err != nil {
		return
	}

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

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

	return
}

// UnmarshalMum will unmarshal a User
func (u *User) UnmarshalMum(dec *mum.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
  • Support for maps (currently needs helper func)
  • Support for complex ints
  • Encoding via helper funcs
  • Decoding via helper funcs
  • Encoding helper funcs created by reflection
  • Decoding helper funcs created by reflection

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 {
	UnmarshalMum(*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() (v []byte, err error)

Bytes will return decoded bytes

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) Int8

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

Int8 decodes an int8 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) String

func (d *Decoder) String() (v 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) Uint8

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

Uint8 decodes a uint8 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

type Encodee

type Encodee interface {
	MarshalMum(*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)

Bool will encode a boolean value to the writer

func (*Encoder) Bytes

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

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)

Float32 encodes an float32 type

func (*Encoder) Float64

func (e *Encoder) Float64(v float64)

Float64 encodes an float64 type

func (*Encoder) Int

func (e *Encoder) Int(v int)

Int encodes an int type

func (*Encoder) Int8

func (e *Encoder) Int8(v int8)

Int8 encodes an int8 type

func (*Encoder) Int16

func (e *Encoder) Int16(v int16)

Int16 encodes an int16 type

func (*Encoder) Int32

func (e *Encoder) Int32(v int32)

Int32 encodes an int32 type

func (*Encoder) Int64

func (e *Encoder) Int64(v int64)

Int64 encodes an int64 type

func (*Encoder) String

func (e *Encoder) String(v string)

String will encode a string to the writer

func (*Encoder) Uint

func (e *Encoder) Uint(v uint)

Uint encodes a uint type

func (*Encoder) Uint8

func (e *Encoder) Uint8(v uint8)

Uint8 encodes a uint8 type

func (*Encoder) Uint16

func (e *Encoder) Uint16(v uint16)

Uint16 encodes a uint16 type

func (*Encoder) Uint32

func (e *Encoder) Uint32(v uint32)

Uint32 encodes a uint32 type

func (*Encoder) Uint64

func (e *Encoder) Uint64(v uint64)

Uint64 encodes a uint64 type

type Reader

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

Reader manages the writing of mum output

func NewReader

func NewReader(buffer []byte) *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

func (*Reader) SetBuffer

func (r *Reader) SetBuffer(bs []byte)

SetBuffer will replace the buffer bytes for a reader

type Writer

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

Writer manages the writing of mum output

func NewWriter

func NewWriter(buffer []byte) *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

Directories

Path Synopsis
example
basic command

Jump to

Keyboard shortcuts

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