codec

package module
v1.0.0 Latest Latest
Warning

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

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

README

codec

GoDoc Build Status codecov Go Report Card GitHub release LICENSE

Package codec implements encoding and decoding of multiple codecs

Feature

Get started

Install
go get github.com/hslam/codec
Import
import "github.com/hslam/codec"
Usage
Example
package main

import (
	"fmt"
	"github.com/hslam/codec"
	"github.com/hslam/codec/example/code"
	"github.com/hslam/codec/example/codepb"
	"github.com/hslam/codec/example/gencode"
	"github.com/hslam/codec/example/gogopb"
	"github.com/hslam/codec/example/model"
	"github.com/hslam/codec/example/msgp"
	"github.com/hslam/codec/example/pb"
)

func main() {
	BYTES()
	CODE()
	GENCODE()
	CODEPB()
	MSGP()
	GOGOPB()
	PB()
	JSON()
	XML()
	GOB()
}

//BYTES Example
func BYTES() {
	var obj = []byte{128, 8, 128, 8, 195, 245, 72, 64, 74, 216, 18, 77, 251, 33, 9, 64, 10, 72, 101, 108, 108, 111, 87, 111, 114, 108, 100, 1, 1, 255, 2, 1, 128, 1, 255}
	c := &codec.BYTESCodec{}
	data, _ := c.Encode(&obj)
	fmt.Printf("bytes Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy []byte
	c.Decode(data, &objCopy)
	fmt.Println("bytes Decode:", objCopy)
}

//CODE Example
func CODE() {
	var obj = code.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: "HelloWorld", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}
	c := &codec.CODECodec{Buffer: make([]byte, 512)}
	data, _ := c.Encode(&obj)
	fmt.Printf("code Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy code.Object
	c.Decode(data, &objCopy)
	fmt.Println("code Decode:", objCopy)
}

//GENCODE Example
func GENCODE() {
	var obj = gencode.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: "HelloWorld", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}
	c := &codec.CODECodec{Buffer: make([]byte, 512)}
	data, _ := c.Encode(&obj)
	fmt.Printf("gencode Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy gencode.Object
	c.Decode(data, &objCopy)
	fmt.Println("gencode Decode:", objCopy)
}

//CODEPB Example
func CODEPB() {
	var obj = codepb.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: "HelloWorld", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}
	c := &codec.CODECodec{Buffer: make([]byte, 512)}
	data, _ := c.Encode(&obj)
	fmt.Printf("codepb Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy codepb.Object
	c.Decode(data, &objCopy)
	fmt.Println("codepb Decode:", objCopy)
}

//MSGP Example
func MSGP() {
	var obj = msgp.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: "HelloWorld", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}
	c := &codec.MSGPCodec{Buffer: make([]byte, 512)}
	data, _ := c.Encode(&obj)
	fmt.Printf("msgp Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy msgp.Object
	c.Decode(data, &objCopy)
	fmt.Println("msgp Decode:", objCopy)
}

//GOGOPB Example
func GOGOPB() {
	var obj = gogopb.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: "HelloWorld", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}
	c := &codec.GOGOPBCodec{Buffer: make([]byte, 512)}
	data, _ := c.Encode(&obj)
	fmt.Printf("gogopb Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy gogopb.Object
	c.Decode(data, &objCopy)
	fmt.Println("gogopb Decode:", objCopy)
}

//PB Example
func PB() {
	var obj = pb.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: "HelloWorld", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}
	c := &codec.PBCodec{}
	data, _ := c.Encode(&obj)
	fmt.Printf("pb Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy pb.Object
	c.Decode(data, &objCopy)
	fmt.Println("pb Decode:", objCopy)
}

//JSON Example
func JSON() {
	var obj = model.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: "HelloWorld", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}
	c := &codec.JSONCodec{}
	data, _ := c.Encode(&obj)
	fmt.Printf("json Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy model.Object
	c.Decode(data, &objCopy)
	fmt.Println("json Decode:", objCopy)
}

//XML Example
func XML() {
	var obj = model.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: "HelloWorld", F: true}
	c := &codec.XMLCodec{}
	data, _ := c.Encode(&obj)
	fmt.Printf("xml Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy model.Object
	c.Decode(data, &objCopy)
	fmt.Println("xml Decode:", objCopy)
}

//GOB Example
func GOB() {
	var obj = model.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: "HelloWorld", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}
	c := &codec.GOBCodec{}
	data, _ := c.Encode(&obj)
	fmt.Printf("gob Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy model.Object
	c.Decode(data, &objCopy)
	fmt.Println("gob Decode:", objCopy)
}
Output
bytes Encode:length-35,hex-80088008c3f548404ad8124dfb2109400a48656c6c6f576f726c640101ff02018001ff
bytes Decode: [128 8 128 8 195 245 72 64 74 216 18 77 251 33 9 64 10 72 101 108 108 111 87 111 114 108 100 1 1 255 2 1 128 1 255]
code Encode:length-35,hex-80088008c3f548404ad8124dfb2109400a48656c6c6f576f726c640101ff02018001ff
code Decode: {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}
gencode Encode:length-43,hex-000400000004000000000000c3f548404ad8124dfb2109400a48656c6c6f576f726c640101ff02018001ff
gencode Decode: {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}
codepb Encode:length-43,hex-0880081080081dc3f54840214ad8124dfb2109402a0a48656c6c6f576f726c6430013a01ff4201804201ff
codepb Decode: {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}
msgp Encode:length-59,hex-88a141cd0400a142cd0400a143ca4048f5c3a144cb400921fb4d12d84aa145aa48656c6c6f576f726c64a146c3a147c401ffa14892c40180c401ff
msgp Decode: {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}
gogopb Encode:length-43,hex-0880081080081dc3f54840214ad8124dfb2109402a0a48656c6c6f576f726c6430013a01ff4201804201ff
gogopb Decode: {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}
pb Encode:length-43,hex-0880081080081dc3f54840214ad8124dfb2109402a0a48656c6c6f576f726c6430013a01ff4201804201ff
pb Decode: {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]] {} [] 0}
json Encode:length-99,hex-7b2241223a313032342c2242223a313032342c2243223a332e31342c2244223a332e313431353932362c2245223a2248656c6c6f576f726c64222c2246223a747275652c2247223a222f773d3d222c2248223a5b2267413d3d222c222f773d3d225d7d
json Decode: {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}
xml Encode:length-94,hex-3c4f626a6563743e3c413e313032343c2f413e3c423e313032343c2f423e3c433e332e31343c2f433e3c443e332e313431353932363c2f443e3c453e48656c6c6f576f726c643c2f453e3c463e747275653c2f463e3c2f4f626a6563743e
xml Decode: {1024 1024 3.14 3.1415926 HelloWorld true [] []}
gob Encode:length-146,hex-45ff81030101064f626a65637401ff82000108010141010600010142010600010143010800010144010800010145010c00010146010200010147010a0001014801ff8400000017ff83020101095b5d5b5d75696e743801ff8400010a000033ff8201fe040001fe040001fb60b81e094001f84ad8124dfb210940010a48656c6c6f576f726c6401010101ff0102018001ff00
gob Decode: {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}
Benchmark

go test -v -run="none" -bench=. -benchtime=1s

goos: darwin
goarch: amd64
pkg: github.com/hslam/codec
BenchmarkEncodeBYTES-4        	1000000000	         0.617 ns/op
BenchmarkEncodeCODE-4         	18222936	        64.2 ns/op
BenchmarkEncodeGENCODE-4      	21402268	        54.6 ns/op
BenchmarkEncodeCODEPB-4       	17994428	        64.9 ns/op
BenchmarkEncodeMSGP-4         	13502540	        87.5 ns/op
BenchmarkEncodeGOGOPB-4       	15440156	        76.1 ns/op
BenchmarkEncodePB-4           	 4889565	       243 ns/op
BenchmarkEncodeJSON-4         	 1267345	       943 ns/op
BenchmarkEncodeXML-4          	  293656	      4069 ns/op
BenchmarkEncodeGOB-4          	  199935	      5675 ns/op
BenchmarkDecodeBYTES-4        	1000000000	         0.961 ns/op
BenchmarkDecodeCODE-4         	23251303	        50.4 ns/op
BenchmarkDecodeGENCODE-4      	16884618	        69.7 ns/op
BenchmarkDecodeCODEPB-4       	16531594	        71.0 ns/op
BenchmarkDecodeMSGP-4         	 3916285	       305 ns/op
BenchmarkDecodeGOGOPB-4       	 2650914	       395 ns/op
BenchmarkDecodePB-4           	 2646182	       447 ns/op
BenchmarkDecodeJSON-4         	  428173	      2828 ns/op
BenchmarkDecodeXML-4          	  144920	      8089 ns/op
BenchmarkDecodeGOB-4          	   47562	     25011 ns/op
BenchmarkRoundtripBYTES-4     	1000000000	         1.11 ns/op
BenchmarkRoundtripCODE-4      	10003128	       119 ns/op
BenchmarkRoundtripGENCODE-4   	 9484033	       126 ns/op
BenchmarkRoundtripCODEPB-4    	 8389878	       142 ns/op
BenchmarkRoundtripMSGP-4      	 2885835	       415 ns/op
BenchmarkRoundtripGOGOPB-4    	 2837972	       464 ns/op
BenchmarkRoundtripPB-4        	 1637950	       730 ns/op
BenchmarkRoundtripJSON-4      	  296553	      3953 ns/op
BenchmarkRoundtripXML-4       	   94116	     12485 ns/op
BenchmarkRoundtripGOB-4       	   37731	     31733 ns/op
PASS
ok  	github.com/hslam/codec	41.632s
License

This package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)

Authors

codec was written by Meng Huang.

Documentation

Overview

Package codec implements encoding and decoding of multiple codecs

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BYTESCodec

type BYTESCodec struct {
}

BYTESCodec struct

func (*BYTESCodec) Decode

func (c *BYTESCodec) Decode(data []byte, v interface{}) error

Decode parses the BYTES-encoded data and stores the result in the value pointed to by v.

func (*BYTESCodec) Encode

func (c *BYTESCodec) Encode(v interface{}) ([]byte, error)

Encode returns the BYTES encoding of v.

type BYTESCodecPool

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

BYTESCodecPool implements a pool of BYTESCodec in the form of a bounded channel.

func NewBYTESCodecPool

func NewBYTESCodecPool(total int) *BYTESCodecPool

NewBYTESCodecPool creates a new BYTESCodecPool bounded to the given total.

func (*BYTESCodecPool) Get

func (cp *BYTESCodecPool) Get() (c Codec)

Get gets a BYTESCodec from the BYTESCodecPool, or creates a new one if none are available in the pool.

func (*BYTESCodecPool) Put

func (cp *BYTESCodecPool) Put(c Codec)

Put returns the given BYTESCodec to the BYTESCodecPool.

type CODECodec

type CODECodec struct {
	Buffer []byte
}

CODECodec struct

func (*CODECodec) Decode

func (c *CODECodec) Decode(data []byte, v interface{}) error

Decode parses the CODE-encoded data and stores the result in the value pointed to by v.

func (*CODECodec) Encode

func (c *CODECodec) Encode(v interface{}) ([]byte, error)

Encode returns the CODE encoding of v.

type CODECodecPool

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

CODECodecPool implements a pool of CODECodec in the form of a bounded channel.

func NewCODECodecPool

func NewCODECodecPool(total int, width int) *CODECodecPool

NewCODECodecPool creates a new CODECodecPool bounded to the given total.

func (*CODECodecPool) Get

func (cp *CODECodecPool) Get() (c Codec)

Get gets a CODECodec from the CODECodecPool, or creates a new one if none are available in the pool.

func (*CODECodecPool) Put

func (cp *CODECodecPool) Put(c Codec)

Put returns the given CODECodec to the CODECodecPool.

type Code

type Code interface {
	Marshal(buf []byte) ([]byte, error)
	Unmarshal(buf []byte) (uint64, error)
}

Code defines the interface for code.

type Codec

type Codec interface {
	Encode(v interface{}) ([]byte, error)
	Decode(data []byte, v interface{}) error
}

Codec defines the interface for encoding/decoding.

type GOBCodec

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

GOBCodec struct

func (*GOBCodec) Decode

func (c *GOBCodec) Decode(data []byte, v interface{}) error

Decode parses the GOB-encoded data and stores the result in the value pointed to by v.

func (*GOBCodec) Encode

func (c *GOBCodec) Encode(v interface{}) ([]byte, error)

Encode returns the GOB encoding of v.

type GOBCodecPool

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

GOBCodecPool implements a pool of GOBCodec in the form of a bounded channel.

func NewGOBCodecPool

func NewGOBCodecPool(total int) *GOBCodecPool

NewGOBCodecPool creates a new GOBCodecPool bounded to the given total.

func (*GOBCodecPool) Get

func (cp *GOBCodecPool) Get() (c Codec)

Get gets a GOBCodec from the GOBCodecPool, or creates a new one if none are available in the pool.

func (*GOBCodecPool) Put

func (cp *GOBCodecPool) Put(c Codec)

Put returns the given GOBCodec to the GOBCodecPool.

type GOGOPBCodec

type GOGOPBCodec struct {
	Buffer []byte
}

GOGOPBCodec struct

func (*GOGOPBCodec) Decode

func (c *GOGOPBCodec) Decode(data []byte, v interface{}) error

Decode parses the GOGOPB-encoded data and stores the result in the value pointed to by v.

func (*GOGOPBCodec) Encode

func (c *GOGOPBCodec) Encode(v interface{}) ([]byte, error)

Encode returns the GOGOPB encoding of v.

type GOGOPBCodecPool

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

GOGOPBCodecPool implements a pool of GOGOPBCodec in the form of a bounded channel.

func NewGOGOPBCodecPool

func NewGOGOPBCodecPool(total int, width int) *GOGOPBCodecPool

NewGOGOPBCodecPool creates a new GOGOPBCodecPool bounded to the given total.

func (*GOGOPBCodecPool) Get

func (cp *GOGOPBCodecPool) Get() (c Codec)

Get gets a GOGOPBCodec from the GOGOPBCodecPool, or creates a new one if none are available in the pool.

func (*GOGOPBCodecPool) Put

func (cp *GOGOPBCodecPool) Put(c Codec)

Put returns the given GOGOPBCodec to the GOGOPBCodecPool.

type JSONCodec

type JSONCodec struct {
}

JSONCodec struct

func (*JSONCodec) Decode

func (c *JSONCodec) Decode(data []byte, v interface{}) error

Decode parses the JSON-encoded data and stores the result in the value pointed to by v.

func (*JSONCodec) Encode

func (c *JSONCodec) Encode(v interface{}) ([]byte, error)

Encode returns the JSON encoding of v.

type JSONCodecPool

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

JSONCodecPool implements a pool of JSONCodec in the form of a bounded channel.

func NewJSONCodecPool

func NewJSONCodecPool(total int) *JSONCodecPool

NewJSONCodecPool creates a new JSONCodecPool bounded to the given total.

func (*JSONCodecPool) Get

func (cp *JSONCodecPool) Get() (c Codec)

Get gets a JSONCodec from the JSONCodecPool, or creates a new one if none are available in the pool.

func (*JSONCodecPool) Put

func (cp *JSONCodecPool) Put(c Codec)

Put returns the given JSONCodec to the JSONCodecPool.

type MSGPCodec

type MSGPCodec struct {
	Buffer []byte
}

MSGPCodec struct

func (*MSGPCodec) Decode

func (c *MSGPCodec) Decode(data []byte, v interface{}) error

Decode parses the MSGP-encoded data and stores the result in the value pointed to by v.

func (*MSGPCodec) Encode

func (c *MSGPCodec) Encode(v interface{}) ([]byte, error)

Encode returns the MSGP encoding of v.

type MSGPCodecPool

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

MSGPCodecPool implements a pool of MSGPCodec in the form of a bounded channel.

func NewMSGPCodecPool

func NewMSGPCodecPool(total int, width int) *MSGPCodecPool

NewMSGPCodecPool creates a new MSGPCodecPool bounded to the given total.

func (*MSGPCodecPool) Get

func (cp *MSGPCodecPool) Get() (c Codec)

Get gets a MSGPCodec from the MSGPCodecPool, or creates a new one if none are available in the pool.

func (*MSGPCodecPool) Put

func (cp *MSGPCodecPool) Put(c Codec)

Put returns the given MSGPCodec to the MSGPCodecPool.

type PBCodec

type PBCodec struct {
}

PBCodec struct

func (*PBCodec) Decode

func (c *PBCodec) Decode(data []byte, v interface{}) error

Decode parses the PB-encoded data and stores the result in the value pointed to by v.

func (*PBCodec) Encode

func (c *PBCodec) Encode(v interface{}) ([]byte, error)

Encode returns the PB encoding of v.

type PBCodecPool

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

PBCodecPool implements a pool of PBCodec in the form of a bounded channel.

func NewPBCodecPool

func NewPBCodecPool(total int) *PBCodecPool

NewPBCodecPool creates a new PBCodecPool bounded to the given total.

func (*PBCodecPool) Get

func (cp *PBCodecPool) Get() (c Codec)

Get gets a PBCodec from the PBCodecPool, or creates a new one if none are available in the pool.

func (*PBCodecPool) Put

func (cp *PBCodecPool) Put(c Codec)

Put returns the given PBCodec to the PBCodecPool.

type Pool

type Pool interface {
	Get() Codec
	Put(c Codec)
}

Pool defines the interface of codec pool.

type XMLCodec

type XMLCodec struct {
}

XMLCodec struct

func (*XMLCodec) Decode

func (c *XMLCodec) Decode(data []byte, v interface{}) error

Decode parses the XML-encoded data and stores the result in the value pointed to by v.

func (*XMLCodec) Encode

func (c *XMLCodec) Encode(v interface{}) ([]byte, error)

Encode returns the XML encoding of v.

type XMLCodecPool

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

XMLCodecPool implements a pool of XMLCodec in the form of a bounded channel.

func NewXMLCodecPool

func NewXMLCodecPool(total int) *XMLCodecPool

NewXMLCodecPool creates a new XMLCodecPool bounded to the given total.

func (*XMLCodecPool) Get

func (cp *XMLCodecPool) Get() (c Codec)

Get gets a XMLCodec from the XMLCodecPool, or creates a new one if none are available in the pool.

func (*XMLCodecPool) Put

func (cp *XMLCodecPool) Put(c Codec)

Put returns the given XMLCodec to the XMLCodecPool.

Directories

Path Synopsis
gogopb
Package gogopb is a generated protocol buffer package.
Package gogopb is a generated protocol buffer package.
pb

Jump to

Keyboard shortcuts

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