升级到v3版本 (#94)

This commit is contained in:
libin
2025-07-08 15:26:54 +08:00
committed by GitHub
parent 20172967bd
commit e1378e8d56
197 changed files with 934 additions and 940 deletions
+20 -20
View File
@@ -16,7 +16,7 @@
## 快速开始
**安装**
```bash
go get -u github.com/jefferyjob/go-easy-utils/v2
go get -u github.com/jefferyjob/go-easy-utils/v3
```
**使用Demo**
@@ -25,33 +25,33 @@ package main
import (
"fmt"
"github.com/jefferyjob/go-easy-utils/v2/sliceUtil"
"github.com/jefferyjob/go-easy-utils/v3/slicex"
)
func main() {
var slice = []string{"this", "is", "go", "easy", "utils"}
chunkSlice := sliceUtil.ChunkSlice(slice, 2)
fmt.Printf("%v", chunkSlice)
var s = []string{"this", "is", "go", "easy", "utils"}
c := slicex.Chunk(s, 2)
fmt.Printf("%v", c)
}
```
## 功能列表
| 包名 | 函数概要 | 文档 |
|--------------| ----------------------------------------------------------------------------------------- |----------------------|
| anyUtil | 将任意类型的数据转换为指定类型 | [README](anyUtil) |
| byteUtil | 字节数组转换 | [README](byteUtil) |
| cryptoUtil | 各种加密处理 | [README](cryptoUtil) |
| emojiUtil | 表情符号的解码和编码 | [README](emojiUtil) |
| floatUtil | 浮点数数据处理 | [README](floatUtil) |
| intUtil | 数值数据处理 | [README](intUtil) |
| jsonUtil | JSON 数据转换,支持弱类型转换 | [README](jsonUtil) |
| mapUtil | Map 类型数据处理 | [README](mapUtil) |
| mathUtil | 数学函数可以处理整数和浮点数范围内的值 | [README](mathUtil) |
| randUtil | 随机数生成,包括:数字、字符串、字节数组 | [README](randUtil) |
| sliceUtil | 切片处理(分组、求和、转换、合并等) | [README](sliceUtil) |
| strUtil | 字符串转换处理 | [README](strUtil) |
| validUtil | 常见数据验证,如:中文、英文、姓名、身份证号、电话号码、电子邮件 | [README](validUtil) |
| 包名 | 函数概要 | 文档 |
|---------| ----------------------------------------------------------------------------------------- |----------------------|
| anyx | 将任意类型的数据转换为指定类型 | [README](anyx) |
| bytex | 字节数组转换 | [README](bytex) |
| cryptox | 各种加密处理 | [README](cryptox) |
| emojix | 表情符号的解码和编码 | [README](emojix) |
| floatx | 浮点数数据处理 | [README](floatx) |
| intx | 数值数据处理 | [README](intUtil) |
| jsonx | JSON 数据转换,支持弱类型转换 | [README](jsonx) |
| mapx | Map 类型数据处理 | [README](mapx) |
| mathx | 数学函数可以处理整数和浮点数范围内的值 | [README](mathx) |
| randx | 随机数生成,包括:数字、字符串、字节数组 | [README](randUtil) |
| slicex | 切片处理(分组、求和、转换、合并等) | [README](slicex) |
| strx | 字符串转换处理 | [README](strx) |
| validx | 常见数据验证,如:中文、英文、姓名、身份证号、电话号码、电子邮件 | [README](validx) |
## 许可证
本库采用 Apache-2.0 进行授权。有关详细信息,请参阅 LICENSE 文件。
+18 -24
View File
@@ -16,7 +16,7 @@ This is a general data type processing tool class based on Go language, which he
## Quick Start
**Install**
```bash
go get -u github.com/jefferyjob/go-easy-utils/v2
go get -u github.com/jefferyjob/go-easy-utils/v3
```
**Use Demo**
@@ -25,13 +25,13 @@ package main
import (
"fmt"
"github.com/jefferyjob/go-easy-utils/v2/sliceUtil"
"github.com/jefferyjob/go-easy-utils/v3/slicex"
)
func main() {
var slice = []string{"this", "is", "go", "easy", "utils"}
chunkSlice := sliceUtil.ChunkSlice(slice, 2)
fmt.Printf("%v", chunkSlice)
var s = []string{"this", "is", "go", "easy", "utils"}
c := slicex.Chunk(s, 2)
fmt.Printf("%v", c)
}
```
@@ -39,26 +39,20 @@ func main() {
| Package name | Function Outline | Document |
|--------------| ----------------------------------------------------------------------------------------- |----------------------|
| anyUtil | Convert any type of data to the specified type | [README](anyUtil) |
| byteUtil | Conversion of byte array | [README](byteUtil) |
| cryptoUtil | Various encryption processing | [README](cryptoUtil) |
| emojiUtil | Decoding and encoding of emoji expression | [README](emojiUtil) |
| floatUtil | Floating-point data processing | [README](floatUtil) |
| intUtil | Numerical data processing | [README](intUtil) |
| jsonUtil | Json data conversion, support weak type conversion | [README](jsonUtil) |
| mapUtil | Map type data processing | [README](mapUtil) |
| mathUtil | The Math function can handle values within the range of integers and floats. | [README](mathUtil) |
| randUtil | Random number generation, including: number, string, byte array | [README](randUtil) |
| sliceUtil | Slice processing (grouping, summation, transformation, merging, etc.) | [README](sliceUtil) |
| strUtil | String conversion processing | [README](strUtil) |
| validUtil | Common data verification, such as: Chinese, English, name, ID number, phone number, email | [README](validUtil) |
| anyx | Convert any type of data to the specified type | [README](anyx) |
| bytex | Conversion of byte array | [README](bytex) |
| cryptox | Various encryption processing | [README](cryptox) |
| emojix | Decoding and encoding of emoji expression | [README](emojix) |
| floatx | Floating-point data processing | [README](floatx) |
| intx | Numerical data processing | [README](intUtil) |
| jsonx | Json data conversion, support weak type conversion | [README](jsonx) |
| mapx | Map type data processing | [README](mapx) |
| mathx | The Math function can handle values within the range of integers and floats. | [README](mathx) |
| randx | Random number generation, including: number, string, byte array | [README](randUtil) |
| slicex | Slice processing (grouping, summation, transformation, merging, etc.) | [README](slicex) |
| strx | String conversion processing | [README](strx) |
| validx | Common data verification, such as: Chinese, English, name, ID number, phone number, email | [README](validx) |
## Sponsorship and support
`go-easy-utils` Thank JetBrains for their support
<a href="https://www.jetbrains.com"><img src="https://raw.githubusercontent.com/panjf2000/illustrations/master/jetbrains/jetbrains-variant-4.png" height="100" alt="JetBrains"/></a>
## License
This library is licensed under the Apache-2.0. See the LICENSE file for details.
-61
View File
@@ -1,61 +0,0 @@
# anyUtil
## Install
```bash
go get -u github.com/jefferyjob/go-easy-utils/v2/anyUtil
```
## Import
```go
import (
"github.com/jefferyjob/go-easy-utils/v2/anyUtil"
)
```
## Functions
```go
// AnyToFloat32 将给定的值转换为float32
func AnyToFloat32(i any) (float32, error)
// AnyToFloat64 将给定的值转换为float64
func AnyToFloat64(i any) (float64, error)
// AnyToInt 将给定的值转换为 int
func AnyToInt(i any) (int, error)
// AnyToInt8 将给定的值转换为 int8
func AnyToInt8(i any) (int8, error)
// AnyToInt16 将给定的值转换为 int16
func AnyToInt16(i any) (int16, error)
// AnyToInt32 将给定的值转换为 int32
func AnyToInt32(i any) (int32, error)
// AnyToInt64 将给定的值转换为 int64
func AnyToInt64(i any) (int64, error)
// AnyToStr 任意类型数据转string
func AnyToStr(i any) string
// AnyToUint 将给定的值转换为 uint
func AnyToUint(i any) (uint, error)
// AnyToUint8 将给定的值转换为 uint8
func AnyToUint8(i any) (uint8, error)
// AnyToUint16 将给定的值转换为 uint16
func AnyToUint16(i any) (uint16, error)
// AnyToUint32 将给定的值转换为 uint32
func AnyToUint32(i any) (uint32, error)
// AnyToUint64 将给定的值转换为 uint64
func AnyToUint64(i any) (uint64, error)
// AnyToBool 将给定的值转换为bool
func AnyToBool(i any) bool
```
+61
View File
@@ -0,0 +1,61 @@
# anyx
## Install
```bash
go get -u github.com/jefferyjob/go-easy-utils/v3/anyx
```
## Import
```go
import (
"github.com/jefferyjob/go-easy-utils/v3/anyx"
)
```
## Functions
```go
// ToFloat32 将给定的值转换为float32
func ToFloat32(i any) (float32, error)
// ToFloat64 将给定的值转换为float64
func ToFloat64(i any) (float64, error)
// ToInt 将给定的值转换为 int
func ToInt(i any) (int, error)
// ToInt8 将给定的值转换为 int8
func ToInt8(i any) (int8, error)
// ToInt16 将给定的值转换为 int16
func ToInt16(i any) (int16, error)
// ToInt32 将给定的值转换为 int32
func ToInt32(i any) (int32, error)
// ToInt64 将给定的值转换为 int64
func ToInt64(i any) (int64, error)
// ToStr 任意类型数据转string
func ToStr(i any) string
// ToUint 将给定的值转换为 uint
func ToUint(i any) (uint, error)
// ToUint8 将给定的值转换为 uint8
func ToUint8(i any) (uint8, error)
// ToUint16 将给定的值转换为 uint16
func ToUint16(i any) (uint16, error)
// ToUint32 将给定的值转换为 uint32
func ToUint32(i any) (uint32, error)
// ToUint64 将给定的值转换为 uint64
func ToUint64(i any) (uint64, error)
// ToBool 将给定的值转换为bool
func ToBool(i any) bool
```
+3 -3
View File
@@ -1,9 +1,9 @@
package anyUtil
package anyx
import "reflect"
// AnyToBool 将给定的值转换为bool
func AnyToBool(i any) bool {
// ToBool 将给定的值转换为bool
func ToBool(i any) bool {
if i == nil {
return false
}
@@ -1,12 +1,12 @@
package anyUtil
package anyx
import "fmt"
func ExampleAnyToBool() {
func ExampleToBool() {
a1 := ""
a2 := "hello"
res1 := AnyToBool(a1)
res2 := AnyToBool(a2)
res1 := ToBool(a1)
res2 := ToBool(a2)
fmt.Println(res1)
fmt.Println(res2)
@@ -1,4 +1,4 @@
package anyUtil
package anyx
import (
"testing"
@@ -52,13 +52,13 @@ func TestToBool(t *testing.T) {
{[]int{}, false},
}
for _, test := range tests {
if got := AnyToBool(test.input); got != test.want {
if got := ToBool(test.input); got != test.want {
t.Errorf("toBool(%v) = %v; want %v", test.input, got, test.want)
}
}
}
func TestAnyToBool(t *testing.T) {
func TestToBool2(t *testing.T) {
iPtr := 90
testCases := []struct {
name string
@@ -109,8 +109,8 @@ func TestAnyToBool(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if got := AnyToBool(tc.input); got != tc.want {
t.Errorf("AnyToBool(%v) = %v; want %v", tc.input, got, tc.want)
if got := ToBool(tc.input); got != tc.want {
t.Errorf("ToBool(%v) = %v; want %v", tc.input, got, tc.want)
}
})
}
@@ -1,4 +1,4 @@
package anyUtil
package anyx
import (
"math"
@@ -6,9 +6,9 @@ import (
"strconv"
)
// AnyToFloat32 将给定的值转换为float32
func AnyToFloat32(i any) (float32, error) {
f64, err := AnyToFloat64(i)
// ToFloat32 将给定的值转换为float32
func ToFloat32(i any) (float32, error) {
f64, err := ToFloat64(i)
if err != nil {
return 0, err
}
@@ -18,8 +18,8 @@ func AnyToFloat32(i any) (float32, error) {
return float32(f64), nil
}
// AnyToFloat64 将给定的值转换为float64
func AnyToFloat64(i any) (float64, error) {
// ToFloat64 将给定的值转换为float64
func ToFloat64(i any) (float64, error) {
if i == nil {
return 0, nil
}
@@ -1,10 +1,10 @@
package anyUtil
package anyx
import "fmt"
func ExampleAnyToFloat32() {
func ExampleToFloat32() {
f := "3"
res, _ := AnyToFloat32(f)
res, _ := ToFloat32(f)
fmt.Printf("%T\n", res)
@@ -12,9 +12,9 @@ func ExampleAnyToFloat32() {
// float32
}
func ExampleAnyToFloat64() {
func ExampleToFloat64() {
f := "5"
res, _ := AnyToFloat64(f)
res, _ := ToFloat64(f)
fmt.Printf("%T\n", res)
@@ -1,4 +1,4 @@
package anyUtil
package anyx
import (
"errors"
@@ -6,7 +6,7 @@ import (
"testing"
)
func TestAnyToFloat32(t *testing.T) {
func TestToFloat32(t *testing.T) {
testCases := []struct {
name string
input any
@@ -33,24 +33,24 @@ func TestAnyToFloat32(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
got, err := AnyToFloat32(test.input)
got, err := ToFloat32(test.input)
if got != test.want {
t.Errorf("AnyToFloat32(%v) = %v; want %v", test.input, got, test.want)
t.Errorf("ToFloat32(%v) = %v; want %v", test.input, got, test.want)
}
if !errors.Is(err, test.err) {
if err != nil && test.err != nil {
if err.Error() != test.err.Error() {
t.Errorf("AnyToFloat32(%v) error = %v; want %v", test.input, err, test.err)
t.Errorf("ToFloat32(%v) error = %v; want %v", test.input, err, test.err)
}
} else {
t.Errorf("AnyToFloat32(%v) error = %v; want %v", test.input, err, test.err)
t.Errorf("ToFloat32(%v) error = %v; want %v", test.input, err, test.err)
}
}
})
}
}
func TestAnyToFloat64(t *testing.T) {
func TestToFloat64(t *testing.T) {
iPtr := 90
tests := []struct {
@@ -84,17 +84,17 @@ func TestAnyToFloat64(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := AnyToFloat64(test.input)
got, err := ToFloat64(test.input)
if got != test.want {
t.Errorf("AnyToFloat64(%v) = %v; want %v", test.input, got, test.want)
t.Errorf("ToFloat64(%v) = %v; want %v", test.input, got, test.want)
}
if err != test.err {
if err != nil && test.err != nil {
if err.Error() != test.err.Error() {
t.Errorf("AnyToFloat64(%v) error = %v; want %v", test.input, err, test.err)
t.Errorf("ToFloat64(%v) error = %v; want %v", test.input, err, test.err)
}
} else {
t.Errorf("AnyToFloat64(%v) error = %v; want %v", test.input, err, test.err)
t.Errorf("ToFloat64(%v) error = %v; want %v", test.input, err, test.err)
}
}
})
+17 -17
View File
@@ -1,4 +1,4 @@
package anyUtil
package anyx
import (
"math"
@@ -6,24 +6,24 @@ import (
"strconv"
)
// AnyToInt 将给定的值转换为 int
func AnyToInt(i any) (int, error) {
v, err := AnyToInt64(i)
// ToInt 将给定的值转换为 int
func ToInt(i any) (int, error) {
v, err := ToInt64(i)
if err != nil {
return 0, err
}
// int 兼容32位和64位系统
//if int64(int(v)) != v {
// if int64(int(v)) != v {
// return 0, ErrValOut
//}
// }
return int(v), nil
}
// AnyToInt8 将给定的值转换为 int8
func AnyToInt8(i any) (int8, error) {
value, err := AnyToInt64(i)
// ToInt8 将给定的值转换为 int8
func ToInt8(i any) (int8, error) {
value, err := ToInt64(i)
if err != nil {
return 0, err
}
@@ -33,9 +33,9 @@ func AnyToInt8(i any) (int8, error) {
return int8(value), nil
}
// AnyToInt16 将给定的值转换为 int16
func AnyToInt16(i any) (int16, error) {
value, err := AnyToInt64(i)
// ToInt16 将给定的值转换为 int16
func ToInt16(i any) (int16, error) {
value, err := ToInt64(i)
if err != nil {
return 0, err
}
@@ -45,9 +45,9 @@ func AnyToInt16(i any) (int16, error) {
return int16(value), nil
}
// AnyToInt32 将给定的值转换为 int32
func AnyToInt32(i any) (int32, error) {
value, err := AnyToInt64(i)
// ToInt32 将给定的值转换为 int32
func ToInt32(i any) (int32, error) {
value, err := ToInt64(i)
if err != nil {
return 0, err
}
@@ -57,8 +57,8 @@ func AnyToInt32(i any) (int32, error) {
return int32(value), nil
}
// AnyToInt64 将给定的值转换为 int64
func AnyToInt64(i any) (int64, error) {
// ToInt64 将给定的值转换为 int64
func ToInt64(i any) (int64, error) {
if i == nil {
return 0, nil
}
@@ -1,12 +1,12 @@
package strUtil
package anyx
import "fmt"
func ExampleStrToInt() {
func ExampleToInt() {
s1 := "100"
s2 := "-100"
res1 := StrToInt(s1)
res2 := StrToInt(s2)
res1, _ := ToInt(s1)
res2, _ := ToInt(s2)
fmt.Printf("%d,%T\n", res1, res1)
fmt.Printf("%d,%T\n", res2, res2)
@@ -16,11 +16,11 @@ func ExampleStrToInt() {
// -100,int
}
func ExampleStrToInt8() {
func ExampleToInt8() {
s1 := "100"
s2 := "-100"
res1 := StrToInt8(s1)
res2 := StrToInt8(s2)
res1, _ := ToInt8(s1)
res2, _ := ToInt8(s2)
fmt.Printf("%d,%T\n", res1, res1)
fmt.Printf("%d,%T\n", res2, res2)
@@ -30,11 +30,11 @@ func ExampleStrToInt8() {
// -100,int8
}
func ExampleStrToInt16() {
func ExampleToInt16() {
s1 := "100"
s2 := "-100"
res1 := StrToInt16(s1)
res2 := StrToInt16(s2)
res1, _ := ToInt16(s1)
res2, _ := ToInt16(s2)
fmt.Printf("%d,%T\n", res1, res1)
fmt.Printf("%d,%T\n", res2, res2)
@@ -44,11 +44,11 @@ func ExampleStrToInt16() {
// -100,int16
}
func ExampleStrToInt32() {
func ExampleToInt32() {
s1 := "100"
s2 := "-100"
res1 := StrToInt32(s1)
res2 := StrToInt32(s2)
res1, _ := ToInt32(s1)
res2, _ := ToInt32(s2)
fmt.Printf("%d,%T\n", res1, res1)
fmt.Printf("%d,%T\n", res2, res2)
@@ -58,11 +58,11 @@ func ExampleStrToInt32() {
// -100,int32
}
func ExampleStrToInt64() {
func ExampleToInt64() {
s1 := "100"
s2 := "-100"
res1 := StrToInt64(s1)
res2 := StrToInt64(s2)
res1, _ := ToInt64(s1)
res2, _ := ToInt64(s2)
fmt.Printf("%d,%T\n", res1, res1)
fmt.Printf("%d,%T\n", res2, res2)
@@ -1,4 +1,4 @@
package anyUtil
package anyx
import (
"errors"
@@ -7,7 +7,7 @@ import (
"testing"
)
func TestAnyToInt(t *testing.T) {
func TestToInt(t *testing.T) {
iPtr := 90
testCases := []struct {
name string
@@ -45,15 +45,15 @@ func TestAnyToInt(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
res, err := AnyToInt(test.input)
res, err := ToInt(test.input)
if res != test.expected || !errors.Is(err, test.err) {
t.Errorf("AnyToInt(%v) = (%d, %v); want (%d, %v)", test.input, res, err, test.expected, test.err)
t.Errorf("ToInt(%v) = (%d, %v); want (%d, %v)", test.input, res, err, test.expected, test.err)
}
})
}
}
func TestAnyToInt8(t *testing.T) {
func TestToInt8(t *testing.T) {
testCases := []struct {
name string
input any
@@ -86,15 +86,15 @@ func TestAnyToInt8(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
res, err := AnyToInt8(test.input)
res, err := ToInt8(test.input)
if res != test.expected || !errors.Is(err, test.err) {
t.Errorf("AnyToInt8(%v) = (%d, %v); want (%d, %v)", test.input, res, err, test.expected, test.err)
t.Errorf("ToInt8(%v) = (%d, %v); want (%d, %v)", test.input, res, err, test.expected, test.err)
}
})
}
}
func TestAnyToInt16(t *testing.T) {
func TestToInt16(t *testing.T) {
testCases := []struct {
name string
input any
@@ -127,15 +127,15 @@ func TestAnyToInt16(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
res, err := AnyToInt16(test.input)
res, err := ToInt16(test.input)
if res != test.expected || !errors.Is(err, test.err) {
t.Errorf("AnyToInt16(%v) = (%d, %v); want (%d, %v)", test.input, res, err, test.expected, test.err)
t.Errorf("ToInt16(%v) = (%d, %v); want (%d, %v)", test.input, res, err, test.expected, test.err)
}
})
}
}
func TestAnyToInt32(t *testing.T) {
func TestToInt32(t *testing.T) {
testCases := []struct {
name string
input any
@@ -168,15 +168,15 @@ func TestAnyToInt32(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
res, err := AnyToInt32(test.input)
res, err := ToInt32(test.input)
if res != test.expected || !errors.Is(err, test.err) {
t.Errorf("AnyToInt32(%v) = (%d, %v); want (%d, %v)", test.input, res, err, test.expected, test.err)
t.Errorf("ToInt32(%v) = (%d, %v); want (%d, %v)", test.input, res, err, test.expected, test.err)
}
})
}
}
func TestAnyToInt64(t *testing.T) {
func TestToInt64(t *testing.T) {
testCases := []struct {
name string
input any
@@ -219,7 +219,7 @@ func TestAnyToInt64(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, err := AnyToInt64(tc.input)
res, err := ToInt64(tc.input)
assert.Equal(t, tc.err, err)
assert.Equal(t, tc.expected, res)
})
+3 -3
View File
@@ -1,4 +1,4 @@
package anyUtil
package anyx
import (
"fmt"
@@ -6,8 +6,8 @@ import (
"strconv"
)
// AnyToStr 任意类型数据转string
func AnyToStr(i any) string {
// ToStr 任意类型数据转string
func ToStr(i any) string {
if i == nil {
return ""
}
@@ -1,10 +1,10 @@
package anyUtil
package anyx
import "fmt"
func ExampleAnyToStr() {
func ExampleToStr() {
a := 123
res := AnyToStr(a)
res := ToStr(a)
fmt.Println(res)
@@ -1,8 +1,8 @@
package anyUtil
package anyx
import "testing"
func TestAnyToStr(t *testing.T) {
func TestToStr(t *testing.T) {
iPtr := 90
testCases := []struct {
name string
@@ -33,7 +33,7 @@ func TestAnyToStr(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := AnyToStr(tc.input)
result := ToStr(tc.input)
if result != tc.expected {
t.Errorf("Unexpected result. Expected: %v, but got: %v", tc.expected, result)
}
+17 -17
View File
@@ -1,4 +1,4 @@
package anyUtil
package anyx
import (
"math"
@@ -6,24 +6,24 @@ import (
"strconv"
)
// AnyToUint 将给定的值转换为 uint
func AnyToUint(i any) (uint, error) {
v, err := AnyToUint64(i)
// ToUint 将给定的值转换为 uint
func ToUint(i any) (uint, error) {
v, err := ToUint64(i)
if err != nil {
return 0, err
}
// uint 兼容32位和64位系统
//if uint64(uint(v)) != v {
// if uint64(uint(v)) != v {
// return 0, ErrValOut
//}
// }
return uint(v), nil
}
// AnyToUint8 将给定的值转换为 uint8
func AnyToUint8(i any) (uint8, error) {
value, err := AnyToUint64(i)
// ToUint8 将给定的值转换为 uint8
func ToUint8(i any) (uint8, error) {
value, err := ToUint64(i)
if err != nil {
return 0, err
}
@@ -33,9 +33,9 @@ func AnyToUint8(i any) (uint8, error) {
return uint8(value), nil
}
// AnyToUint16 将给定的值转换为 uint16
func AnyToUint16(i any) (uint16, error) {
value, err := AnyToUint64(i)
// ToUint16 将给定的值转换为 uint16
func ToUint16(i any) (uint16, error) {
value, err := ToUint64(i)
if err != nil {
return 0, err
}
@@ -45,9 +45,9 @@ func AnyToUint16(i any) (uint16, error) {
return uint16(value), nil
}
// AnyToUint32 将给定的值转换为 uint32
func AnyToUint32(i any) (uint32, error) {
value, err := AnyToUint64(i)
// ToUint32 将给定的值转换为 uint32
func ToUint32(i any) (uint32, error) {
value, err := ToUint64(i)
if err != nil {
return 0, err
}
@@ -57,8 +57,8 @@ func AnyToUint32(i any) (uint32, error) {
return uint32(value), nil
}
// AnyToUint64 将给定的值转换为 uint64
func AnyToUint64(i any) (uint64, error) {
// ToUint64 将给定的值转换为 uint64
func ToUint64(i any) (uint64, error) {
if i == nil {
return 0, nil
}
@@ -1,10 +1,10 @@
package strUtil
package anyx
import "fmt"
func ExampleStrToUint() {
func ExampleToUint() {
s := "200"
res := StrToUint(s)
res, _ := ToUint(s)
fmt.Printf("%d,%T\n", res, res)
@@ -12,9 +12,9 @@ func ExampleStrToUint() {
// 200,uint
}
func ExampleStrToUint8() {
func ExampleToUint8() {
s := "200"
res := StrToUint8(s)
res, _ := ToUint8(s)
fmt.Printf("%d,%T\n", res, res)
@@ -22,9 +22,9 @@ func ExampleStrToUint8() {
// 200,uint8
}
func ExampleStrToUint16() {
func ExampleToUint16() {
s := "200"
res := StrToUint16(s)
res, _ := ToUint16(s)
fmt.Printf("%d,%T\n", res, res)
@@ -32,9 +32,9 @@ func ExampleStrToUint16() {
// 200,uint16
}
func ExampleStrToUint32() {
func ExampleToUint32() {
s := "200"
res := StrToUint32(s)
res, _ := ToUint32(s)
fmt.Printf("%d,%T\n", res, res)
@@ -42,9 +42,9 @@ func ExampleStrToUint32() {
// 200,uint32
}
func ExampleStrToUint64() {
func ExampleToUint64() {
s := "200"
res := StrToUint64(s)
res, _ := ToUint64(s)
fmt.Printf("%d,%T\n", res, res)
@@ -1,4 +1,4 @@
package anyUtil
package anyx
import (
"github.com/stretchr/testify/assert"
@@ -6,7 +6,7 @@ import (
"testing"
)
func TestAnyToUint(t *testing.T) {
func TestToUint(t *testing.T) {
iPtr := 90
testCases := []struct {
name string
@@ -64,14 +64,14 @@ func TestAnyToUint(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, err := AnyToUint(tc.input)
res, err := ToUint(tc.input)
assert.Equal(t, tc.err, err)
assert.Equal(t, tc.expected, res)
})
}
}
func TestAnyToUint8(t *testing.T) {
func TestToUint8(t *testing.T) {
testCases := []struct {
name string
input any
@@ -103,14 +103,14 @@ func TestAnyToUint8(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, err := AnyToUint8(tc.input)
res, err := ToUint8(tc.input)
assert.Equal(t, tc.err, err)
assert.Equal(t, tc.expected, res)
})
}
}
func TestAnyToUint16(t *testing.T) {
func TestToUint16(t *testing.T) {
testCases := []struct {
name string
input any
@@ -142,14 +142,14 @@ func TestAnyToUint16(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, err := AnyToUint16(tc.input)
res, err := ToUint16(tc.input)
assert.Equal(t, tc.err, err)
assert.Equal(t, tc.expected, res)
})
}
}
func TestAnyToUint32(t *testing.T) {
func TestToUint32(t *testing.T) {
testCases := []struct {
name string
input any
@@ -181,14 +181,14 @@ func TestAnyToUint32(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, err := AnyToUint32(tc.input)
res, err := ToUint32(tc.input)
assert.Equal(t, tc.err, err)
assert.Equal(t, tc.expected, res)
})
}
}
func TestAnyToUint64(t *testing.T) {
func TestToUint64(t *testing.T) {
testCases := []struct {
name string
input any
@@ -229,7 +229,7 @@ func TestAnyToUint64(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, err := AnyToUint64(tc.input)
res, err := ToUint64(tc.input)
assert.Equal(t, tc.err, err)
assert.Equal(t, tc.expected, res)
})
+1 -1
View File
@@ -1,4 +1,4 @@
package anyUtil
package anyx
import (
"errors"
-22
View File
@@ -1,22 +0,0 @@
# byteUtil
## Install
```bash
go get -u github.com/jefferyjob/go-easy-utils/v2/byteUtil
```
## Import
```go
import (
"github.com/jefferyjob/go-easy-utils/v2/byteUtil"
)
```
## Functions
```go
// BytesToStr 字节数组转字符串
func BytesToStr(b []byte) string
```
-8
View File
@@ -1,8 +0,0 @@
package byteUtil
import "unsafe"
// BytesToStr 字节数组转字符串
func BytesToStr(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
-1
View File
@@ -1 +0,0 @@
package byteUtil
+22
View File
@@ -0,0 +1,22 @@
# bytex
## Install
```bash
go get -u github.com/jefferyjob/go-easy-utils/v3/bytex
```
## Import
```go
import (
"github.com/jefferyjob/go-easy-utils/v3/bytex"
)
```
## Functions
```go
// ToStr 字节数组转字符串
func ToStr(b []byte) string
```
+8
View File
@@ -0,0 +1,8 @@
package bytex
import "unsafe"
// ToStr 字节数组转字符串
func ToStr(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
@@ -1,10 +1,10 @@
package byteUtil
package bytex
import "fmt"
func ExampleBytesToStr() {
func ExampleToStr() {
b := []byte{'h', 'e', 'l', 'l', 'o'}
res := BytesToStr(b)
res := ToStr(b)
fmt.Println(res)
@@ -1,11 +1,11 @@
package byteUtil
package bytex
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestBytesToStr(t *testing.T) {
func TestToStr(t *testing.T) {
tests := []struct {
name string
data []byte
@@ -16,7 +16,7 @@ func TestBytesToStr(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := BytesToStr(tt.data)
got := ToStr(tt.data)
assert.Equal(t, tt.expected, got)
})
}
+1
View File
@@ -0,0 +1 @@
package bytex
-1
View File
@@ -1 +0,0 @@
package cryptoUtil
-1
View File
@@ -1 +0,0 @@
package cryptoUtil
+3 -3
View File
@@ -1,16 +1,16 @@
# cryptoUtil
# cryptox
## Install
```bash
go get -u github.com/jefferyjob/go-easy-utils/v2/cryptoUtil
go get -u github.com/jefferyjob/go-easy-utils/v3/cryptox
```
## Import
```go
import (
"github.com/jefferyjob/go-easy-utils/v2/cryptoUtil"
"github.com/jefferyjob/go-easy-utils/v3/cryptox"
)
```
+1 -1
View File
@@ -1,4 +1,4 @@
package cryptoUtil
package cryptox
import (
"crypto/sha256"
@@ -1,4 +1,4 @@
package cryptoUtil
package cryptox
import "fmt"
@@ -1,4 +1,4 @@
package cryptoUtil
package cryptox
import (
"github.com/stretchr/testify/assert"
+1 -1
View File
@@ -1,4 +1,4 @@
package cryptoUtil
package cryptox
import (
"crypto/md5"
@@ -1,4 +1,4 @@
package cryptoUtil
package cryptox
import "fmt"
@@ -1,4 +1,4 @@
package cryptoUtil
package cryptox
import (
"github.com/stretchr/testify/assert"
+1 -1
View File
@@ -1,4 +1,4 @@
package cryptoUtil
package cryptox
import (
"crypto/rand"
+1
View File
@@ -0,0 +1 @@
package cryptox
@@ -1,4 +1,4 @@
package cryptoUtil
package cryptox
import (
"crypto/rand"
+1
View File
@@ -0,0 +1 @@
package cryptox
-23
View File
@@ -1,23 +0,0 @@
package emojiUtil
import "fmt"
func ExampleEncodeEmojiUnicode() {
e := "[\\u1F60E]"
res := DecodeEmojiUnicode(e)
fmt.Println(res)
// Output:
// 😎
}
func ExampleDecodeEmojiUnicode() {
e := "😂"
res := EncodeEmojiUnicode(e)
fmt.Println(res)
// Output:
// [\u1f602]
}
-1
View File
@@ -1 +0,0 @@
package emojiUtil
+4 -4
View File
@@ -17,11 +17,11 @@ import (
## Functions
```go
// DecodeEmojiUnicode Emoji表情解码
func DecodeEmojiUnicode(unicode string) string
// Decode Emoji表情解码
func Decode(unicode string) string
// EncodeEmojiUnicode Emoji表情编码
func EncodeEmojiUnicode(emoji string) string
// Encode Emoji表情编码
func Encode(emoji string) string
```
### emoji表情unicode编码表
+7 -7
View File
@@ -1,4 +1,4 @@
package emojiUtil
package emojix
import (
"regexp"
@@ -6,12 +6,12 @@ import (
"strings"
)
// DecodeEmojiUnicode Emoji表情解码
// Decode Emoji表情解码
// 输入unicode编码,得到emoji表情
func DecodeEmojiUnicode(unicode string) string {
//emoji表情的数据表达式
func Decode(unicode string) string {
// emoji表情的数据表达式
re := regexp.MustCompile("\\[[\\\\u0-9a-zA-Z]+\\]")
//提取emoji数据表达式
// 提取emoji数据表达式
reg := regexp.MustCompile("\\[\\\\u|]")
src := re.FindAllString(unicode, -1)
for i := 0; i < len(src); i++ {
@@ -24,9 +24,9 @@ func DecodeEmojiUnicode(unicode string) string {
return unicode
}
// EncodeEmojiUnicode Emoji表情编码
// Encode Emoji表情编码
// 输入emoji表情,得到unicode编码
func EncodeEmojiUnicode(emoji string) string {
func Encode(emoji string) string {
ret := ""
rs := []rune(emoji)
for i := 0; i < len(rs); i++ {
+23
View File
@@ -0,0 +1,23 @@
package emojix
import "fmt"
func ExampleEncode() {
e := "[\\u1F60E]"
res := Decode(e)
fmt.Println(res)
// Output:
// 😎
}
func ExampleDecode() {
e := "😂"
res := Encode(e)
fmt.Println(res)
// Output:
// [\u1f602]
}
@@ -1,4 +1,4 @@
package emojiUtil
package emojix
import (
"github.com/stretchr/testify/assert"
@@ -6,7 +6,7 @@ import (
"testing"
)
func TestDecodeEmojiUnicode(t *testing.T) {
func TestDecode(t *testing.T) {
testCases := []struct {
name string
input string
@@ -20,7 +20,7 @@ func TestDecodeEmojiUnicode(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := DecodeEmojiUnicode(tc.input)
res := Decode(tc.input)
// 转大写
res = strings.ToUpper(res)
expected := strings.ToUpper(tc.expected)
@@ -29,7 +29,7 @@ func TestDecodeEmojiUnicode(t *testing.T) {
}
}
func TestEncodeEmojiUnicode(t *testing.T) {
func TestEncode(t *testing.T) {
testCases := []struct {
name string
input string
@@ -41,7 +41,7 @@ func TestEncodeEmojiUnicode(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := EncodeEmojiUnicode(tc.input)
actual := Encode(tc.input)
// 转大写
actual = strings.ToUpper(actual)
expected := strings.ToUpper(tc.expected)
+1
View File
@@ -0,0 +1 @@
package emojix
-1
View File
@@ -1 +0,0 @@
package floatUtil
+2 -2
View File
@@ -3,14 +3,14 @@
## Install
```bash
go get -u github.com/jefferyjob/go-easy-utils/v2/floatUtil
go get -u github.com/jefferyjob/go-easy-utils/v3/floatx
```
## Import
```go
import (
"github.com/jefferyjob/go-easy-utils/v2/floatUtil"
"github.com/jefferyjob/go-easy-utils/v3/floatx"
)
```
+1 -1
View File
@@ -1,4 +1,4 @@
package floatUtil
package floatx
import (
"fmt"
@@ -1,4 +1,4 @@
package floatUtil
package floatx
import "fmt"
@@ -1,4 +1,4 @@
package floatUtil
package floatx
import (
"github.com/stretchr/testify/assert"
+1
View File
@@ -0,0 +1 @@
package floatx
+1 -1
View File
@@ -1,4 +1,4 @@
module github.com/jefferyjob/go-easy-utils/v2
module github.com/jefferyjob/go-easy-utils/v3
go 1.24
+4 -4
View File
@@ -30,14 +30,14 @@ JsonToStruct 将 JSON 数据转换为 Go 结构。
## 安装
```bash
go get -u github.com/jefferyjob/go-easy-utils/v2/jsonUtil
go get -u github.com/jefferyjob/go-easy-utils/v3/jsonx
```
## 导入
```go
import (
"github.com/jefferyjob/go-easy-utils/jsonUtil"
"github.com/jefferyjob/go-easy-utils/v3/jsonx"
)
```
@@ -66,7 +66,7 @@ func TestDemo1(t *testing.T) {
IsUse bool `json:"is_use"`
}
if err := JsonToStruct(jsonData, &people); err != nil {
if err := ToStruct(jsonData, &people); err != nil {
fmt.Println(err)
return
}
@@ -101,7 +101,7 @@ func TestJsonToStructDemo2(t *testing.T) {
}`
var person Person
err := JsonToStruct(jsonData, &person)
err := ToStruct(jsonData, &person)
if err != nil {
fmt.Println(err)
return
+5 -5
View File
@@ -30,21 +30,21 @@ JsonToStruct converts JSON data to a Go structure.
## Install
```bash
go get -u github.com/jefferyjob/go-easy-utils/v2/jsonUtil
go get -u github.com/jefferyjob/go-easy-utils/v3/jsonx
```
## Import
```go
import (
"github.com/jefferyjob/go-easy-utils/jsonUtil"
"github.com/jefferyjob/go-easy-utils/v3/jsonx"
)
```
## Functions
```go
func JsonToStruct(jsonData string, val any) error
func ToStruct(jsonData string, val any) error
```
## Demo
@@ -66,7 +66,7 @@ func TestDemo1(t *testing.T) {
IsUse bool `json:"is_use"`
}
if err := JsonToStruct(jsonData, &people); err != nil {
if err := ToStruct(jsonData, &people); err != nil {
fmt.Println(err)
return
}
@@ -101,7 +101,7 @@ func TestJsonToStructDemo2(t *testing.T) {
}`
var person Person
err := JsonToStruct(jsonData, &person)
err := ToStruct(jsonData, &person)
if err != nil {
fmt.Println(err)
return
+2 -2
View File
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"fmt"
@@ -47,7 +47,7 @@ func parseValue(fieldVal reflect.Value, item any) error {
}
case reflect.Struct:
if subData, ok := item.(map[string]any); ok {
if err := JsonToStruct(convertToJSONString(subData), fieldVal.Addr().Interface()); err != nil {
if err := ToStruct(convertToJSONString(subData), fieldVal.Addr().Interface()); err != nil {
return err
}
} else {
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"reflect"
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"fmt"
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"reflect"
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"encoding/json"
@@ -30,8 +30,8 @@ func parseSlice(fieldValue reflect.Value, subData []any) error {
return err
}
// 调用 JsonToStruct 将 JSON 字符串解析为 struct 元素
err = JsonToStruct(string(jsonData), elem.Addr().Interface())
// 调用 ToStruct 将 JSON 字符串解析为 struct 元素
err = ToStruct(string(jsonData), elem.Addr().Interface())
if err != nil {
return err
}
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"reflect"
+2 -2
View File
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"reflect"
@@ -36,7 +36,7 @@ func toBoolReflect(i any) bool {
return false
}
return val != ""
//case reflect.Ptr, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Slice:
// case reflect.Ptr, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Slice:
// return !v.IsNil()
default:
return false
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"github.com/stretchr/testify/assert"
+2 -2
View File
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"reflect"
@@ -42,7 +42,7 @@ func toFloat64Reflect(i any) (float64, error) {
} else {
return 0, nil
}
//case reflect.Ptr, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Slice:
// case reflect.Ptr, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Slice:
// return 0, nil
default:
return 0, ErrType
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"github.com/stretchr/testify/assert"
+1 -1
View File
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"reflect"
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"github.com/stretchr/testify/assert"
+1 -1
View File
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"fmt"
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"github.com/stretchr/testify/assert"
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"encoding/json"
@@ -7,7 +7,7 @@ import (
"strings"
)
// JsonToStruct converts JSON data to a Go structure.
// ToStruct converts JSON data to a Go structure.
// Parameters:
//
// jsonData: A string containing the JSON data.
@@ -21,17 +21,17 @@ import (
//
// Checks if the val parameter is a non-nil pointer type, returning ErrPoint if it is not.
// Parses jsonData into a map[string]any variable called data.
// Retrieves the value and type of the structure pointed to by val using reflection.
// Retrieve the value and type of the structure pointed to by val using reflection.
// Iterates through the fields of the structure:
// Retrieves the field's type, name, and value.
// Gets the JSON tag for the field.
// Get the JSON tag for the field.
// Performs the appropriate handling if a key-value pair corresponding to the JSON tag exists in data:
// If the field is a primitive type (string, integer, float, boolean), parses it into the corresponding type's value.
// If the field is a struct type, recursively calls the JsonToStruct function to convert the sub-structure to JSON.
// If the field is a map type, uses the parseMap function to convert the sub-map to JSON.
// If the field is a slice type, uses the parseSlice function to convert the sub-slice to JSON.
// If the field is an interface type, sets the value to nil or the corresponding value.
func JsonToStruct(jsonData string, val any) error {
func ToStruct(jsonData string, val any) error {
if reflect.ValueOf(val).Kind() != reflect.Pointer || reflect.ValueOf(val).IsNil() {
return ErrPoint
}
@@ -68,7 +68,7 @@ func JsonToStruct(jsonData string, val any) error {
case reflect.Struct:
if subData, ok := value.(map[string]any); ok {
subResult := reflect.New(fieldValue.Type())
err := JsonToStruct(convertToJSONString(subData), subResult.Interface())
err := ToStruct(convertToJSONString(subData), subResult.Interface())
if err != nil {
return err
}
@@ -1,8 +1,8 @@
package jsonUtil
package jsonx
import "fmt"
func ExampleJsonToStruct() {
func ExampleToStruct() {
jsonData := `{
"name": "make",
"age": "22",
@@ -15,7 +15,7 @@ func ExampleJsonToStruct() {
IsUse bool `json:"is_use"`
}
if err := JsonToStruct(jsonData, &people); err != nil {
if err := ToStruct(jsonData, &people); err != nil {
fmt.Println(err)
return
}
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"testing"
@@ -30,7 +30,7 @@ func TestJsonToStructMap1(t *testing.T) {
Followed map[string]map[string]bool `json:"followed"`
}
err := JsonToStruct(data, &target)
err := ToStruct(data, &target)
if err != nil {
t.Errorf("err %s", err)
return
@@ -63,7 +63,7 @@ func TestJsonToStructMap2(t *testing.T) {
Foll map[string]Val `json:"foll"`
}
err := JsonToStruct(data, &target)
err := ToStruct(data, &target)
if err != nil {
t.Errorf("err %s", err)
return
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"testing"
@@ -16,7 +16,7 @@ func TestJsonToStructSlice1(t *testing.T) {
Fids []string `json:"fids"`
}
err := JsonToStruct(data, &target)
err := ToStruct(data, &target)
if err != nil {
t.Errorf("err %s", err)
return
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"encoding/json"
@@ -23,7 +23,7 @@ func TestDemo1(t *testing.T) {
IsUse bool `json:"is_use"`
}
if err := JsonToStruct(jsonData, &people); err != nil {
if err := ToStruct(jsonData, &people); err != nil {
fmt.Println(err)
return
}
@@ -56,7 +56,7 @@ func TestJsonToStructDemo2(t *testing.T) {
}`
var person Person
err := JsonToStruct(jsonData2, &person)
err := ToStruct(jsonData2, &person)
if err != nil {
fmt.Println(err)
return
@@ -97,7 +97,7 @@ func BenchmarkJsonToStruct(b *testing.B) {
IsUse bool `json:"is_use"`
}
for i := 0; i < b.N; i++ {
err := JsonToStruct(jsonData, &people)
err := ToStruct(jsonData, &people)
if err != nil {
fmt.Println("err:", err)
return
@@ -112,7 +112,7 @@ func TestJsonToStructErrJson(t *testing.T) {
Name string `json:"name"`
}
var people People
err := JsonToStruct(jsonData, &people)
err := ToStruct(jsonData, &people)
if err == nil {
t.Errorf("err %s", err)
return
@@ -131,7 +131,7 @@ func TestJsonToStructErrResult(t *testing.T) {
Age int `json:"age"`
IsUse bool `json:"is_use"`
}
err := JsonToStruct(jsonData, people)
err := ToStruct(jsonData, people)
if err == nil {
t.Errorf("err %s", err)
return
@@ -154,7 +154,7 @@ func TestJsonToStructEmptyValue(t *testing.T) {
Num float64 `json:"num"`
Status bool `json:"status"`
}
err := JsonToStruct(jsonData, &people)
err := ToStruct(jsonData, &people)
if err != nil {
t.Errorf("err %s", err)
return
@@ -171,7 +171,7 @@ func TestJsonToStructErrInt(t *testing.T) {
Name string `json:"name,omitempty"`
Age int `json:"age"`
}
err := JsonToStruct(jsonData, people)
err := ToStruct(jsonData, people)
if err == nil {
t.Errorf("err %s", err)
return
@@ -188,7 +188,7 @@ func TestJsonToStructErrUint(t *testing.T) {
Name string `json:"name,omitempty"`
Age uint `json:"age"`
}
err := JsonToStruct(jsonData, people)
err := ToStruct(jsonData, people)
if err == nil {
t.Errorf("err %s", err)
return
@@ -205,7 +205,7 @@ func TestJsonToStructErrFloat(t *testing.T) {
Name string `json:"name,omitempty"`
Age float64 `json:"age"`
}
err := JsonToStruct(jsonData, people)
err := ToStruct(jsonData, people)
if err == nil {
t.Errorf("err %s", err)
return
@@ -228,7 +228,7 @@ func TestJsonToStructNestErrInt(t *testing.T) {
Address Address `json:"address"`
}
people := &Person{}
err := JsonToStruct(jsonData, people)
err := ToStruct(jsonData, people)
if err == nil {
t.Errorf("err %s", err)
return
@@ -279,7 +279,7 @@ func TestJsonToStructMoreNest(t *testing.T) {
}
var resultPerson Person
err := JsonToStruct(jsonData, &resultPerson)
err := ToStruct(jsonData, &resultPerson)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
@@ -371,13 +371,13 @@ func TestJsonToStructMoreNest2(t *testing.T) {
}
var res Target
err := JsonToStruct(jsonData, &res)
err := ToStruct(jsonData, &res)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
//jsonRes, _ := json.Marshal(res)
//fmt.Printf("%+v \n", res)
//fmt.Println(string(jsonRes))
// jsonRes, _ := json.Marshal(res)
// fmt.Printf("%+v \n", res)
// fmt.Println(string(jsonRes))
expected := Target{
Uid: 666,
@@ -429,7 +429,7 @@ func TestJsonToStructAny1(t *testing.T) {
Age uint `json:"age"`
}
var target Target
err := JsonToStruct(jsonData, &target)
err := ToStruct(jsonData, &target)
if err != nil {
t.Errorf("err %s", err)
return
@@ -447,7 +447,7 @@ func TestJsonToStructAny2(t *testing.T) {
Age uint `json:"age"`
}
var target Target
err := JsonToStruct(jsonData, &target)
err := ToStruct(jsonData, &target)
if err != nil {
t.Errorf("err %s", err)
return
@@ -463,7 +463,7 @@ func TestJsonToStructAny3(t *testing.T) {
Name []interface{} `json:"name"`
}
var target Target
err := JsonToStruct(jsonData, &target)
err := ToStruct(jsonData, &target)
if err != nil {
t.Errorf("err %s", err)
return
@@ -479,7 +479,7 @@ func TestJsonToStructAny4(t *testing.T) {
Name []any `json:"name"`
}
var target Target
err := JsonToStruct(jsonData, &target)
err := ToStruct(jsonData, &target)
if err != nil {
t.Errorf("err %s", err)
return
@@ -499,7 +499,7 @@ func TestJsonToStructAny5(t *testing.T) {
Name map[string]interface{} `json:"name"`
}
var target Target
err := JsonToStruct(jsonData, &target)
err := ToStruct(jsonData, &target)
if err != nil {
t.Errorf("err %s", err)
return
@@ -518,7 +518,7 @@ func TestJsonToStructAny6(t *testing.T) {
Name map[string]any `json:"name"`
}
var target Target
err := JsonToStruct(jsonData, &target)
err := ToStruct(jsonData, &target)
if err != nil {
t.Errorf("err %s", err)
return
@@ -526,7 +526,7 @@ func TestJsonToStructAny6(t *testing.T) {
}
// 多层级json测试
//func TestJsonToStruct4(t *testing.T) {
// func TestJsonToStruct4(t *testing.T) {
// type Address struct {
// City string `json:"city"`
// Street string `json:"street"`
@@ -565,9 +565,9 @@ func TestJsonToStructAny6(t *testing.T) {
// }
//
// fmt.Printf("%+v \n", student)
//}
// }
//func TestJsonToStruct5(t *testing.T) {
// func TestJsonToStruct5(t *testing.T) {
// type Student struct {
// Name string `json:"name,omitempty"`
// Age int `json:"age,omitempty"`
@@ -584,9 +584,9 @@ func TestJsonToStructAny6(t *testing.T) {
// }
//
// fmt.Printf("%+v \n", student)
//}
// }
//func TestJsonToStruct6(t *testing.T) {
// func TestJsonToStruct6(t *testing.T) {
// type Student struct {
// Name any `json:"name,omitempty"`
// Age int `json:"age,omitempty"`
@@ -603,9 +603,9 @@ func TestJsonToStructAny6(t *testing.T) {
// }
//
// fmt.Printf("%+v \n", student)
//}
// }
//func TestJsonToStruct7(t *testing.T) {
// func TestJsonToStruct7(t *testing.T) {
// type Student struct {
// Name bool `json:"name"`
// Name2 uint `json:"name2"`
@@ -626,4 +626,4 @@ func TestJsonToStructAny6(t *testing.T) {
// }
//
// fmt.Printf("%#v \n", student)
//}
// }
+1 -1
View File
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"reflect"
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"github.com/stretchr/testify/assert"
+1 -1
View File
@@ -1,4 +1,4 @@
package jsonUtil
package jsonx
import (
"errors"
-31
View File
@@ -1,31 +0,0 @@
# mapUtil
## Install
```bash
go get -u github.com/jefferyjob/go-easy-utils/v2/mapUtil
```
## Import
```go
import (
"github.com/jefferyjob/go-easy-utils/v2/mapUtil"
)
```
## Functions
```go
// MapKeyExists 判断map中的key是否存在
func MapKeyExists((m map[T]T2, key T)) bool
// MapValueExists 判断map中的value是否存在
func MapValueExists(m map[T2]T, value T) bool
// ExtractKeys 提取Map的所有键
func ExtractKeys[K comparable, V any](m map[K]V) []K
// ExtractValues 提取Map的所有值
func ExtractValues[K comparable, V any](m map[K]V) []V
```
-10
View File
@@ -1,10 +0,0 @@
package mapUtil
// ExtractKeys 提取Map的所有键
func ExtractKeys[K comparable, V any](m map[K]V) []K {
keys := make([]K, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
-10
View File
@@ -1,10 +0,0 @@
package mapUtil
// ExtractValues 提取Map的所有值
func ExtractValues[K comparable, V any](m map[K]V) []V {
values := make([]V, 0, len(m))
for _, v := range m {
values = append(values, v)
}
return values
}
-17
View File
@@ -1,17 +0,0 @@
package mapUtil
// MapKeyExists 判断map中的key是否存在
func MapKeyExists[T comparable, T2 any](m map[T]T2, key T) bool {
_, exists := m[key]
return exists
}
// MapValueExists 判断map中的value是否存在
func MapValueExists[T comparable, T2 comparable](m map[T2]T, value T) bool {
for _, v := range m {
if v == value {
return true
}
}
return false
}
-1
View File
@@ -1 +0,0 @@
package mapUtil
+31
View File
@@ -0,0 +1,31 @@
# mapUtil
## Install
```bash
go get -u github.com/jefferyjob/go-easy-utils/v2/mapUtil
```
## Import
```go
import (
"github.com/jefferyjob/go-easy-utils/v2/mapUtil"
)
```
## Functions
```go
// KeyExists 判断map中的key是否存在
func KeyExists((m map[T]T2, key T)) bool
// ValueExists 判断map中的value是否存在
func ValueExists(m map[T2]T, value T) bool
// Keys 提取的所有键
func Keys[K comparable, V any](m map[K]V) []K
// Values 提取的所有值
func Values[K comparable, V any](m map[K]V) []V
```
+17
View File
@@ -0,0 +1,17 @@
package mapx
// KeyExists 判断map中的key是否存在
func KeyExists[T comparable, T2 any](m map[T]T2, key T) bool {
_, exists := m[key]
return exists
}
// ValueExists 判断map中的value是否存在
func ValueExists[T comparable, T2 comparable](m map[T2]T, value T) bool {
for _, v := range m {
if v == value {
return true
}
}
return false
}
@@ -1,13 +1,13 @@
package mapUtil
package mapx
import "fmt"
func ExampleMapKeyExists() {
func ExampleKeyExists() {
m := map[string]string{
"foo": "bar",
"baz": "123",
}
res := MapKeyExists(m, "foo")
res := KeyExists(m, "foo")
fmt.Println(res)
@@ -15,12 +15,12 @@ func ExampleMapKeyExists() {
// true
}
func ExampleMapValueExists() {
func ExampleValueExists() {
m := map[string]string{
"foo": "bar",
"baz": "123",
}
res := MapValueExists(m, "123")
res := ValueExists(m, "123")
fmt.Println(res)
@@ -1,20 +1,20 @@
package mapUtil
package mapx
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestMapKeyExists(t *testing.T) {
func TestKeyExists(t *testing.T) {
testCases := []struct {
name string
inputMap map[string]string
input map[string]string
inputKey string
wantRes bool
}{
{
name: "存在",
inputMap: map[string]string{
input: map[string]string{
"for": "jack",
"bar": "123",
},
@@ -23,7 +23,7 @@ func TestMapKeyExists(t *testing.T) {
},
{
name: "不存在",
inputMap: map[string]string{
input: map[string]string{
"for": "jack",
"bar": "123",
},
@@ -34,22 +34,22 @@ func TestMapKeyExists(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := MapKeyExists(tc.inputMap, tc.inputKey)
res := KeyExists(tc.input, tc.inputKey)
assert.Equal(t, tc.wantRes, res)
})
}
}
func TestMapValueExists(t *testing.T) {
func TestValueExists(t *testing.T) {
testCases := []struct {
name string
inputMap map[string]string
input map[string]string
inputValue string
wantRes bool
}{
{
name: "存在",
inputMap: map[string]string{
input: map[string]string{
"for": "jack",
"bar": "123",
},
@@ -58,7 +58,7 @@ func TestMapValueExists(t *testing.T) {
},
{
name: "不存在",
inputMap: map[string]string{
input: map[string]string{
"for": "jack",
"bar": "123",
},
@@ -69,7 +69,7 @@ func TestMapValueExists(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := MapValueExists(tc.inputMap, tc.inputValue)
res := ValueExists(tc.input, tc.inputValue)
assert.Equal(t, tc.wantRes, res)
})
}
+10
View File
@@ -0,0 +1,10 @@
package mapx
// Keys 提取的所有键
func Keys[K comparable, V any](m map[K]V) []K {
keys := make([]K, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
@@ -1,13 +1,13 @@
package mapUtil
package mapx
import (
"fmt"
"sort"
)
func ExampleExtractKeys() {
func ExampleKeys() {
m := map[string]int{"apple": 5, "banana": 3, "orange": 7}
keys := ExtractKeys(m)
keys := Keys(m)
// 对结果排序以确保输出一致(map 迭代顺序随机)
sort.Strings(keys)
@@ -1,11 +1,11 @@
package mapUtil
package mapx
import (
"sort"
"testing"
)
func TestExtractKeys(t *testing.T) {
func TestKeys(t *testing.T) {
tests := []struct {
name string
input map[string]int
@@ -30,7 +30,7 @@ func TestExtractKeys(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ExtractKeys(tt.input)
result := Keys(tt.input)
// 对结果排序,因为 map 迭代顺序是随机的
sort.Strings(result)
@@ -50,7 +50,7 @@ func TestExtractKeys(t *testing.T) {
}
}
func TestExtractKeys2(t *testing.T) {
func TestKeys2(t *testing.T) {
tests := []struct {
name string
input map[int]string
@@ -65,7 +65,7 @@ func TestExtractKeys2(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ExtractKeys(tt.input)
result := Keys(tt.input)
// 对结果排序,因为 map 迭代顺序是随机的
sort.Ints(result)
@@ -85,7 +85,7 @@ func TestExtractKeys2(t *testing.T) {
}
}
func TestExtractKeys3(t *testing.T) {
func TestKeys3(t *testing.T) {
tests := []struct {
name string
input map[float64]bool
@@ -100,7 +100,7 @@ func TestExtractKeys3(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ExtractKeys(tt.input)
result := Keys(tt.input)
// 对结果排序,因为 map 迭代顺序是随机的
sort.Float64s(result)
+10
View File
@@ -0,0 +1,10 @@
package mapx
// Values 提取的所有值
func Values[K comparable, V any](m map[K]V) []V {
values := make([]V, 0, len(m))
for _, v := range m {
values = append(values, v)
}
return values
}
@@ -1,13 +1,13 @@
package mapUtil
package mapx
import (
"fmt"
"sort"
)
func ExampleExtractValues() {
func ExampleValues() {
m := map[string]int{"apple": 5, "banana": 3, "orange": 7}
values := ExtractValues(m)
values := Values(m)
// 对结果排序以确保输出一致(map 迭代顺序随机)
sort.Ints(values)
@@ -1,11 +1,11 @@
package mapUtil
package mapx
import (
"sort"
"testing"
)
func TestExtractValues(t *testing.T) {
func TestValues(t *testing.T) {
tests := []struct {
name string
input map[string]int
@@ -35,7 +35,7 @@ func TestExtractValues(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ExtractValues(tt.input)
result := Values(tt.input)
// 对结果排序,因为 map 迭代顺序是随机的
sort.Ints(result)
@@ -55,7 +55,7 @@ func TestExtractValues(t *testing.T) {
}
}
func TestExtractValues2(t *testing.T) {
func TestValues2(t *testing.T) {
tests := []struct {
name string
input map[int]string
@@ -70,7 +70,7 @@ func TestExtractValues2(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ExtractValues(tt.input)
result := Values(tt.input)
// 对结果排序,因为 map 迭代顺序是随机的
sort.Strings(result)
@@ -90,7 +90,7 @@ func TestExtractValues2(t *testing.T) {
}
}
func TestExtractValues3(t *testing.T) {
func TestValues3(t *testing.T) {
type Point struct{ X, Y int }
tests := []struct {
@@ -113,7 +113,7 @@ func TestExtractValues3(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ExtractValues(tt.input)
result := Values(tt.input)
if len(result) != len(tt.expected) {
t.Errorf("Expected %v, got %v", tt.expected, result)
+1
View File
@@ -0,0 +1 @@
package mapx
-1
View File
@@ -1 +0,0 @@
package mathUtil
+2 -2
View File
@@ -3,14 +3,14 @@
## Install
```bash
go get -u github.com/jefferyjob/go-easy-utils/v2/mathUtil
go get -u github.com/jefferyjob/go-easy-utils/v3/mathx
```
## Import
```go
import (
"github.com/jefferyjob/go-easy-utils/v2/mathUtil"
"github.com/jefferyjob/go-easy-utils/v3/mathx"
)
```
+1 -1
View File
@@ -1,4 +1,4 @@
package mathUtil
package mathx
import "math"
@@ -1,4 +1,4 @@
package mathUtil
package mathx
import "fmt"

Some files were not shown because too many files have changed in this diff Show More