From f5ec5eb58d4bcd20adf906da04ecf34cd50db316 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 29 Aug 2022 15:08:50 +0800 Subject: [PATCH] feat: add EncodeByte and DecodeByte --- convertor/convertor.go | 19 +++++++++ convertor/convertor_test.go | 18 ++++++++ docs/convertor.md | 57 +++++++++++++++++++++++++ docs/convertor_zh-CN.md | 84 ++++++++++++++++++++++++++++++++----- 4 files changed, 168 insertions(+), 10 deletions(-) diff --git a/convertor/convertor.go b/convertor/convertor.go index 63f37fe..9e7baf5 100644 --- a/convertor/convertor.go +++ b/convertor/convertor.go @@ -7,6 +7,7 @@ package convertor import ( "bytes" "encoding/binary" + "encoding/gob" "encoding/json" "fmt" "math" @@ -243,3 +244,21 @@ func ToChannel(array []interface{}) <-chan interface{} { return ch } + +// EncodeByte encode data to byte +func EncodeByte(data interface{}) ([]byte, error) { + buffer := bytes.NewBuffer(nil) + encoder := gob.NewEncoder(buffer) + err := encoder.Encode(data) + if err != nil { + return nil, err + } + return buffer.Bytes(), nil +} + +// DecodeByte decode byte data to target object +func DecodeByte(data []byte, target interface{}) error { + buffer := bytes.NewBuffer(data) + decoder := gob.NewDecoder(buffer) + return decoder.Decode(target) +} diff --git a/convertor/convertor_test.go b/convertor/convertor_test.go index a028309..5b1d925 100644 --- a/convertor/convertor_test.go +++ b/convertor/convertor_test.go @@ -195,3 +195,21 @@ func TestToChannel(t *testing.T) { _, ok := <-ch assert.Equal(false, ok) } + +func TestEncodeByte(t *testing.T) { + assert := internal.NewAssert(t, "TestEncodeByte") + + byteData, _ := EncodeByte("abc") + expected := []byte{6, 12, 0, 3, 97, 98, 99} + + assert.Equal(expected, byteData) +} + +func TestDecodeByte(t *testing.T) { + assert := internal.NewAssert(t, "TestDecodeByte") + + var obj string + byteData := []byte{6, 12, 0, 3, 97, 98, 99} + DecodeByte(byteData, &obj) + assert.Equal("abc", obj) +} diff --git a/docs/convertor.md b/docs/convertor.md index 751c092..5b0279c 100644 --- a/docs/convertor.md +++ b/docs/convertor.md @@ -29,6 +29,8 @@ import ( - [ToJson](#ToJson) - [ToString](#ToString) - [StructToMap](#StructToMap) +- [EncodeByte](#EncodeByte) +- [DecodeByte](#DecodeByte)
@@ -384,4 +386,59 @@ func main() { fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test] } +``` + + +### EncodeByte + +

Encode data to byte slice.

+ +Signature: + +```go +func EncodeByte(data any) ([]byte, error) +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + byteData, _ := convertor.EncodeByte("abc") + fmt.Println(byteData) //[]byte{6, 12, 0, 3, 97, 98, 99} +} +``` + + + +### DecodeByte + +

Decode byte data to target object. target should be a pointer instance.

+ +Signature: + +```go +func DecodeByte(data []byte, target any) error +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + var result string + byteData := []byte{6, 12, 0, 3, 97, 98, 99} + convertor.DecodeByte(byteData, &result) + fmt.Println(result) //"abc" +} ``` \ No newline at end of file diff --git a/docs/convertor_zh-CN.md b/docs/convertor_zh-CN.md index e11ad31..3e4c769 100644 --- a/docs/convertor_zh-CN.md +++ b/docs/convertor_zh-CN.md @@ -21,16 +21,25 @@ import ( ## 目录 -- [ColorHexToRGB](#ColorHexToRGB) -- [ColorRGBToHex](#ColorRGBToHex) -- [ToBool](#ToBool) -- [ToBytes](#ToBytes) -- [ToChar](#ToChar) -- [ToChannel](#ToChannel) -- [ToInt](#ToInt) -- [ToJson](#ToJson) -- [ToString](#ToString) -- [StructToMap](#StructToMap) +- [Convertor](#convertor) + - [源码:](#源码) + - [用法:](#用法) + - [目录](#目录) + - [文档](#文档) + - [ColorHexToRGB](#colorhextorgb) + - [ColorRGBToHex](#colorrgbtohex) + - [ToBool](#tobool) + - [ToBytes](#tobytes) + - [ToChar](#tochar) + - [ToChannel](#tochannel) + - [ToFloat](#tofloat) + - [ToInt](#toint) + - [ToJson](#tojson) + - [ToString](#tostring) + - [StructToMap](#structtomap) + - [EncodeByte](#encodebyte) + - [DecodeByte](#decodebyte) +
@@ -387,4 +396,59 @@ func main() { fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test] } +``` + + +### EncodeByte + +

将data编码成字节切片

+ +函数签名: + +```go +func EncodeByte(data any) ([]byte, error) +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + byteData, _ := convertor.EncodeByte("abc") + fmt.Println(byteData) //[]byte{6, 12, 0, 3, 97, 98, 99} +} +``` + + + +### DecodeByte + +

解码字节切片到目标对象,目标对象需要传入一个指针实例子

+ +函数签名: + +```go +func DecodeByte(data []byte, target any) error +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + var result string + byteData := []byte{6, 12, 0, 3, 97, 98, 99} + convertor.DecodeByte(byteData, &result) + fmt.Println(result) //"abc" +} ``` \ No newline at end of file