coding

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2021 License: MIT Imports: 6 Imported by: 0

README

Package coding

Go Tests Go Reference

Package coding contains structures for encoding and decoding values.

Installing

Navigate to your module and execute the following.

$ go get github.com/colinc86/coding

Using

Import the package:

import "github.com/colinc86/coding"
Encoding Data

The Encoder type is responsible for encoding values.

Creating an Encoder

Calling NewEncoder creates a new encoder with an empty internal buffer.

e := coding.NewEncoder()
Encoding

The Encoder type supports encoding:

  • bool
  • int, int64, int32, int16, int8
  • uint, uint64, uint32, uint16, uint8
  • float64, float32
  • string
  • []byte

The order that you encode values is the order that they must be decoded with a Decoder.

e.EncodeBool(true)
e.EncodeString("Hello, World!")
e.EncodeInt(42)

d, err := json.Marshal(someStruct)
e.EncodeData(d)
Flushing Data

If you need to start over, you can call Flush on the encoder to clear its internal buffer.

e.Flush()
Getting Encoded Data

Use the Data function to get the encoder's encoded data. This method calculates the encoded data's CRC32 and appends the bytes to the end of the encoded data before returning it so it may be verified by a decoder.

encodedData := e.Data()
Compressing

You can, optionally, call the Compress function which will return a compressed version of the data returned by calling Data on the encoder.

compressedData, err := e.Compress()
Decoding

The Decoder type is responsible for decoding values.

Creating a Decoder

Call NewDecoder and give it the data to decode.

d := NewDecoder(compressedData)
Decompressing

If you obtained data from an encoder by calling Compress, then the first method you must call on the decoder is Decompress. Otherwise, this step is unnecessary and will return an error.

Calling Decompress decompresses the decoder's internal buffer so there is no need to keep track of new slices.

err := d.Decompress()
Validating Data

It is advised that you validate the decoder's data. Validating checks the CRC bytes that were appened to the encoder's data when calling Data.

if err := d.Validate; err != nil {
	fmt.Printf("Invalid data: %s\n", err)
}
Decoding Data

The Decoder type supports decoding all of the same types as encoders. As mentioned above, you must decode values in the order they were decoded.

// You should catch errors, but for the sake of brevity...
boolValue, _ := d.DecodeBool()
stringValue, _ := d.DecodeString()
intValue, _ := d.DecodeInt()
jsonData, _ := d.DecodeData()

someStruct = new(SomeStruct)
_ := json.Unmarshal(jsonData, someStruct)

Example

package main

import (
	"fmt"

	"github.com/colinc86/coding"
)

func main() {
	e := coding.NewEncoder()
	e.EncodeString("{ \"name\": \"pi\" }")
	e.EncodeFloat64(math.Pi)
	e.EncodeString("{ \"name\": \"phi\" }")
	e.EncodeFloat64(math.Phi)
	e.EncodeString("{ \"name\": \"e\" }")
	e.EncodeFloat64(math.E)
	e.EncodeString("{ \"name\": \"ln(2)\" }")
	e.EncodeFloat64(math.Ln2)

	fmt.Printf("Bytes: %d\n", len(e.Data()))

	cd, err := e.Compress()
	if err != nil {
		fmt.Printf("Error compressing data: %s\n", err)
		return
	}

	fmt.Printf("Compressed bytes: %d\n", len(cd))
	fmt.Printf("Change: %0.f%%\n", 100.0*float64(len(cd)-len(e.data))/float64(len(e.data)))

	d := coding.NewDecoder(cd)
	if err = d.Decompress(); err != nil {
		fmt.Printf("Error decompressing data: %s\n", err)
		return
	}

	if err = d.Validate(); err != nil {
		fmt.Printf("Error validating data: %s\n", err)
		return
	}

	var s string
	if s, err = d.DecodeString(); err != nil {
		fmt.Printf("Error decoding string: %s\n", err)
		return
	}
	fmt.Printf("String: %s\n", s)

	var f float64
	if f, err = d.DecodeFloat64(); err != nil {
		fmt.Printf("Error decoding float: %s\n", err)
		return
	}
	fmt.Printf("Float: %f\n", f)
}

Output:

Bytes: 153
Compressed bytes: 106
Change: -28%
String: { "name": "pi" }
Float: 3.141593

Documentation

Overview

Package coding contains structures for encoding and decoding values.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrEOB is an end of buffer error.
	ErrEOB error = errors.New("end of buffer")

	// ErrType is an incorrect type error.
	ErrType error = errors.New("incorrect type")

	// ErrByteLength is an incorrect byte length error.
	ErrByteLength error = errors.New("incorrect byte length")

	// ErrCRC is a CRC check error.
	ErrCRC = errors.New("crc check failed")
)

Functions

This section is empty.

Types

type Decoder

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

Decoder types decode bytes and keep track of an offset.

func NewDecoder

func NewDecoder(data []byte) *Decoder

NewDecoder creates and returns a new decoder with the given data.

func (*Decoder) DecodeBool

func (d *Decoder) DecodeBool() (bool, error)

DecodeBool decodes the next value as a boolean.

func (*Decoder) DecodeData

func (d *Decoder) DecodeData() ([]byte, error)

DecodeData decodes the next value as a byte array.

func (*Decoder) DecodeFloat32

func (d *Decoder) DecodeFloat32() (float32, error)

DecodeFloat32 decodes the next value as a floating point number.

func (*Decoder) DecodeFloat64

func (d *Decoder) DecodeFloat64() (float64, error)

DecodeFloat64 decodes the next value as a floating point number.

func (*Decoder) DecodeInt

func (d *Decoder) DecodeInt() (int, error)

DecodeInt decodes the next value as an integer.

func (*Decoder) DecodeInt16

func (d *Decoder) DecodeInt16() (int16, error)

DecodeInt16 decodes the next value as an integer.

func (*Decoder) DecodeInt32

func (d *Decoder) DecodeInt32() (int32, error)

DecodeInt32 decodes the next value as an integer.

func (*Decoder) DecodeInt64

func (d *Decoder) DecodeInt64() (int64, error)

DecodeInt64 decodes the next value as an integer.

func (*Decoder) DecodeInt8

func (d *Decoder) DecodeInt8() (int8, error)

DecodeInt8 decodes the next value as an integer.

func (*Decoder) DecodeString

func (d *Decoder) DecodeString() (string, error)

DecodeString decodes the next value as a string.

func (*Decoder) DecodeUint

func (d *Decoder) DecodeUint() (uint, error)

DecodeUint decodes the next value as an integer.

func (*Decoder) DecodeUint16

func (d *Decoder) DecodeUint16() (uint16, error)

DecodeUint16 decodes the next value as an integer.

func (*Decoder) DecodeUint32

func (d *Decoder) DecodeUint32() (uint32, error)

DecodeUint32 decodes the next value as an integer.

func (*Decoder) DecodeUint64

func (d *Decoder) DecodeUint64() (uint64, error)

DecodeUint64 decodes the next value as an integer.

func (*Decoder) DecodeUint8

func (d *Decoder) DecodeUint8() (uint8, error)

DecodeUint8 decodes the next value as an integer.

func (*Decoder) Decompress

func (d *Decoder) Decompress() error

Decompress decompresses the decoder's data and places the result in data.

Example
e := NewEncoder()
e.EncodeString("{ \"name\": \"pi\" }")
e.EncodeFloat64(math.Pi)
e.EncodeString("{ \"name\": \"phi\" }")
e.EncodeFloat64(math.Phi)
e.EncodeString("{ \"name\": \"e\" }")
e.EncodeFloat64(math.E)
e.EncodeString("{ \"name\": \"ln(2)\" }")
e.EncodeFloat64(math.Ln2)

fmt.Printf("Bytes: %d\n", len(e.Data()))

cd, err := e.Compress()
if err != nil {
	fmt.Printf("Error compressing data: %s\n", err)
	return
}

fmt.Printf("Compressed bytes: %d\n", len(cd))
fmt.Printf("Change: %0.f%%\n", 100.0*float64(len(cd)-len(e.data))/float64(len(e.data)))

d := NewDecoder(cd)
if err = d.Decompress(); err != nil {
	fmt.Printf("Error decompressing data: %s\n", err)
	return
}

if err = d.Validate(); err != nil {
	fmt.Printf("Error validating data: %s\n", err)
	return
}

var s string
if s, err = d.DecodeString(); err != nil {
	fmt.Printf("Error decoding string: %s\n", err)
	return
}
fmt.Printf("String: %s\n", s)

var f float64
if f, err = d.DecodeFloat64(); err != nil {
	fmt.Printf("Error decoding float: %s\n", err)
	return
}
fmt.Printf("Float: %f\n", f)
Output:

Bytes: 153
Compressed bytes: 106
Change: -28%
String: { "name": "pi" }
Float: 3.141593

func (Decoder) Validate

func (d Decoder) Validate() error

Validate validates the decoder's data by calculating its CRC32 and comparing.

type Encoder

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

Encoder types encode encode values to binary data.

func NewEncoder

func NewEncoder() *Encoder

NewEncoder creates a new encoder.

func (*Encoder) Compress

func (e *Encoder) Compress() ([]byte, error)

Compress compresses the encoder's data and returns the result.

Compress calls the encoder's Data function so that its data's CRC is included in the compressed bytes.

func (Encoder) Data

func (e Encoder) Data() []byte

Data returns the encoder's data along with trailing CRC data.

func (*Encoder) EncodeBool

func (e *Encoder) EncodeBool(b bool)

EncodeBool encodes a boolean.

func (*Encoder) EncodeData

func (e *Encoder) EncodeData(b []byte)

EncodeData encodes the data.

func (*Encoder) EncodeFloat32

func (e *Encoder) EncodeFloat32(f float32)

EncodeFloat32 encodes a float.

func (*Encoder) EncodeFloat64

func (e *Encoder) EncodeFloat64(f float64)

EncodeFloat64 encodes a float.

func (*Encoder) EncodeInt

func (e *Encoder) EncodeInt(n int)

EncodeInt encodes an integer.

func (*Encoder) EncodeInt16

func (e *Encoder) EncodeInt16(n int16)

EncodeInt16 encodes an integer.

func (*Encoder) EncodeInt32

func (e *Encoder) EncodeInt32(n int32)

EncodeInt32 encodes an integer.

func (*Encoder) EncodeInt64

func (e *Encoder) EncodeInt64(n int64)

EncodeInt64 encodes an integer.

func (*Encoder) EncodeInt8

func (e *Encoder) EncodeInt8(n int8)

EncodeInt8 encodes an integer.

func (*Encoder) EncodeString

func (e *Encoder) EncodeString(s string)

EncodeString encodes the string.

func (*Encoder) EncodeUint

func (e *Encoder) EncodeUint(n uint)

EncodeUint encodes an integer.

func (*Encoder) EncodeUint16

func (e *Encoder) EncodeUint16(n uint16)

EncodeUint16 encodes an integer.

func (*Encoder) EncodeUint32

func (e *Encoder) EncodeUint32(n uint32)

EncodeUint32 encodes an integer.

func (*Encoder) EncodeUint64

func (e *Encoder) EncodeUint64(n uint64)

EncodeUint64 encodes an integer.

func (*Encoder) EncodeUint8

func (e *Encoder) EncodeUint8(n uint8)

EncodeUint8 encodes an integer.

func (*Encoder) Flush

func (e *Encoder) Flush()

Flush clears the encoder's data.

Jump to

Keyboard shortcuts

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