From 4595a94b4c6e83005eb49a0c264bc85c5d070ae3 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 4 Dec 2024 10:40:18 +0800 Subject: [PATCH] update doc --- compare/compare_internal.go | 239 +++++++++++++++--------------------- compare/compare_test.go | 29 +++-- convertor/convertor.go | 34 +++++ convertor/convertor_test.go | 81 ++++++++++++ datetime/datetime.go | 1 - docs/compare.md | 12 +- docs/compare_zh-CN.md | 12 +- docs/convertor.md | 54 ++++++-- docs/convertor_zh-CN.md | 61 ++++++--- docs/datetime.md | 2 +- docs/fileutil.md | 2 +- docs/fileutil_zh-CN.md | 2 +- docs/validator.md | 2 +- strutil/string.go | 1 - 14 files changed, 339 insertions(+), 193 deletions(-) diff --git a/compare/compare_internal.go b/compare/compare_internal.go index 562072d8..64a9829c 100644 --- a/compare/compare_internal.go +++ b/compare/compare_internal.go @@ -3,6 +3,7 @@ package compare import ( "bytes" "encoding/json" + "math/big" "reflect" "time" @@ -155,169 +156,129 @@ func compareBasicValue(operator string, leftValue, rightValue interface{}) bool } switch leftVal := leftValue.(type) { - case json.Number: - if left, err := leftVal.Float64(); err == nil { - switch rightVal := rightValue.(type) { - case json.Number: - if right, err := rightVal.Float64(); err == nil { - switch operator { - case equal: - if left == right { - return true - } - case lessThan: - if left < right { - return true - } - case greaterThan: - if left > right { - return true - } - case lessOrEqual: - if left <= right { - return true - } - case greaterOrEqual: - if left >= right { - return true - } - } - - } - - case float32, float64, int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: - right, err := convertor.ToFloat(rightValue) - if err != nil { - return false - } - switch operator { - case equal: - if left == right { - return true - } - case lessThan: - if left < right { - return true - } - case greaterThan: - if left > right { - return true - } - case lessOrEqual: - if left <= right { - return true - } - case greaterOrEqual: - if left >= right { - return true - } - } - } + case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: + left, err := convertor.ToBigInt(leftValue) + if err != nil { + return false + } + right, err := convertor.ToBigInt(rightValue) + if err != nil { + return false } - case float32, float64, int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: + return compareBigInt(operator, left, right) + + case float32, float64: left, err := convertor.ToFloat(leftValue) if err != nil { return false } - switch rightVal := rightValue.(type) { - case json.Number: - if right, err := rightVal.Float64(); err == nil { - switch operator { - case equal: - if left == right { - return true - } - case lessThan: - if left < right { - return true - } - case greaterThan: - if left > right { - return true - } - case lessOrEqual: - if left <= right { - return true - } - case greaterOrEqual: - if left >= right { - return true - } - } - } - case float32, float64, int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: - right, err := convertor.ToFloat(rightValue) - if err != nil { - return false - } - - switch operator { - case equal: - if left == right { - return true - } - case lessThan: - if left < right { - return true - } - case greaterThan: - if left > right { - return true - } - case lessOrEqual: - if left <= right { - return true - } - case greaterOrEqual: - if left >= right { - return true - } - } + right, err := convertor.ToFloat(rightValue) + if err != nil { + return false } + return compareFloats(operator, left, right) + case string: left := leftVal switch right := rightValue.(type) { case string: - switch operator { - case equal: - if left == right { - return true - } - case lessThan: - if left < right { - return true - } - case greaterThan: - if left > right { - return true - } - case lessOrEqual: - if left <= right { - return true - } - case greaterOrEqual: - if left >= right { - return true - } - } + return compareStrings(operator, left, right) } case bool: left := leftVal switch right := rightValue.(type) { case bool: - switch operator { - case equal: - if left == right { - return true + return compareBools(operator, left, right) + } + + case json.Number: + if left, err := leftVal.Float64(); err == nil { + switch rightVal := rightValue.(type) { + case json.Number: + if right, err := rightVal.Float64(); err == nil { + return compareFloats(operator, left, right) } + case float32, float64: + right, err := convertor.ToFloat(rightValue) + if err != nil { + return false + } + return compareFloats(operator, left, right) + + case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: + right, err := convertor.ToBigInt(rightValue) + if err != nil { + return false + } + left, err := convertor.ToBigInt(left) + return compareBigInt(operator, left, right) } } + } + + return false +} +// compareBigInt compares two big.Int values based on the operator +func compareBigInt(operator string, left, right *big.Int) bool { + switch operator { + case equal: + return left.Cmp(right) == 0 + case lessThan: + return left.Cmp(right) < 0 + case greaterThan: + return left.Cmp(right) > 0 + case lessOrEqual: + return left.Cmp(right) <= 0 + case greaterOrEqual: + return left.Cmp(right) >= 0 } + return false +} + +// compareFloats compares two float64 values based on the operator +func compareFloats(operator string, left, right float64) bool { + switch operator { + case equal: + return left == right + case lessThan: + return left < right + case greaterThan: + return left > right + case lessOrEqual: + return left <= right + case greaterOrEqual: + return left >= right + } + return false +} +// compareStrings compares two string values based on the operator +func compareStrings(operator string, left, right string) bool { + switch operator { + case equal: + return left == right + case lessThan: + return left < right + case greaterThan: + return left > right + case lessOrEqual: + return left <= right + case greaterOrEqual: + return left >= right + } + return false +} + +// compareBools compares two boolean values based on the operator +func compareBools(operator string, left, right bool) bool { + switch operator { + case equal: + return left == right + } return false } diff --git a/compare/compare_test.go b/compare/compare_test.go index 61f26f08..37354f0f 100644 --- a/compare/compare_test.go +++ b/compare/compare_test.go @@ -1,6 +1,7 @@ package compare import ( + "encoding/json" "testing" "time" @@ -70,18 +71,28 @@ func TestEqualValue(t *testing.T) { } func TestLessThan(t *testing.T) { + t.Parallel() assert := internal.NewAssert(t, "TestLessThan") - assert.Equal(true, LessThan(1, 2)) - assert.Equal(true, LessThan(1.1, 2.2)) - assert.Equal(true, LessThan("a", "b")) - - time1 := time.Now() - time2 := time1.Add(time.Second) - assert.Equal(true, LessThan(time1, time2)) + tests := []struct { + left interface{} + right interface{} + want bool + }{ + {1, 2, true}, + {1.1, 2.2, true}, + {"a", "b", true}, + {time.Now(), time.Now().Add(time.Second), true}, + {[]byte("hello1"), []byte("hello2"), true}, + {json.Number("123"), json.Number("124"), true}, + {645680099112988673, 645680099112988675, true}, + {1, 1, false}, + {1, int64(1), false}, + } - assert.Equal(false, LessThan(1, 1)) - assert.Equal(false, LessThan(1, int64(1))) + for _, tt := range tests { + assert.Equal(tt.want, LessThan(tt.left, tt.right)) + } } func TestGreaterThan(t *testing.T) { diff --git a/convertor/convertor.go b/convertor/convertor.go index 4b10528e..2f5364df 100644 --- a/convertor/convertor.go +++ b/convertor/convertor.go @@ -14,6 +14,7 @@ import ( "fmt" "io" "math" + "math/big" "reflect" "regexp" "strconv" @@ -460,3 +461,36 @@ func ToRawUrlBase64(value interface{}) string { return base64.RawURLEncoding.EncodeToString(marshal) } } + +// ToBigInt converts an integer of any supported type (int, int64, uint64, etc.) to *big.Int +// Play: todo +func ToBigInt(v interface{}) (*big.Int, error) { + result := new(big.Int) + + switch v := (v).(type) { + case int: + result.SetInt64(int64(v)) + case int8: + result.SetInt64(int64(v)) + case int16: + result.SetInt64(int64(v)) + case int32: + result.SetInt64(int64(v)) + case int64: + result.SetInt64(v) + case uint: + result.SetUint64(uint64(v)) + case uint8: + result.SetUint64(uint64(v)) + case uint16: + result.SetUint64(uint64(v)) + case uint32: + result.SetUint64(uint64(v)) + case uint64: + result.SetUint64(v) + default: + return nil, fmt.Errorf("unsupported type: %T", v) + } + + return result, nil +} diff --git a/convertor/convertor_test.go b/convertor/convertor_test.go index c452f527..9cffdb30 100644 --- a/convertor/convertor_test.go +++ b/convertor/convertor_test.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "math/big" "reflect" "testing" "unicode/utf8" @@ -684,3 +685,83 @@ func TestToRawUrlBase64(t *testing.T) { d15, _ := base64.RawURLEncoding.DecodeString(r15) assert.Equal("4+3/4?=", string(d15)) } + +func TestToBigInt(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestToBigInt") + + tests := []struct { + name string + input interface{} + want *big.Int + hasErr bool + }{ + { + name: "int", + input: 42, + want: big.NewInt(42), + }, + { + name: "int8", + input: int8(127), + want: big.NewInt(127), + }, + { + name: "int16", + input: int16(32000), + want: big.NewInt(32000), + }, + { + name: "int32", + input: int32(123456), + want: big.NewInt(123456), + }, + { + name: "int64", + input: int64(987654321), + want: big.NewInt(987654321), + }, + { + name: "uint", + input: uint(987654321), + want: big.NewInt(987654321), + }, + { + name: "uint8", + input: uint8(255), + want: big.NewInt(255), + }, + { + name: "uint16", + input: uint16(65535), + want: big.NewInt(65535), + }, + { + name: "uint32", + input: uint32(4294967295), + want: big.NewInt(4294967295), + }, + { + name: "uint64", + input: uint64(18446744073709551615), + want: new(big.Int).SetUint64(18446744073709551615), + }, + { + name: "unsupported type", + input: 3.14, // Unsupported type + hasErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := ToBigInt(tt.input) + if (err != nil) != tt.hasErr { + t.Errorf("ToBigInt() error = %v, hasErr %v", err, tt.hasErr) + return + } + + assert.Equal(tt.want, got) + }) + } +} diff --git a/datetime/datetime.go b/datetime/datetime.go index 64f98ab0..41dea469 100644 --- a/datetime/datetime.go +++ b/datetime/datetime.go @@ -325,7 +325,6 @@ func TimestampNano(timezone ...string) int64 { // TrackFuncTime track the time of function execution. // call it at top of the func like `defer TrackFuncTime(time.Now())()` -// Play: todo func TrackFuncTime(pre time.Time) func() { callerName := getCallerName() return func() { diff --git a/docs/compare.md b/docs/compare.md index d0aefaf2..f838a33c 100644 --- a/docs/compare.md +++ b/docs/compare.md @@ -42,7 +42,7 @@ import ( Signature: ```go -func Equal(left, right any) bool +func Equal(left, right interface{}) bool ``` Example: @@ -91,7 +91,7 @@ func main() { Signature: ```go -func EqualValue(left, right any) bool +func EqualValue(left, right interface{}) bool ``` Example: @@ -130,7 +130,7 @@ func main() { Signature: ```go -func LessThan(left, right any) bool +func LessThan(left, right interface{}) bool ``` Example: @@ -179,7 +179,7 @@ func main() { Signature: ```go -func GreaterThan(left, right any) bool +func GreaterThan(left, right interface{}) bool ``` Example: @@ -231,7 +231,7 @@ func main() { Signature: ```go -func LessOrEqual(left, right any) bool +func LessOrEqual(left, right interface{}) bool ``` Example: @@ -280,7 +280,7 @@ func main() { Signature: ```go -func GreaterOrEqual(left, right any) bool +func GreaterOrEqual(left, right interface{}) bool ``` Example: diff --git a/docs/compare_zh-CN.md b/docs/compare_zh-CN.md index 8693d21b..97d22c4f 100644 --- a/docs/compare_zh-CN.md +++ b/docs/compare_zh-CN.md @@ -42,7 +42,7 @@ import ( 函数签名: ```go -func Equal(left, right any) bool +func Equal(left, right interface{}) bool ``` 示例: @@ -91,7 +91,7 @@ func main() { 函数签名: ```go -func EqualValue(left, right any) bool +func EqualValue(left, right interface{}) bool ``` 示例: @@ -130,7 +130,7 @@ func main() { 函数签名: ```go -func LessThan(left, right any) bool +func LessThan(left, right interface{}) bool ``` 示例: @@ -179,7 +179,7 @@ func main() { 函数签名: ```go -func GreaterThan(left, right any) bool +func GreaterThan(left, right interface{}) bool ``` 示例: @@ -231,7 +231,7 @@ func main() { 函数签名: ```go -func LessOrEqual(left, right any) bool +func LessOrEqual(left, right interface{}) bool ``` 示例: @@ -280,7 +280,7 @@ func main() { 函数签名: ```go -func GreaterOrEqual(left, right any) bool +func GreaterOrEqual(left, right interface{}) bool ``` 示例: diff --git a/docs/convertor.md b/docs/convertor.md index f497a39f..75905853 100644 --- a/docs/convertor.md +++ b/docs/convertor.md @@ -45,7 +45,7 @@ import ( - [ToUrlBase64](#ToUrlBase64) - [ToRawStdBase64](#ToRawStdBase64) - [ToRawUrlBase64](#ToRawUrlBase64) - +- [ToBigInt](#ToBigInt)
@@ -454,7 +454,7 @@ func main() { Signature: ```go -func EncodeByte(data any) ([]byte, error) +func EncodeByte(data interface{}) ([]byte, error) ``` Example: @@ -480,7 +480,7 @@ func main() { Signature: ```go -func DecodeByte(data []byte, target any) error +func DecodeByte(data []byte, target interface{}) error ``` Example: @@ -508,7 +508,7 @@ func main() { Signature: ```go -func DeepClone[T any](src T) T +func DeepClone[T interface{}](src T) T ``` Example: @@ -753,7 +753,7 @@ func main() { Signature: ```go -func ToStdBase64(value any) string +func ToStdBase64(value interface{}) string ``` Example: @@ -785,7 +785,7 @@ func main() { afterEncode = convertor.ToStdBase64(intVal) fmt.Println(afterEncode) - mapVal := map[string]any{"a": "hi", "b": 2, "c": struct { + mapVal := map[string]interface{}{"a": "hi", "b": 2, "c": struct { A string B int }{"hello", 3}} @@ -825,7 +825,7 @@ func main() { Signature: ```go -func ToUrlBase64(value any) string +func ToUrlBase64(value interface{}) string ``` Example: @@ -855,7 +855,7 @@ func main() { afterEncode = convertor.ToUrlBase64(intVal) fmt.Println(afterEncode) - mapVal := map[string]any{"a": "hi", "b": 2, "c": struct { + mapVal := map[string]interface{}{"a": "hi", "b": 2, "c": struct { A string B int }{"hello", 3}} @@ -894,7 +894,7 @@ func main() { Signature: ```go -func ToRawStdBase64(value any) string +func ToRawStdBase64(value interface{}) string ``` Example: @@ -921,7 +921,7 @@ func main() { afterEncode = convertor.ToRawStdBase64(intVal) fmt.Println(afterEncode) - mapVal := map[string]any{"a": "hi", "b": 2, "c": struct { + mapVal := map[string]interface{}{"a": "hi", "b": 2, "c": struct { A string B int }{"hello", 3}} @@ -958,7 +958,7 @@ func main() { Signature: ```go -func ToRawUrlBase64(value any) string +func ToRawUrlBase64(value interface{}) string ``` Example: @@ -985,7 +985,7 @@ func main() { afterEncode = convertor.ToRawUrlBase64(intVal) fmt.Println(afterEncode) - mapVal := map[string]any{"a": "hi", "b": 2, "c": struct { + mapVal := map[string]interface{}{"a": "hi", "b": 2, "c": struct { A string B int }{"hello", 3}} @@ -1013,4 +1013,34 @@ func main() { // dHJ1ZQ // ZXJy } +``` + +### ToBigInt + +

Convert value to bigInt.

+ +Signature: + +```go +func ToBigInt(v interface{}) (*big.Int, error) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + n := 9876543210 + bigInt, _ := convertor.ToBigInt(n) + + fmt.Println(bigInt) + // Output: + // 9876543210 +} ``` \ No newline at end of file diff --git a/docs/convertor_zh-CN.md b/docs/convertor_zh-CN.md index caa6d6ac..c89b8e54 100644 --- a/docs/convertor_zh-CN.md +++ b/docs/convertor_zh-CN.md @@ -45,6 +45,7 @@ import ( - [ToUrlBase64](#ToUrlBase64) - [ToRawStdBase64](#ToRawStdBase64) - [ToRawUrlBase64](#ToRawUrlBase64) +- [ToBigInt](#ToBigInt)
@@ -453,7 +454,7 @@ func main() { 函数签名: ```go -func EncodeByte(data any) ([]byte, error) +func EncodeByte(data interface{}) ([]byte, error) ``` 例子: @@ -479,7 +480,7 @@ func main() { 函数签名: ```go -func DecodeByte(data []byte, target any) error +func DecodeByte(data []byte, target interface{}) error ``` 例子: @@ -507,7 +508,7 @@ func main() { 函数签名: ```go -func DeepClone[T any](src T) T +func DeepClone[T interface{}](src T) T ``` 示例: @@ -753,7 +754,7 @@ func main() { 函数签名: ```go -func ToStdBase64(value any) string +func ToStdBase64(value interface{}) string ``` 示例: @@ -763,7 +764,7 @@ package main import ( "fmt" - "github.com/duke-git/lancet/v2/convertor" + "github.com/duke-git/lancet/convertor" ) func main() { @@ -785,7 +786,7 @@ func main() { afterEncode = convertor.ToStdBase64(intVal) fmt.Println(afterEncode) - mapVal := map[string]any{"a": "hi", "b": 2, "c": struct { + mapVal := map[string]interface{}{"a": "hi", "b": 2, "c": struct { A string B int }{"hello", 3}} @@ -825,7 +826,7 @@ func main() { 函数签名: ```go -func ToUrlBase64(value any) string +func ToUrlBase64(value interface{}) string ``` 示例: @@ -835,7 +836,7 @@ package main import ( "fmt" - "github.com/duke-git/lancet/v2/convertor" + "github.com/duke-git/lancet/convertor" ) func main() { @@ -855,7 +856,7 @@ func main() { afterEncode = convertor.ToUrlBase64(intVal) fmt.Println(afterEncode) - mapVal := map[string]any{"a": "hi", "b": 2, "c": struct { + mapVal := map[string]interface{}{"a": "hi", "b": 2, "c": struct { A string B int }{"hello", 3}} @@ -894,7 +895,7 @@ func main() { 函数签名: ```go -func ToRawStdBase64(value any) string +func ToRawStdBase64(value interface{}) string ``` 示例: @@ -904,7 +905,7 @@ package main import ( "fmt" - "github.com/duke-git/lancet/v2/convertor" + "github.com/duke-git/lancet/convertor" ) func main() { @@ -921,7 +922,7 @@ func main() { afterEncode = convertor.ToRawStdBase64(intVal) fmt.Println(afterEncode) - mapVal := map[string]any{"a": "hi", "b": 2, "c": struct { + mapVal := map[string]interface{}{"a": "hi", "b": 2, "c": struct { A string B int }{"hello", 3}} @@ -958,7 +959,7 @@ func main() { 函数签名: ```go -func ToRawUrlBase64(value any) string +func ToRawUrlBase64(value interface{}) string ``` 示例: @@ -968,7 +969,7 @@ package main import ( "fmt" - "github.com/duke-git/lancet/v2/convertor" + "github.com/duke-git/lancet/convertor" ) func main() { @@ -985,7 +986,7 @@ func main() { afterEncode = convertor.ToRawUrlBase64(intVal) fmt.Println(afterEncode) - mapVal := map[string]any{"a": "hi", "b": 2, "c": struct { + mapVal := map[string]interface{}{"a": "hi", "b": 2, "c": struct { A string B int }{"hello", 3}} @@ -1013,4 +1014,34 @@ func main() { // dHJ1ZQ // ZXJy } +``` + +### ToBigInt + +

将整数值转换为bigInt。

+ +函数签名: + +```go +func ToBigInt(v interface{}) (*big.Int, error) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + n := 9876543210 + bigInt, _ := convertor.ToBigInt(n) + + fmt.Println(bigInt) + // Output: + // 9876543210 +} ``` \ No newline at end of file diff --git a/docs/datetime.md b/docs/datetime.md index 55b9b900..0e4ef66c 100644 --- a/docs/datetime.md +++ b/docs/datetime.md @@ -1381,7 +1381,7 @@ package main import ( "fmt" - "github.com/duke-git/lancet/v2/datetime" + "github.com/duke-git/lancet/datetime" ) func main() { diff --git a/docs/fileutil.md b/docs/fileutil.md index 268e95e9..a3d409cb 100644 --- a/docs/fileutil.md +++ b/docs/fileutil.md @@ -902,7 +902,7 @@ package main import ( "fmt" - "github.com/duke-git/lancet/v2/fileutil" + "github.com/duke-git/lancet/fileutil" ) func main() { diff --git a/docs/fileutil_zh-CN.md b/docs/fileutil_zh-CN.md index 1c697dd0..7db9edff 100644 --- a/docs/fileutil_zh-CN.md +++ b/docs/fileutil_zh-CN.md @@ -900,7 +900,7 @@ package main import ( "fmt" - "github.com/duke-git/lancet/v2/fileutil" + "github.com/duke-git/lancet/fileutil" ) func main() { diff --git a/docs/validator.md b/docs/validator.md index d4d506ab..0ad14926 100644 --- a/docs/validator.md +++ b/docs/validator.md @@ -1225,7 +1225,7 @@ func IsMasterCard(v string) bool ```go import ( "fmt" - "github.com/duke-git/lancet/v2/validator" + "github.com/duke-git/lancet/validator" ) func main() { diff --git a/strutil/string.go b/strutil/string.go index 33974c00..870ec8bc 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -360,7 +360,6 @@ func StringToBytes(str string) (b []byte) { } // BytesToString converts a byte slice to string without a memory allocation. -// Play: todo func BytesToString(bytes []byte) string { return *(*string)(unsafe.Pointer(&bytes)) }