mum

package module
v0.4.83 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2020 License: MIT Imports: 5 Imported by: 0

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

View Source
const (
	// ErrInvalidLength is returned when a byteslice has an invalid length for it's desired primitive
	ErrInvalidLength = errors.Error("invalid length")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BinaryReader

type BinaryReader struct{}

BinaryReader will read numbers from binary bytes

func (*BinaryReader) Int16

func (b *BinaryReader) Int16(bs []byte) (v int16, err error)

Int16 will return the int16 value from a provided byteslice

func (*BinaryReader) Int32

func (b *BinaryReader) Int32(bs []byte) (v int32, err error)

Int32 will return the int32 value from a provided byteslice

func (*BinaryReader) Int64

func (b *BinaryReader) Int64(bs []byte) (v int64, err error)

Int64 will return the int64 value from a provided byteslice

func (*BinaryReader) Uint16

func (b *BinaryReader) Uint16(bs []byte) (v uint16, err error)

Uint16 will return the uint16 value from a provided byteslice

func (*BinaryReader) Uint32

func (b *BinaryReader) Uint32(bs []byte) (v uint32, err error)

Uint32 will return the uint32 value from a provided byteslice

func (*BinaryReader) Uint64

func (b *BinaryReader) Uint64(bs []byte) (v uint64, err error)

Uint64 will return the uint64 value from a provided byteslice

type BinaryWriter

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

BinaryWriter will write numbers as binary bytes

func (*BinaryWriter) Int8

func (b *BinaryWriter) Int8(v int8) []byte

Int8 will return the byteslice representation of a int8 value

func (*BinaryWriter) Int16

func (b *BinaryWriter) Int16(v int16) []byte

Int16 will return the byteslice representation of a int16 value

func (*BinaryWriter) Int32

func (b *BinaryWriter) Int32(v int32) []byte

Int32 will return the byteslice representation of a int32 value

func (*BinaryWriter) Int64

func (b *BinaryWriter) Int64(v int64) []byte

Int64 will return the byteslice representation of a int64 value

func (*BinaryWriter) Uint8

func (b *BinaryWriter) Uint8(v uint8) []byte

Uint8 will return the byteslice representation of a uint8 value

func (*BinaryWriter) Uint16

func (b *BinaryWriter) Uint16(v uint16) []byte

Uint16 will return the byteslice representation of a uint16 value

func (*BinaryWriter) Uint32

func (b *BinaryWriter) Uint32(v uint32) []byte

Uint32 will return the byteslice representation of a uint32 value

func (*BinaryWriter) Uint64

func (b *BinaryWriter) Uint64(v uint64) []byte

Uint64 will return the byteslice representation of a uint64 value

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 NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder will return a new Decoder

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

func (d *Decoder) BytesUnsafe() (v []byte, err error)

BytesUnsafe will return decoded bytes without copying

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 NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder will return a new Encoder

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

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

Int8 encodes an int8 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) 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) Uint8

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

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

type Type

type Type uint8

Type represents a primitive type

const (
	// Nil is the zero-value for the Types block
	Nil Type = iota
	// UInt8 represents uint8
	UInt8
	// UInt16 represents uint16
	UInt16
	// UInt32 represents uint32
	UInt32
	// UInt64 represents uin64
	UInt64
	// Int8 represents int8
	Int8
	// Int16 represents int16
	Int16
	// Int32 represents int32
	Int32
	// Int64 represents int64
	Int64
	// Bytes represents a byteslice
	Bytes
	// String represents a string
	String
)

Directories

Path Synopsis
example
basic command

Jump to

Keyboard shortcuts

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