codec

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2020 License: MIT Imports: 5 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"
	codecpb "github.com/hslam/codec/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{}
	var buf = make([]byte, 512)
	data, _ := c.Marshal(buf, &obj)
	fmt.Printf("bytes Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy []byte
	c.Unmarshal(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{}
	var buf = make([]byte, 512)
	data, _ := c.Marshal(buf, &obj)
	fmt.Printf("code Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy code.Object
	c.Unmarshal(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{}
	var buf = make([]byte, 512)
	data, _ := c.Marshal(buf, &obj)
	fmt.Printf("gencode Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy gencode.Object
	c.Unmarshal(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{}
	var buf = make([]byte, 512)
	data, _ := c.Marshal(buf, &obj)
	fmt.Printf("codepb Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy codepb.Object
	c.Unmarshal(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{}
	var buf = make([]byte, 512)
	data, _ := c.Marshal(buf, &obj)
	fmt.Printf("msgp Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy msgp.Object
	c.Unmarshal(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{}
	var buf = make([]byte, 512)
	data, _ := c.Marshal(buf, &obj)
	fmt.Printf("gogopb Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy gogopb.Object
	c.Unmarshal(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 := &codecpb.Codec{}
	var buf = make([]byte, 512)
	data, _ := c.Marshal(buf, &obj)
	fmt.Printf("pb Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy pb.Object
	c.Unmarshal(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{}
	var buf = make([]byte, 512)
	data, _ := c.Marshal(buf, &obj)
	fmt.Printf("json Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy model.Object
	c.Unmarshal(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{}
	var buf = make([]byte, 512)
	data, _ := c.Marshal(buf, &obj)
	fmt.Printf("xml Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy model.Object
	c.Unmarshal(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{}
	var buf = make([]byte, 512)
	data, _ := c.Marshal(buf, &obj)
	fmt.Printf("gob Encode:length-%d,hex-%x\n", len(data), data)
	var objCopy model.Object
	c.Unmarshal(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
BenchmarkMarshalBYTES-4       	1000000000	         0.394 ns/op
BenchmarkMarshalCODE-4        	18543157	        63.9 ns/op
BenchmarkMarshalGENCODE-4     	22040894	        53.5 ns/op
BenchmarkMarshalCODEPB-4      	17600244	        66.0 ns/op
BenchmarkMarshalMSGP-4        	13804621	        86.7 ns/op
BenchmarkMarshalGOGOPB-4      	11963642	        98.8 ns/op
BenchmarkMarshalPB-4          	 3721976	       311 ns/op
BenchmarkMarshalJSON-4        	 1268824	       947 ns/op
BenchmarkMarshalXML-4         	  289130	      4086 ns/op
BenchmarkMarshalGOB-4         	  205197	      5849 ns/op
BenchmarkUnmarshalBYTES-4     	1000000000	         0.960 ns/op
BenchmarkUnmarshalCODE-4      	23269940	        50.6 ns/op
BenchmarkUnmarshalGENCODE-4   	16862955	        69.9 ns/op
BenchmarkUnmarshalCODEPB-4    	16749501	        70.0 ns/op
BenchmarkUnmarshalMSGP-4      	 3930744	       305 ns/op
BenchmarkUnmarshalGOGOPB-4    	 2558491	       445 ns/op
BenchmarkUnmarshalPB-4        	 2313656	       516 ns/op
BenchmarkUnmarshalJSON-4      	  418756	      2880 ns/op
BenchmarkUnmarshalXML-4       	  143637	      8256 ns/op
BenchmarkUnmarshalGOB-4       	   47180	     25421 ns/op
BenchmarkRoundtripBYTES-4     	1000000000	         1.10 ns/op
BenchmarkRoundtripCODE-4      	10084428	       117 ns/op
BenchmarkRoundtripGENCODE-4   	 9428412	       126 ns/op
BenchmarkRoundtripCODEPB-4    	 8580951	       139 ns/op
BenchmarkRoundtripMSGP-4      	 2873107	       416 ns/op
BenchmarkRoundtripGOGOPB-4    	 2705097	       474 ns/op
BenchmarkRoundtripPB-4        	 1378863	       868 ns/op
BenchmarkRoundtripJSON-4      	  291931	      4071 ns/op
BenchmarkRoundtripXML-4       	   93944	     12529 ns/op
BenchmarkRoundtripGOB-4       	   37376	     32642 ns/op
PASS
ok  	github.com/hslam/codec	42.864s
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

View Source
var ErrorCODE = errors.New("is not Code")

ErrorCODE is the error that v is not Code

View Source
var ErrorGOGOPB = errors.New("is not GoGoProtobuf")

ErrorGOGOPB is the error that v is not GoGoProtobuf

View Source
var ErrorMSGP = errors.New("is not MSGP")

ErrorMSGP is the error that v is not MSGP

Functions

This section is empty.

Types

type BYTESCodec

type BYTESCodec struct {
}

BYTESCodec struct

func (*BYTESCodec) Marshal

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

Marshal returns the BYTES encoding of v.

func (*BYTESCodec) Unmarshal

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

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

type CODECodec

type CODECodec struct {
}

CODECodec struct

func (*CODECodec) Marshal

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

Marshal returns the CODE encoding of v.

func (*CODECodec) Unmarshal

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

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

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 {
	Marshal(buf []byte, v interface{}) ([]byte, error)
	Unmarshal(data []byte, v interface{}) error
}

Codec defines the interface for encoding/decoding.

type GOBCodec

type GOBCodec struct {
}

GOBCodec struct

func (*GOBCodec) Marshal

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

Marshal returns the GOB encoding of v.

func (*GOBCodec) Unmarshal

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

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

type GOGOPBCodec

type GOGOPBCodec struct {
}

GOGOPBCodec struct

func (*GOGOPBCodec) Marshal

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

Marshal returns the GOGOPB encoding of v.

func (*GOGOPBCodec) Unmarshal

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

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

type GoGoProtobuf

type GoGoProtobuf interface {
	Size() (n int)
	Marshal() (data []byte, err error)
	MarshalTo(buf []byte) (int, error)
	Unmarshal(data []byte) error
}

GoGoProtobuf defines the interface for gogo's protobuf.

type JSONCodec

type JSONCodec struct {
}

JSONCodec struct

func (*JSONCodec) Marshal

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

Marshal returns the JSON encoding of v.

func (*JSONCodec) Unmarshal

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

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

type MSGPCodec

type MSGPCodec struct {
}

MSGPCodec struct

func (*MSGPCodec) Marshal

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

Marshal returns the MSGP encoding of v.

func (*MSGPCodec) Unmarshal

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

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

type MsgPack

type MsgPack interface {
	MarshalMsg(buf []byte) ([]byte, error)
	UnmarshalMsg(bts []byte) (o []byte, err error)
}

MsgPack defines the interface for msgp.

type XMLCodec

type XMLCodec struct {
}

XMLCodec struct

func (*XMLCodec) Marshal

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

Marshal returns the XML encoding of v.

func (*XMLCodec) Unmarshal

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

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

Source Files

  • codec.go

Directories

Path Synopsis
pb

Jump to

Keyboard shortcuts

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