objpack

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2018 License: MIT Imports: 8 Imported by: 1

README

objpack

A tiny and stupid object packing/unpacking package written in Golang for my personal project

Limits and Features

Limits
  • All fields in a struct must be exported.
  • All your data's types must be explicit.
  • To unpack data properly, the types' signature of original object and target object must match.
    • This means once you packed your object, you'd better not change the structure for your data, or you will need to convert them manually.
  • complex64/complex128 has not been supported.
  • circular references will cause stack overflow.
Supported Types
  • basic types

    • bool
    • uint uint8 uint16 uint32 uint64 byte
    • int int8 int16 int32 int64
    • float32 float64
    • string
  • arrays, slices and maps of supported type

  • structs that all fields are exported, and all fields' type has been supported

Example

type EmbeddedStruct struct {
    SliceField []*bool
    MapField   map[string]uint
}
type Data struct {
    Float64Field float64
    StringField  string
    StructField  EmbeddedStruct
}

var originalData = Data{123.4, "Hello", EmbeddedStruct{[]*bool{new(bool), new(bool)}, map[string]uint{"World": 0, "!": 1}}}

var signature, errSignature = objpack.MakeTypeSignature(originalData)
if errSignature != nil {
    panic(errSignature)
}
fmt.Println(signature)

var packedData, errPack = objpack.Pack(originalData)
if errPack != nil {
    panic(errPack)
}
fmt.Printf("%v\n", packedData)

var unpackedDataPointer = reflect.New(reflect.TypeOf(Data{})).Interface()
var errUnpack = objpack.Unpack(packedData, unpackedDataPointer)
if errUnpack != nil {
    panic(errUnpack)
}
var unpackedData = *unpackedDataPointer.(*Data)
fmt.Println(reflect.DeepEqual(originalData, unpackedData))

Todo

  • Handling nil
  • Comments/Documents
  • Packer/Unpacker
  • Benchmark
  • Improve Performance

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeTypeSignature

func MakeTypeSignature(object interface{}) (result string, err error)

MakeTypeSignature returns the signature of the type of given object.

func Pack

func Pack(object interface{}) (result []byte, err error)

Pack returns the packed data of object.

func PackToWriter

func PackToWriter(writer io.Writer, v reflect.Value) (err error)

PackToWriter packs and writes the packed data of object to writer

func Unpack

func Unpack(data []byte, object interface{}) (err error)

Unpack unpacks the encoded data and stores the result in the value pointed to by object.

func UnpackFromReader

func UnpackFromReader(reader *bytes.Reader, v reflect.Value) (err error)

UnpackFromReader reads the encoded data from reader and unpacks and stores the result in the value pointed to by v.

Types

type BadUnpackTypeError

type BadUnpackTypeError struct {
	Type reflect.Type
}

BadUnpackTypeError 是在 unpack 时传入了非指针类型 object 参数是返回的错误

func (BadUnpackTypeError) Error

func (e BadUnpackTypeError) Error() string

type ErrCircling

type ErrCircling struct {
	Type reflect.Type
}

ErrCircling 实在 unpack 中发生 panic 时返回的错误

func (ErrCircling) Error

func (e ErrCircling) Error() string

type PanicInUnpackingError

type PanicInUnpackingError struct {
	Panic interface{}
}

PanicInUnpackingError 实在 unpack 中发生 panic 时返回的错误

func (PanicInUnpackingError) Error

func (e PanicInUnpackingError) Error() string

type UnexportedFieldError

type UnexportedFieldError struct {
	Field reflect.StructField
}

UnexportedFieldError 是在结构体中遇到未导出的字段时返回的错误

func (UnexportedFieldError) Error

func (e UnexportedFieldError) Error() string

type UnresolvableTypeError

type UnresolvableTypeError struct {
	Type reflect.Type
}

UnresolvableTypeError 是遇到无法 pack/unpack 的类型时返回的错误

func (UnresolvableTypeError) Error

func (e UnresolvableTypeError) Error() string

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

UnsupportedTypeError 是遇到尚未支持的类型时返回的错误

func (UnsupportedTypeError) Error

func (e UnsupportedTypeError) Error() string

Jump to

Keyboard shortcuts

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