diff --git a/README.md b/README.md index 7baf088..d7b50a3 100644 --- a/README.md +++ b/README.md @@ -246,6 +246,8 @@ import "github.com/duke-git/lancet/v2/convertor" - **DecodeByte** : decode byte slice data to target object. [[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#DecodeByte)] [[play](https://go.dev/play/p/zI6xsmuQRbn)] +- **DeepClone** : creates a deep copy of passed item, can't clone unexported field of struct. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#DeepClone)] ### 5. Cryptor package is for data encryption and decryption. diff --git a/README_zh-CN.md b/README_zh-CN.md index 34371fc..e1303c0 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -245,6 +245,9 @@ import "github.com/duke-git/lancet/v2/convertor" - **DecodeByte** : 解码字节切片到目标对象,目标对象需要传入一个指针实例。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#DecodeByte)] [[play](https://go.dev/play/p/zI6xsmuQRbn)] +- **DeepClone** : 创建一个传入值的深拷贝, 无法克隆结构体的非导出字段。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#DeepClone)] + ### 5. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。 diff --git a/docs/convertor.md b/docs/convertor.md index b894901..0d5ff36 100644 --- a/docs/convertor.md +++ b/docs/convertor.md @@ -1,15 +1,17 @@ # Convertor + Package convertor contains some functions for data type convertion.
## Source: -- [https://github.com/duke-git/lancet/blob/main/convertor/convertor.go](https://github.com/duke-git/lancet/blob/main/convertor/convertor.go) +- [https://github.com/duke-git/lancet/blob/main/convertor/convertor.go](https://github.com/duke-git/lancet/blob/main/convertor/convertor.go)
## Usage: + ```go import ( "github.com/duke-git/lancet/v2/convertor" @@ -19,28 +21,31 @@ import (
## Index -- [ColorHexToRGB](#ColorHexToRGB) -- [ColorRGBToHex](#ColorRGBToHex) -- [ToBool](#ToBool) -- [ToBytes](#ToBytes) -- [ToChar](#ToChar) -- [ToChannel](#ToChannel) -- [ToFloat](#ToFloat) -- [ToInt](#ToInt) -- [ToJson](#ToJson) -- [ToMap](#ToMap) -- [ToPointer](#ToPointer) -- [ToString](#ToString) -- [StructToMap](#StructToMap) -- [MapToSlice](#MapToSlice) -- [EncodeByte](#EncodeByte) -- [DecodeByte](#DecodeByte) + +- [ColorHexToRGB](#ColorHexToRGB) +- [ColorRGBToHex](#ColorRGBToHex) +- [ToBool](#ToBool) +- [ToBytes](#ToBytes) +- [ToChar](#ToChar) +- [ToChannel](#ToChannel) +- [ToFloat](#ToFloat) +- [ToInt](#ToInt) +- [ToJson](#ToJson) +- [ToMap](#ToMap) +- [ToPointer](#ToPointer) +- [ToString](#ToString) +- [StructToMap](#StructToMap) +- [MapToSlice](#MapToSlice) +- [EncodeByte](#EncodeByte) +- [DecodeByte](#DecodeByte) +- [DeepClone](#DeepClone)
## Documentation ### ColorHexToRGB +

Convert color hex to color rgb.

Signature: @@ -48,6 +53,7 @@ import ( ```go func ColorHexToRGB(colorHex string) (red, green, blue int) ``` + Example: ```go @@ -78,6 +84,7 @@ func main() { ```go func ColorRGBToHex(red, green, blue int) string ``` + Example: ```go @@ -110,6 +117,7 @@ func main() { ```go func ToBool(s string) (bool, error) ``` + Example: ```go @@ -150,6 +158,7 @@ func main() { ```go func ToBytes(data any) ([]byte, error) ``` + Example: ```go @@ -182,6 +191,7 @@ func main() { ```go func ToChar(s string) []string ``` + Example: ```go @@ -217,6 +227,7 @@ func main() { ```go func ToChannel[T any](array []T) <-chan T ``` + Example: ```go @@ -253,6 +264,7 @@ func main() { ```go func ToFloat(value any) (float64, error) ``` + Example: ```go @@ -297,6 +309,7 @@ func main() { ```go func ToInt(value any) (int64, error) ``` + Example: ```go @@ -338,6 +351,7 @@ func main() { ```go func ToJson(value any) (string, error) ``` + Example: ```go @@ -372,6 +386,7 @@ func main() { ```go func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V ``` + Example: ```go @@ -412,6 +427,7 @@ func main() { ```go func ToPointer[T any](value T) *T ``` + Example: ```go @@ -424,8 +440,8 @@ import ( func main() { result := convertor.ToPointer(123) - fmt.Println(*result) - + fmt.Println(*result) + // Output: // 123 } @@ -440,6 +456,7 @@ func main() { ```go func ToString(value any) string ``` + Example: ```go @@ -487,6 +504,7 @@ func main() { ```go func StructToMap(value any) (map[string]any, error) ``` + Example: ```go @@ -524,6 +542,7 @@ func main() { ```go func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T ``` + Example: ```go @@ -544,7 +563,6 @@ func main() { } ``` - ### EncodeByte

Encode data to byte slice.

@@ -554,6 +572,7 @@ func main() { ```go func EncodeByte(data any) ([]byte, error) ``` + Example: ```go @@ -582,6 +601,7 @@ func main() { ```go func DecodeByte(data []byte, target any) error ``` + Example: ```go @@ -595,15 +615,80 @@ import ( func main() { var result string byteData := []byte{6, 12, 0, 3, 97, 98, 99} - + err := convertor.DecodeByte(byteData, &result) if err != nil { return } fmt.Println(result) - + // Output: // abc } +``` + + +### DeepClone + +

Creates a deep copy of passed item, can't clone unexported field of struct.

+ +Signature: + +```go +func DeepClone[T any](src T) T +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + type Struct struct { + Str string + Int int + Float float64 + Bool bool + Nil interface{} + unexported string + } + + cases := []interface{}{ + true, + 1, + 0.1, + map[string]int{ + "a": 1, + "b": 2, + }, + &Struct{ + Str: "test", + Int: 1, + Float: 0.1, + Bool: true, + Nil: nil, + // unexported: "can't be cloned", + }, + } + + for _, item := range cases { + cloned := convertor.DeepClone(item) + + isPointerEqual := &cloned == &item + fmt.Println(cloned, isPointerEqual) + } + + // Output: + // true false + // 1 false + // 0.1 false + // map[a:1 b:2] false + // &{test 1 0.1 true } false +} ``` \ No newline at end of file diff --git a/docs/convertor_zh-CN.md b/docs/convertor_zh-CN.md index 4ff38b7..356db7c 100644 --- a/docs/convertor_zh-CN.md +++ b/docs/convertor_zh-CN.md @@ -1,11 +1,12 @@ # Convertor -convertor转换器包支持一些常见的数据类型转换 + +convertor 转换器包支持一些常见的数据类型转换
## 源码: -- [https://github.com/duke-git/lancet/blob/main/convertor/convertor.go](https://github.com/duke-git/lancet/blob/main/convertor/convertor.go) +- [https://github.com/duke-git/lancet/blob/main/convertor/convertor.go](https://github.com/duke-git/lancet/blob/main/convertor/convertor.go)
@@ -21,29 +22,30 @@ import ( ## 目录 -- [ColorHexToRGB](#ColorHexToRGB) -- [ColorRGBToHex](#ColorRGBToHex) -- [ToBool](#ToBool) -- [ToBytes](#ToBytes) -- [ToChar](#ToChar) -- [ToChannel](#ToChannel) -- [ToFloat](#ToFloat) -- [ToInt](#ToInt) -- [ToJson](#ToJson) -- [ToMap](#ToMap) -- [ToPointer](#ToPointer) -- [ToString](#ToString) -- [StructToMap](#StructToMap) -- [MapToSlice](#MapToSlice) -- [EncodeByte](#EncodeByte) -- [DecodeByte](#DecodeByte) +- [ColorHexToRGB](#ColorHexToRGB) +- [ColorRGBToHex](#ColorRGBToHex) +- [ToBool](#ToBool) +- [ToBytes](#ToBytes) +- [ToChar](#ToChar) +- [ToChannel](#ToChannel) +- [ToFloat](#ToFloat) +- [ToInt](#ToInt) +- [ToJson](#ToJson) +- [ToMap](#ToMap) +- [ToPointer](#ToPointer) +- [ToString](#ToString) +- [StructToMap](#StructToMap) +- [MapToSlice](#MapToSlice) +- [EncodeByte](#EncodeByte) +- [DecodeByte](#DecodeByte) +- [DeepClone](#DeepClone)
## 文档 - ### ColorHexToRGB +

颜色值十六进制转rgb。

函数签名: @@ -51,6 +53,7 @@ import ( ```go func ColorHexToRGB(colorHex string) (red, green, blue int) ``` + 示例: ```go @@ -81,6 +84,7 @@ func main() { ```go func ColorRGBToHex(red, green, blue int) string ``` + 示例: ```go @@ -113,6 +117,7 @@ func main() { ```go func ToBool(s string) (bool, error) ``` + 示例: ```go @@ -153,6 +158,7 @@ func main() { ```go func ToBytes(data any) ([]byte, error) ``` + 示例: ```go @@ -185,6 +191,7 @@ func main() { ```go func ToChar(s string) []string ``` + 示例: ```go @@ -220,6 +227,7 @@ func main() { ```go func ToChannel[T any](array []T) <-chan T ``` + 示例: ```go @@ -256,6 +264,7 @@ func main() { ```go func ToFloat(value any) (float64, error) ``` + 示例: ```go @@ -300,6 +309,7 @@ func main() { ```go func ToInt(value any) (int64, error) ``` + 示例: ```go @@ -341,6 +351,7 @@ func main() { ```go func ToJson(value any) (string, error) ``` + 示例: ```go @@ -375,6 +386,7 @@ func main() { ```go func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V ``` + 示例: ```go @@ -415,6 +427,7 @@ func main() { ```go func ToPointer[T any](value T) *T ``` + 示例: ```go @@ -427,8 +440,8 @@ import ( func main() { result := convertor.ToPointer(123) - fmt.Println(*result) - + fmt.Println(*result) + // Output: // 123 } @@ -443,6 +456,7 @@ func main() { ```go func ToString(value any) string ``` + 示例: ```go @@ -490,6 +504,7 @@ func main() { ```go func StructToMap(value any) (map[string]any, error) ``` + 示例: ```go @@ -527,6 +542,7 @@ func main() { ```go func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T ``` + 示例: ```go @@ -556,6 +572,7 @@ func main() { ```go func EncodeByte(data any) ([]byte, error) ``` + 示例: ```go @@ -584,6 +601,7 @@ func main() { ```go func DecodeByte(data []byte, target any) error ``` + 示例: ```go @@ -597,15 +615,79 @@ import ( func main() { var result string byteData := []byte{6, 12, 0, 3, 97, 98, 99} - + err := convertor.DecodeByte(byteData, &result) if err != nil { return } fmt.Println(result) - + // Output: // abc } +``` + +### DeepClone + +

创建一个传入值的深拷贝, 无法克隆结构体的非导出字段。

+ +函数签名: + +```go +func DeepClone[T any](src T) T +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + type Struct struct { + Str string + Int int + Float float64 + Bool bool + Nil interface{} + unexported string + } + + cases := []interface{}{ + true, + 1, + 0.1, + map[string]int{ + "a": 1, + "b": 2, + }, + &Struct{ + Str: "test", + Int: 1, + Float: 0.1, + Bool: true, + Nil: nil, + // unexported: "can't be cloned", + }, + } + + for _, item := range cases { + cloned := convertor.DeepClone(item) + + isPointerEqual := &cloned == &item + fmt.Println(cloned, isPointerEqual) + } + + // Output: + // true false + // 1 false + // 0.1 false + // map[a:1 b:2] false + // &{test 1 0.1 true } false +} ``` \ No newline at end of file