1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-19 04:02:27 +08:00

Compare commits

...

5 Commits

Author SHA1 Message Date
dudaodong
cc1bacff74 doc: add document for EncodeByte and DecodeByte 2022-08-26 10:54:51 +08:00
dudaodong
f49f75b371 test: add unit test for EncodeByte and DecodeByte 2022-08-26 10:47:25 +08:00
dudaodong
b30f4a7bab feat: add DecodeByte 2022-08-26 10:35:54 +08:00
dudaodong
982cb8932b feat: add EncodeByte 2022-08-26 10:34:42 +08:00
dudaodong
9107eb4b32 fix: fix resize bug in HashMap 2022-08-25 14:04:26 +08:00
6 changed files with 166 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ package convertor
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"encoding/gob"
"encoding/json" "encoding/json"
"fmt" "fmt"
"math" "math"
@@ -270,3 +271,21 @@ func ColorRGBToHex(red, green, blue int) string {
return "#" + r + g + b return "#" + r + g + b
} }
// EncodeByte encode data to byte
func EncodeByte(data any) ([]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 any) error {
buffer := bytes.NewBuffer(data)
decoder := gob.NewDecoder(buffer)
return decoder.Decode(target)
}

View File

@@ -238,3 +238,21 @@ func TestToPointer(t *testing.T) {
assert.Equal(*result, 123) assert.Equal(*result, 123)
} }
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)
}

View File

@@ -98,6 +98,8 @@ func (hm *HashMap) resize() {
tempTable := hm.table tempTable := hm.table
hm.table = make([]*mapNode, hm.capacity)
for i := 0; i < len(tempTable); i++ { for i := 0; i < len(tempTable); i++ {
node := tempTable[i] node := tempTable[i]
if node == nil { if node == nil {

View File

@@ -19,6 +19,18 @@ func TestHashMap_PutAndGet(t *testing.T) {
assert.Equal(4, hm.Get("abc")) assert.Equal(4, hm.Get("abc"))
} }
func TestHashMap_Resize(t *testing.T) {
assert := internal.NewAssert(t, "TestHashMap_Resize")
hm := NewHashMapWithCapacity(3, 3)
for i := 0; i < 20; i++ {
hm.Put(i, 10)
}
assert.Equal(10, hm.Get(5))
}
func TestHashMap_Delete(t *testing.T) { func TestHashMap_Delete(t *testing.T) {
assert := internal.NewAssert(t, "TestHashMap_Delete") assert := internal.NewAssert(t, "TestHashMap_Delete")

View File

@@ -34,6 +34,8 @@ import (
- [ToString](#ToString) - [ToString](#ToString)
- [StructToMap](#StructToMap) - [StructToMap](#StructToMap)
- [MapToSlice](#MapToSlice) - [MapToSlice](#MapToSlice)
- [EncodeByte](#EncodeByte)
- [DecodeByte](#DecodeByte)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -457,4 +459,59 @@ func main() {
fmt.Println(result) //[]string{"a:1", "b:2", "c:3"} fmt.Println(result) //[]string{"a:1", "b:2", "c:3"}
} }
```
### <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/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>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/v2/convertor"
)
func main() {
var result string
byteData := []byte{6, 12, 0, 3, 97, 98, 99}
convertor.DecodeByte(byteData, &result)
fmt.Println(result) //"abc"
}
``` ```

View File

@@ -36,6 +36,8 @@ import (
- [ToString](#ToString) - [ToString](#ToString)
- [StructToMap](#StructToMap) - [StructToMap](#StructToMap)
- [MapToSlice](#MapToSlice) - [MapToSlice](#MapToSlice)
- [EncodeByte](#EncodeByte)
- [DecodeByte](#DecodeByte)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -489,3 +491,59 @@ func main() {
fmt.Println(result) //[]string{"a:1", "b:2", "c:3"} fmt.Println(result) //[]string{"a:1", "b:2", "c:3"}
} }
``` ```
### <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"
}
```