mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-11 00:02:28 +08:00
feat: add EncodeByte and DecodeByte
This commit is contained in:
@@ -7,6 +7,7 @@ package convertor
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"encoding/gob"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
@@ -243,3 +244,21 @@ func ToChannel(array []interface{}) <-chan interface{} {
|
|||||||
|
|
||||||
return ch
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -195,3 +195,21 @@ func TestToChannel(t *testing.T) {
|
|||||||
_, ok := <-ch
|
_, ok := <-ch
|
||||||
assert.Equal(false, ok)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ import (
|
|||||||
- [ToJson](#ToJson)
|
- [ToJson](#ToJson)
|
||||||
- [ToString](#ToString)
|
- [ToString](#ToString)
|
||||||
- [StructToMap](#StructToMap)
|
- [StructToMap](#StructToMap)
|
||||||
|
- [EncodeByte](#EncodeByte)
|
||||||
|
- [DecodeByte](#DecodeByte)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -384,4 +386,59 @@ func main() {
|
|||||||
|
|
||||||
fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test]
|
fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test]
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EncodeByte">EncodeByte</span>
|
||||||
|
|
||||||
|
<p>Encode data to byte slice.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EncodeByte(data any) ([]byte, error)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```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}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="DecodeByte">DecodeByte</span>
|
||||||
|
|
||||||
|
<p>Decode byte data to target object. target should be a pointer instance.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DecodeByte(data []byte, target any) error
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```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"
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -21,16 +21,25 @@ import (
|
|||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [ColorHexToRGB](#ColorHexToRGB)
|
- [Convertor](#convertor)
|
||||||
- [ColorRGBToHex](#ColorRGBToHex)
|
- [源码:](#源码)
|
||||||
- [ToBool](#ToBool)
|
- [用法:](#用法)
|
||||||
- [ToBytes](#ToBytes)
|
- [目录](#目录)
|
||||||
- [ToChar](#ToChar)
|
- [文档](#文档)
|
||||||
- [ToChannel](#ToChannel)
|
- [<span id="ColorHexToRGB">ColorHexToRGB</span>](#colorhextorgb)
|
||||||
- [ToInt](#ToInt)
|
- [<span id="ColorRGBToHex">ColorRGBToHex</span>](#colorrgbtohex)
|
||||||
- [ToJson](#ToJson)
|
- [<span id="ToBool">ToBool</span>](#tobool)
|
||||||
- [ToString](#ToString)
|
- [<span id="ToBytes">ToBytes</span>](#tobytes)
|
||||||
- [StructToMap](#StructToMap)
|
- [<span id="ToChar">ToChar</span>](#tochar)
|
||||||
|
- [<span id="ToChannel">ToChannel</span>](#tochannel)
|
||||||
|
- [<span id="ToFloat">ToFloat</span>](#tofloat)
|
||||||
|
- [<span id="ToInt">ToInt</span>](#toint)
|
||||||
|
- [<span id="ToJson">ToJson</span>](#tojson)
|
||||||
|
- [<span id="ToString">ToString</span>](#tostring)
|
||||||
|
- [<span id="StructToMap">StructToMap</span>](#structtomap)
|
||||||
|
- [<span id="EncodeByte">EncodeByte</span>](#encodebyte)
|
||||||
|
- [<span id="DecodeByte">DecodeByte</span>](#decodebyte)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -387,4 +396,59 @@ func main() {
|
|||||||
|
|
||||||
fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test]
|
fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test]
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EncodeByte">EncodeByte</span>
|
||||||
|
|
||||||
|
<p>将data编码成字节切片</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EncodeByte(data any) ([]byte, error)
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```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}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="DecodeByte">DecodeByte</span>
|
||||||
|
|
||||||
|
<p>解码字节切片到目标对象,目标对象需要传入一个指针实例子</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DecodeByte(data []byte, target any) error
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```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"
|
||||||
|
}
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user