From c886ff6b20c5f5fbe49f276d8e427ccd79a26a03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 7 Feb 2023 17:29:29 +0800 Subject: [PATCH 01/29] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=B1=BB=E5=9E=8B=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- file.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/file.go b/file.go index 41c0caf..16a0e79 100644 --- a/file.go +++ b/file.go @@ -9,6 +9,7 @@ package util import ( "io/ioutil" + "net/http" "os" "strings" @@ -156,3 +157,25 @@ func (f *file) IsFileExist(filePath string) (bool, bool) { fileStat, err := os.Stat(filePath) return nil == err || os.IsExist(err), (nil == err || os.IsExist(err)) && !fileStat.IsDir() } + +// GetFileMIMEType 获取本地文件的MIME类型 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:15 2023/2/7 +func (f *file) GetFileMIMEType(filePath string) (string, error) { + var ( + err error + fileHandler *os.File + ) + if fileHandler, err = os.Open(filePath); nil != err { + return "", err + } + // 只需要前 512 个字节就可以了 + buffer := make([]byte, 512) + if _, err = fileHandler.Read(buffer); nil != err { + return "", err + } + contentType := http.DetectContentType(buffer) + return contentType, nil +} From fcdda09503445b0ec6f3f2f020df13ef0ce7f246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 11 Feb 2023 21:14:57 +0800 Subject: [PATCH 02/29] =?UTF-8?q?=E5=A2=9E=E5=8A=A0jwt=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 7 ++++--- go.sum | 8 ++++++++ init.go | 6 +++++- jwt.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 jwt.go diff --git a/go.mod b/go.mod index 0339e13..0b50fc6 100644 --- a/go.mod +++ b/go.mod @@ -5,15 +5,16 @@ go 1.17 require ( github.com/Jeffail/gabs v1.4.0 github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 - github.com/go-ini/ini v1.66.6 + github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/go-ini/ini v1.67.0 github.com/pkg/errors v0.9.1 github.com/spaolacci/murmur3 v1.1.0 - github.com/tidwall/gjson v1.14.1 + github.com/tidwall/gjson v1.14.4 gopkg.in/yaml.v3 v3.0.1 ) require ( github.com/stretchr/testify v1.8.0 // indirect github.com/tidwall/match v1.1.1 // indirect - github.com/tidwall/pretty v1.2.0 // indirect + github.com/tidwall/pretty v1.2.1 // indirect ) diff --git a/go.sum b/go.sum index 0345e8d..5bfea82 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,12 @@ github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIg github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/go-ini/ini v1.66.6 h1:h6k2Bb0HWS/BXXHCXj4QHjxPmlIU4NK+7MuLp9SD+4k= github.com/go-ini/ini v1.66.6/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= +github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= +github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -20,10 +24,14 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo= github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= +github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/init.go b/init.go index 7f78400..1734b5e 100644 --- a/init.go +++ b/init.go @@ -31,7 +31,9 @@ var ( // Calculate ... Calculate *calculate // Project ... - Project = &project{} + Project *project + // JWT ... + JWT *ownJwt ) func init() { @@ -46,4 +48,6 @@ func init() { URL = &ownURL{} Map = &ownMap{} Calculate = &calculate{} + Project = &project{} + JWT = &ownJwt{} } diff --git a/jwt.go b/jwt.go new file mode 100644 index 0000000..2a88609 --- /dev/null +++ b/jwt.go @@ -0,0 +1,64 @@ +// Package util ... +// +// Description : go-jwt具体实现 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-02-11 20:09 +package util + +import ( + "errors" + + "github.com/dgrijalva/jwt-go" +) + +type ownJwt struct { +} + +type MyClaims struct { + jwt.StandardClaims +} + +// Generate 生成 token +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 20:13 2023/2/11 +func (j *ownJwt) Generate(signMethod jwt.SigningMethod, secret string, claims jwt.Claims) (string, error) { + if nil == signMethod { + signMethod = jwt.SigningMethodHS256 + } + var ( + token *jwt.Token + ) + + if nil == claims { + token = jwt.New(signMethod) + } else { + token = jwt.NewWithClaims(signMethod, claims) + } + return token.SignedString(secret) +} + +// Validate 验证token +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 20:13 2023/2/11 +func (j *ownJwt) Validate(inputToken string, secret string) (jwt.Claims, error) { + var ( + token *jwt.Token + err error + ) + + if token, err = jwt.Parse(inputToken, func(tokenInfo *jwt.Token) (interface{}, error) { + return secret, nil + }); nil != err { + return nil, errors.New("token parse fail : " + err.Error()) + } + if !token.Valid { + return nil, errors.New("token is valid : " + err.Error()) + } + return token.Claims, nil +} From cccfc05ea7b8bbd95dd21fb3e0ec98228fc22608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 12 Feb 2023 00:00:50 +0800 Subject: [PATCH 03/29] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20jwt=20secret=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jwt.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jwt.go b/jwt.go index 2a88609..39f0160 100644 --- a/jwt.go +++ b/jwt.go @@ -38,7 +38,7 @@ func (j *ownJwt) Generate(signMethod jwt.SigningMethod, secret string, claims jw } else { token = jwt.NewWithClaims(signMethod, claims) } - return token.SignedString(secret) + return token.SignedString([]byte(secret)) } // Validate 验证token @@ -53,7 +53,7 @@ func (j *ownJwt) Validate(inputToken string, secret string) (jwt.Claims, error) ) if token, err = jwt.Parse(inputToken, func(tokenInfo *jwt.Token) (interface{}, error) { - return secret, nil + return []byte(secret), nil }); nil != err { return nil, errors.New("token parse fail : " + err.Error()) } From 661eed5f0af8649561c4a1cc69da7837be78de8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 12 Feb 2023 00:06:39 +0800 Subject: [PATCH 04/29] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=A7=92=E7=BA=A7?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3=E8=BD=AC=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- time.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/time.go b/time.go index 69741bc..d57abb0 100644 --- a/time.go +++ b/time.go @@ -46,7 +46,6 @@ func (ot *ownTime) FormatUnixNano(timestamp int64) string { // // Date : 11:55 2022/7/14 func (ot *ownTime) FormatUnixMilli(timestamp int64) string { - time.Now().UnixMilli() return time.UnixMilli(timestamp).Format("2006-01-02 15:04:05.000") } @@ -56,7 +55,7 @@ func (ot *ownTime) FormatUnixMilli(timestamp int64) string { // // Date : 12:06 2022/7/14 func (ot *ownTime) FormatUnixSec(timestamp int64) string { - return time.UnixMilli(timestamp).Format("2006-01-02 15:04:05") + return time.Unix(timestamp, 0).Format("2006-01-02 15:04:05") } // ParseISO8601Time 解析 2006-01-02T15:04:05+08:00 格式时间 -> 2006-01-02 15:04:05 From 1a6a3e1d3197edbac9ef75f8c7e63eb09f067d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 12 Feb 2023 00:36:24 +0800 Subject: [PATCH 05/29] =?UTF-8?q?=E4=BC=98=E5=8C=96jwt=20token=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jwt.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/jwt.go b/jwt.go index 39f0160..6920141 100644 --- a/jwt.go +++ b/jwt.go @@ -46,7 +46,10 @@ func (j *ownJwt) Generate(signMethod jwt.SigningMethod, secret string, claims jw // Author : go_developer@163.com<白茶清欢> // // Date : 20:13 2023/2/11 -func (j *ownJwt) Validate(inputToken string, secret string) (jwt.Claims, error) { +func (j *ownJwt) Validate(inputToken string, secret string, convert func(c jwt.Claims) error) error { + if nil == convert { + return errors.New("convert func is nil") + } var ( token *jwt.Token err error @@ -55,10 +58,15 @@ func (j *ownJwt) Validate(inputToken string, secret string) (jwt.Claims, error) if token, err = jwt.Parse(inputToken, func(tokenInfo *jwt.Token) (interface{}, error) { return []byte(secret), nil }); nil != err { - return nil, errors.New("token parse fail : " + err.Error()) + return errors.New("token parse fail : " + err.Error()) } if !token.Valid { - return nil, errors.New("token is valid : " + err.Error()) + return errors.New("token is valid : " + err.Error()) } - return token.Claims, nil + if nil != convert { + if err = convert(token.Claims); nil != err { + return err + } + } + return nil } From 2560949681515f7420ee559b738432ba86477d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 12 Feb 2023 00:42:27 +0800 Subject: [PATCH 06/29] =?UTF-8?q?=E4=BC=98=E5=8C=96jwt=20token=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jwt.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/jwt.go b/jwt.go index 6920141..5a03731 100644 --- a/jwt.go +++ b/jwt.go @@ -46,27 +46,21 @@ func (j *ownJwt) Generate(signMethod jwt.SigningMethod, secret string, claims jw // Author : go_developer@163.com<白茶清欢> // // Date : 20:13 2023/2/11 -func (j *ownJwt) Validate(inputToken string, secret string, convert func(c jwt.Claims) error) error { - if nil == convert { - return errors.New("convert func is nil") - } +func (j *ownJwt) Validate(inputToken string, secret string, claims jwt.Claims) (jwt.Claims, error) { + var ( token *jwt.Token err error ) - if token, err = jwt.Parse(inputToken, func(tokenInfo *jwt.Token) (interface{}, error) { + if token, err = jwt.ParseWithClaims(inputToken, claims, func(tokenInfo *jwt.Token) (interface{}, error) { return []byte(secret), nil }); nil != err { - return errors.New("token parse fail : " + err.Error()) + return nil, errors.New("token parse fail : " + err.Error()) } if !token.Valid { - return errors.New("token is valid : " + err.Error()) + return nil, errors.New("token is valid : " + err.Error()) } - if nil != convert { - if err = convert(token.Claims); nil != err { - return err - } - } - return nil + + return token.Claims, nil } From 6710e345ccec831d13147a7cb8b0240657d8c207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 19 Mar 2023 14:13:11 +0800 Subject: [PATCH 07/29] =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E5=A2=9E=E5=8A=A0=E6=97=B6=E5=8C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- time.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/time.go b/time.go index d57abb0..bd3ba82 100644 --- a/time.go +++ b/time.go @@ -26,7 +26,7 @@ type ownTime struct { // // Date : 1:34 上午 2021/10/7 func (ot *ownTime) GetCurrentFormatTime() string { - return time.Now().Format("2006-01-02 15:04:05") + return time.Now().In(time.Local).Format("2006-01-02 15:04:05") } // FormatUnixNano 格式化纳秒时间戳 @@ -46,7 +46,7 @@ func (ot *ownTime) FormatUnixNano(timestamp int64) string { // // Date : 11:55 2022/7/14 func (ot *ownTime) FormatUnixMilli(timestamp int64) string { - return time.UnixMilli(timestamp).Format("2006-01-02 15:04:05.000") + return time.UnixMilli(timestamp).In(time.Local).Format("2006-01-02 15:04:05.000") } // FormatUnixSec ... @@ -55,7 +55,7 @@ func (ot *ownTime) FormatUnixMilli(timestamp int64) string { // // Date : 12:06 2022/7/14 func (ot *ownTime) FormatUnixSec(timestamp int64) string { - return time.Unix(timestamp, 0).Format("2006-01-02 15:04:05") + return time.Unix(timestamp, 0).In(time.Local).Format("2006-01-02 15:04:05") } // ParseISO8601Time 解析 2006-01-02T15:04:05+08:00 格式时间 -> 2006-01-02 15:04:05 @@ -74,5 +74,5 @@ func (ot *ownTime) ParseISO8601Time(parseTime string) string { return "" } - return formatTime.Format("2006-01-02 15:04:05") + return formatTime.In(time.Local).Format("2006-01-02 15:04:05") } From 6cee12a850b801ed4325c40fb4d0e825916455b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 30 Mar 2023 14:49:38 +0800 Subject: [PATCH 08/29] =?UTF-8?q?=E5=A2=9E=E5=8A=A0in=20array=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- array.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ array_test.go | 20 ++++++++++++++++++++ init.go | 6 +++++- 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 array.go create mode 100644 array_test.go diff --git a/array.go b/array.go new file mode 100644 index 0000000..f1ef8f5 --- /dev/null +++ b/array.go @@ -0,0 +1,50 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-03-30 14:35 +package util + +import ( + "fmt" + "reflect" +) + +type array struct { +} + +// In 判断指定数据是否在目标数据集中, 不存在返回 -1 , 存在时返回数据对应的索引 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:38 2023/3/30 +func (a *array) In(search interface{}, source interface{}) int { + if nil == source { + return -1 + } + list, ok := source.([]interface{}) + if !ok { + return -1 + } + if nil == search { + for idx, item := range list { + if item == nil { + return idx + } + } + return -1 + } + searchType := reflect.TypeOf(search).Kind() + for idx, item := range list { + if searchType != reflect.TypeOf(item).Kind() { + // 类型不同 + continue + } + if fmt.Sprintf("%v", search) == fmt.Sprintf("%v", item) { + return idx + } + } + return -1 +} diff --git a/array_test.go b/array_test.go new file mode 100644 index 0000000..dd4d604 --- /dev/null +++ b/array_test.go @@ -0,0 +1,20 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-03-30 14:43 +package util + +import ( + "fmt" + "testing" +) + +func Test_array_In(t *testing.T) { + fmt.Println(Array.In(1234, []string{"1234", "123", "1"})) + fmt.Println(Array.In(1234, []interface{}{1234, "123", "1"})) + fmt.Println(Array.In(nil, []interface{}{1234, "123", "1"})) + fmt.Println(Array.In(nil, []interface{}{1234, "123", "1", nil})) +} diff --git a/init.go b/init.go index 7f78400..50a808a 100644 --- a/init.go +++ b/init.go @@ -31,7 +31,9 @@ var ( // Calculate ... Calculate *calculate // Project ... - Project = &project{} + Project *project + // 数组操作 + Array *array ) func init() { @@ -46,4 +48,6 @@ func init() { URL = &ownURL{} Map = &ownMap{} Calculate = &calculate{} + Project = &project{} + Array = &array{} } From 8aee2fb9076f0fcce25d598e4c78596d8f92a06a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 30 Mar 2023 16:22:56 +0800 Subject: [PATCH 09/29] =?UTF-8?q?in=5Farray=E4=BC=98=E5=8C=96=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- array.go | 31 +++++++++++++++++++++++++------ array_test.go | 11 ++++++----- go.mod | 4 +++- go.sum | 5 ----- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/array.go b/array.go index f1ef8f5..0bfc59c 100644 --- a/array.go +++ b/array.go @@ -8,8 +8,10 @@ package util import ( + "encoding/json" "fmt" "reflect" + "strings" ) type array struct { @@ -24,12 +26,20 @@ func (a *array) In(search interface{}, source interface{}) int { if nil == source { return -1 } - list, ok := source.([]interface{}) - if !ok { + sourceType := reflect.TypeOf(source).Kind() + if sourceType != reflect.Array && sourceType != reflect.Slice { return -1 } + + byteData, _ := json.Marshal(source) + var ( + formatSourceData []interface{} + ) + + _ = JSON.UnmarshalWithNumber(byteData, &formatSourceData) + if nil == search { - for idx, item := range list { + for idx, item := range formatSourceData { if item == nil { return idx } @@ -37,9 +47,18 @@ func (a *array) In(search interface{}, source interface{}) int { return -1 } searchType := reflect.TypeOf(search).Kind() - for idx, item := range list { - if searchType != reflect.TypeOf(item).Kind() { - // 类型不同 + for idx, item := range formatSourceData { + itemKind := reflect.TypeOf(item).Kind() + if searchType != itemKind { + // 类型不同, 检测是否为数字 + if strings.Contains(searchType.String(), "int") || strings.Contains(searchType.String(), "float") { + // 查询的是数字 + if _, ok := item.(json.Number); ok { + if fmt.Sprintf("%v", search) == fmt.Sprintf("%v", item) { + return idx + } + } + } continue } if fmt.Sprintf("%v", search) == fmt.Sprintf("%v", item) { diff --git a/array_test.go b/array_test.go index dd4d604..8cb0110 100644 --- a/array_test.go +++ b/array_test.go @@ -8,13 +8,14 @@ package util import ( - "fmt" "testing" + + "github.com/stretchr/testify/assert" ) func Test_array_In(t *testing.T) { - fmt.Println(Array.In(1234, []string{"1234", "123", "1"})) - fmt.Println(Array.In(1234, []interface{}{1234, "123", "1"})) - fmt.Println(Array.In(nil, []interface{}{1234, "123", "1"})) - fmt.Println(Array.In(nil, []interface{}{1234, "123", "1", nil})) + //assert.Equal(t, -1, Array.In(1234, []string{"1234", "123", "1"})) + assert.Equal(t, 0, Array.In(1234, []interface{}{1234, "123", "1"})) + //assert.Equal(t, -1, Array.In(nil, []interface{}{1234, "123", "1"})) + //assert.Equal(t, 3, Array.In(nil, []interface{}{1234, "123", "1", nil})) } diff --git a/go.mod b/go.mod index 0b50fc6..8a31b63 100644 --- a/go.mod +++ b/go.mod @@ -9,12 +9,14 @@ require ( github.com/go-ini/ini v1.67.0 github.com/pkg/errors v0.9.1 github.com/spaolacci/murmur3 v1.1.0 + github.com/stretchr/testify v1.8.0 github.com/tidwall/gjson v1.14.4 gopkg.in/yaml.v3 v3.0.1 ) require ( - github.com/stretchr/testify v1.8.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect ) diff --git a/go.sum b/go.sum index 5bfea82..b303a87 100644 --- a/go.sum +++ b/go.sum @@ -7,8 +7,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= -github.com/go-ini/ini v1.66.6 h1:h6k2Bb0HWS/BXXHCXj4QHjxPmlIU4NK+7MuLp9SD+4k= -github.com/go-ini/ini v1.66.6/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -22,13 +20,10 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo= -github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= -github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= From 662152cb682dd0fc92f413660cb654127ce09739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 30 Mar 2023 16:26:19 +0800 Subject: [PATCH 10/29] =?UTF-8?q?=E5=A2=9E=E5=8A=A0in=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E7=9A=84=E7=AE=80=E5=8C=96=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- array.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/array.go b/array.go index 0bfc59c..91c751b 100644 --- a/array.go +++ b/array.go @@ -67,3 +67,12 @@ func (a *array) In(search interface{}, source interface{}) int { } return -1 } + +// InAny In 函数的简化写法 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:25 2023/3/30 +func (a *array) InAny(search interface{}, source ...interface{}) int { + return a.In(search, source) +} From b5add69124eb10d354b5657ffd96bbcbdc28b0a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 31 Mar 2023 18:58:14 +0800 Subject: [PATCH 11/29] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E9=9A=8F=E6=9C=BAUA=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- init.go | 5 +++ user_agent.go | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 user_agent.go diff --git a/init.go b/init.go index 9c79a13..47f8d9a 100644 --- a/init.go +++ b/init.go @@ -36,6 +36,8 @@ var ( Array *array // JWT ... JWT *ownJwt + // UserAgent ... + UserAgent *userAgent ) func init() { @@ -53,4 +55,7 @@ func init() { Project = &project{} Array = &array{} JWT = &ownJwt{} + UserAgent = &userAgent{ + list: defaultUserAgentList, + } } diff --git a/user_agent.go b/user_agent.go new file mode 100644 index 0000000..c5aebf9 --- /dev/null +++ b/user_agent.go @@ -0,0 +1,100 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-03-31 18:51 +package util + +import ( + "math/rand" + "time" +) + +var defaultUserAgentList = []string{ + "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6", + "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER", + "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11", + "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11", + "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; 360SE)", + "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20", + "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6", + "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1", + "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)", + "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12", + "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1", + "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.3 Mobile/14E277 Safari/603.1.30", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1", + "(KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1", + "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11", + "(KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6", + "(KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6", + "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6", + "(KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6", + "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1", + "(KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1", + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5", + "(KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5", + "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5", + "(KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", + "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", + "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", + "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", + "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", + "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3", + "(KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3", + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24", + "(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24", + "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24", + "(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24", +} + +type userAgent struct { + list []string +} + +// Set 设置ua池 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:55 2023/3/31 +func (ua *userAgent) Set(agentList []string) { + if len(agentList) == 0 { + return + } + ua.list = agentList +} + +// GetRandomUA 随机获取UA +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:56 2023/3/31 +func (ua *userAgent) GetRandomUA() string { + rand.Seed(time.Now().UnixNano()) + idx := rand.Intn(len(ua.list)) + return ua.list[idx] +} From 5114db00c66cd8c9b1b2bc7ca9536517419743ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 4 Apr 2023 16:26:02 +0800 Subject: [PATCH 12/29] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=8F=B0=E6=95=B0=E6=8D=AE=E8=BE=93=E5=87=BA=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- console.go | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ console_test.go | 23 +++++++++++++ init.go | 3 ++ 3 files changed, 116 insertions(+) create mode 100644 console.go create mode 100644 console_test.go diff --git a/console.go b/console.go new file mode 100644 index 0000000..620f999 --- /dev/null +++ b/console.go @@ -0,0 +1,90 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-04-04 16:09 +package util + +import ( + "encoding/json" + "fmt" + "reflect" +) + +// console 终端数据输出的一些操作 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:10 2023/4/4 +type console struct { + enable bool +} + +// Enable 启用 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:10 2023/4/4 +func (c *console) Enable() { + c.enable = true +} + +// Disable 禁用 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:11 2023/4/4 +func (c *console) Disable() { + c.enable = false +} + +// Output 禁用终端输出 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:13 2023/4/4 +func (c *console) Output(dataList ...interface{}) { + if !c.enable { + return + } + list := make([]interface{}, 0) + for _, item := range dataList { + list = append(list, c.getDataStr(item)) + } + fmt.Println(list...) +} + +// getDataStr 数据转换为字符串 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:14 2023/4/4 +func (c *console) getDataStr(data interface{}) string { + if nil == data { + return "nil" + } + dataValue := reflect.ValueOf(data) + dataType := reflect.TypeOf(data).Kind() + if dataType == reflect.Ptr { + dataValue = dataValue.Elem() + } + + switch dataValue.Kind() { + case reflect.Map: + fallthrough + case reflect.Slice: + fallthrough + case reflect.Array: + fallthrough + case reflect.Struct: + byteData, _ := json.Marshal(data) + return string(byteData) + case reflect.Func: + return dataValue.String() + default: + return fmt.Sprintf("%v", data) + } + return "" +} diff --git a/console_test.go b/console_test.go new file mode 100644 index 0000000..732ead1 --- /dev/null +++ b/console_test.go @@ -0,0 +1,23 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-04-04 16:21 +package util + +import "testing" + +func Test_console_Output(t *testing.T) { + Console.Enable() + var a = map[string]interface{}{ + "name": "baicha", + } + type U struct { + Name string + } + b := &U{Name: "qinghuan"} + c := U{Name: "test"} + Console.Output(a, b, c) +} diff --git a/init.go b/init.go index 47f8d9a..37c36a2 100644 --- a/init.go +++ b/init.go @@ -38,6 +38,8 @@ var ( JWT *ownJwt // UserAgent ... UserAgent *userAgent + // Console 控制台输出数据 + Console *console ) func init() { @@ -58,4 +60,5 @@ func init() { UserAgent = &userAgent{ list: defaultUserAgentList, } + Console = &console{} } From 33ce7169d0c8a67a4401bee94015ed60252a00aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 4 Apr 2023 16:30:40 +0800 Subject: [PATCH 13/29] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E5=8F=B0=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- console.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/console.go b/console.go index 620f999..8e1c1a2 100644 --- a/console.go +++ b/console.go @@ -8,6 +8,7 @@ package util import ( + "bytes" "encoding/json" "fmt" "reflect" @@ -49,11 +50,9 @@ func (c *console) Output(dataList ...interface{}) { if !c.enable { return } - list := make([]interface{}, 0) - for _, item := range dataList { - list = append(list, c.getDataStr(item)) + for idx, item := range dataList { + fmt.Println(fmt.Sprintf("%v --> %v", idx, c.getDataStr(item))) } - fmt.Println(list...) } // getDataStr 数据转换为字符串 @@ -79,12 +78,13 @@ func (c *console) getDataStr(data interface{}) string { case reflect.Array: fallthrough case reflect.Struct: + var out bytes.Buffer byteData, _ := json.Marshal(data) - return string(byteData) + _ = json.Indent(&out, []byte(string(byteData)+"\n"), "", "\t") + return string(out.Bytes()) case reflect.Func: return dataValue.String() default: return fmt.Sprintf("%v", data) } - return "" } From 32f50cce5bc3969fdb498309b9f37aa8b5a7def3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 4 Apr 2023 16:37:03 +0800 Subject: [PATCH 14/29] =?UTF-8?q?=E4=BC=98=E5=8C=96JSON=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- console.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/console.go b/console.go index 8e1c1a2..e6faafd 100644 --- a/console.go +++ b/console.go @@ -20,7 +20,8 @@ import ( // // Date : 16:10 2023/4/4 type console struct { - enable bool + enable bool + formatJson bool } // Enable 启用 @@ -41,6 +42,15 @@ func (c *console) Disable() { c.enable = false } +// FormatJson 是否格式化json +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:35 2023/4/4 +func (c *console) FormatJson(format bool) { + c.formatJson = format +} + // Output 禁用终端输出 // // Author : go_developer@163.com<白茶清欢> @@ -50,8 +60,8 @@ func (c *console) Output(dataList ...interface{}) { if !c.enable { return } - for idx, item := range dataList { - fmt.Println(fmt.Sprintf("%v --> %v", idx, c.getDataStr(item))) + for _, item := range dataList { + fmt.Println(c.getDataStr(item)) } } @@ -78,8 +88,11 @@ func (c *console) getDataStr(data interface{}) string { case reflect.Array: fallthrough case reflect.Struct: - var out bytes.Buffer byteData, _ := json.Marshal(data) + if !c.formatJson { + return string(byteData) + } + var out bytes.Buffer _ = json.Indent(&out, []byte(string(byteData)+"\n"), "", "\t") return string(out.Bytes()) case reflect.Func: From 812faa2e21979b7cf3f149d9c3e093fc52f84876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 4 Apr 2023 17:01:46 +0800 Subject: [PATCH 15/29] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B8=B8=E8=A7=81?= =?UTF-8?q?=E7=9A=84=E4=B8=AD=E6=96=87=E5=AD=97=E7=AC=A6=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E6=88=90=E8=8B=B1=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- string.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/string.go b/string.go index d62cb1a..de97f97 100644 --- a/string.go +++ b/string.go @@ -172,3 +172,24 @@ func (s *stringOperate) ClearChar(src string, charList ...string) string { } return src } + +// ReplaceChineseChar 替换常见的中文符号 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:59 2023/4/4 +func (s *stringOperate) ReplaceChineseChar(str string) string { + charTable := map[string]string{ + "(": "(", + ")": ")", + ":": ":", + ",": ",", + "。": ".", + "【": "]", + "】": "]", + } + for k, v := range charTable { + str = strings.ReplaceAll(str, k, v) + } + return str +} From f5fddb92e850320506308160c09fa72d47e6c485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 8 Apr 2023 14:38:13 +0800 Subject: [PATCH 16/29] =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BC=A0=E5=85=A5layout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- time.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/time.go b/time.go index bd3ba82..876da61 100644 --- a/time.go +++ b/time.go @@ -18,6 +18,7 @@ import ( // // Date : 15:24 2022/5/14 type ownTime struct { + format string } // GetCurrentFormatTime 获取当前时间的格式化时间(秒) @@ -34,10 +35,13 @@ func (ot *ownTime) GetCurrentFormatTime() string { // Author : go_developer@163.com<白茶清欢> // // Date : 11:54 2022/7/14 -func (ot *ownTime) FormatUnixNano(timestamp int64) string { +func (ot *ownTime) FormatUnixNano(timestamp int64, layout ...string) string { + if len(layout) == 0 { + layout = []string{"2006-01-02 15:04:05.000"} + } nano := timestamp % 1e6 milli := timestamp / 1e6 - return ot.FormatUnixMilli(milli) + fmt.Sprintf(" %v", nano) + return ot.FormatUnixMilli(milli, layout...) + fmt.Sprintf(" %v", nano) } // FormatUnixMilli 格式化毫秒时间戳 @@ -45,8 +49,11 @@ func (ot *ownTime) FormatUnixNano(timestamp int64) string { // Author : go_developer@163.com<白茶清欢> // // Date : 11:55 2022/7/14 -func (ot *ownTime) FormatUnixMilli(timestamp int64) string { - return time.UnixMilli(timestamp).In(time.Local).Format("2006-01-02 15:04:05.000") +func (ot *ownTime) FormatUnixMilli(timestamp int64, layout ...string) string { + if len(layout) == 0 { + layout = []string{"2006-01-02 15:04:05.000"} + } + return time.UnixMilli(timestamp).In(time.Local).Format(layout[0]) } // FormatUnixSec ... @@ -54,8 +61,11 @@ func (ot *ownTime) FormatUnixMilli(timestamp int64) string { // Author : go_developer@163.com<白茶清欢> // // Date : 12:06 2022/7/14 -func (ot *ownTime) FormatUnixSec(timestamp int64) string { - return time.Unix(timestamp, 0).In(time.Local).Format("2006-01-02 15:04:05") +func (ot *ownTime) FormatUnixSec(timestamp int64, layout ...string) string { + if len(layout) == 0 { + layout = []string{"2006-01-02 15:04:05"} + } + return time.Unix(timestamp, 0).In(time.Local).Format(layout[0]) } // ParseISO8601Time 解析 2006-01-02T15:04:05+08:00 格式时间 -> 2006-01-02 15:04:05 From 82a530d77903777a53a09928d21098d5e21a431b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 8 Apr 2023 14:48:53 +0800 Subject: [PATCH 17/29] =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BC=A0=E5=85=A5layout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- time.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/time.go b/time.go index 876da61..ac11065 100644 --- a/time.go +++ b/time.go @@ -26,8 +26,11 @@ type ownTime struct { // Author : go_developer@163.com<白茶清欢> // // Date : 1:34 上午 2021/10/7 -func (ot *ownTime) GetCurrentFormatTime() string { - return time.Now().In(time.Local).Format("2006-01-02 15:04:05") +func (ot *ownTime) GetCurrentFormatTime(layout ...string) string { + if len(layout) == 0 { + layout = []string{"2006-01-02 15:04:05"} + } + return time.Now().In(time.Local).Format(layout[0]) } // FormatUnixNano 格式化纳秒时间戳 From 5cfbbbb8328c53d29ee3cce519c994ab666d9ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 9 Apr 2023 02:37:42 +0800 Subject: [PATCH 18/29] =?UTF-8?q?=E5=8D=87=E7=BA=A7go=E7=89=88=E6=9C=ACgo1?= =?UTF-8?q?.17=20->=20go1.20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 ++-- go.sum | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 8a31b63..72d1a6a 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module git.zhangdeman.cn/zhangdeman/util -go 1.17 +go 1.20 require ( github.com/Jeffail/gabs v1.4.0 @@ -9,7 +9,7 @@ require ( github.com/go-ini/ini v1.67.0 github.com/pkg/errors v0.9.1 github.com/spaolacci/murmur3 v1.1.0 - github.com/stretchr/testify v1.8.0 + github.com/stretchr/testify v1.8.2 github.com/tidwall/gjson v1.14.4 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index b303a87..d03dc3e 100644 --- a/go.sum +++ b/go.sum @@ -17,13 +17,16 @@ github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0b github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= From 0a75d06083d15cb6c812a810b2d0c39e7d0c7891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 9 Apr 2023 19:06:07 +0800 Subject: [PATCH 19/29] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E9=BB=98=E8=AE=A4=E6=B5=8F=E8=A7=88=E5=99=A8?= =?UTF-8?q?=E6=89=93=E5=BC=80=E8=BF=9E=E6=8E=A5=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- browser.go | 33 +++++++++++++++++++++++++++++++++ init.go | 9 +++++++++ 2 files changed, 42 insertions(+) create mode 100644 browser.go diff --git a/browser.go b/browser.go new file mode 100644 index 0000000..30c22c8 --- /dev/null +++ b/browser.go @@ -0,0 +1,33 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-04-09 19:02 +package util + +import ( + "fmt" + "os/exec" + "runtime" +) + +type browser struct { + commandTable map[string]string +} + +// Open 使用系统默认浏览器打开链接 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:05 2023/4/9 +func (b *browser) Open(link string) error { + run, ok := b.commandTable[runtime.GOOS] + if !ok { + return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS) + } + + cmd := exec.Command(run, link) + return cmd.Start() +} diff --git a/init.go b/init.go index 37c36a2..3455b98 100644 --- a/init.go +++ b/init.go @@ -40,6 +40,8 @@ var ( UserAgent *userAgent // Console 控制台输出数据 Console *console + // Browser 浏览器操作实例 + Browser *browser ) func init() { @@ -61,4 +63,11 @@ func init() { list: defaultUserAgentList, } Console = &console{} + Browser = &browser{ + commandTable: map[string]string{ + "windows": "start", + "darwin": "open", + "linux": "xdg-open", + }, + } } From 8d9c4231c3ab730da24e839b4645f1dba664bafc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 18 Apr 2023 22:32:57 +0800 Subject: [PATCH 20/29] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=AE=B6=E7=9B=AE=E5=BD=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 ++ project.go | 12 ++++++++++++ 3 files changed, 15 insertions(+) diff --git a/go.mod b/go.mod index 72d1a6a..48eaf33 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect diff --git a/go.sum b/go.sum index d03dc3e..2eaf4f1 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/project.go b/project.go index 8f1c610..edf49f0 100644 --- a/project.go +++ b/project.go @@ -12,6 +12,8 @@ import ( "path/filepath" "runtime" "strings" + + "github.com/mitchellh/go-homedir" ) type project struct { @@ -88,3 +90,13 @@ func (p *project) GetProjectPath() string { dir, _ := os.Getwd() return dir } + +// GetHomeDir 获取家目录 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 22:31 2023/4/18 +func (p *project) GetHomeDir() string { + homePath, _ := homedir.Dir() + return homePath +} From 0054da0620a5c0124f230a2ac47af267e12547d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 20 Apr 2023 16:36:09 +0800 Subject: [PATCH 21/29] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ua=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 ++ user_agent.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/go.mod b/go.mod index 72d1a6a..a57b65e 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/mssola/user_agent v0.6.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect diff --git a/go.sum b/go.sum index d03dc3e..4eb137e 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= +github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4= +github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/user_agent.go b/user_agent.go index c5aebf9..4873f89 100644 --- a/user_agent.go +++ b/user_agent.go @@ -10,6 +10,8 @@ package util import ( "math/rand" "time" + + "github.com/mssola/user_agent" ) var defaultUserAgentList = []string{ @@ -98,3 +100,77 @@ func (ua *userAgent) GetRandomUA() string { idx := rand.Intn(len(ua.list)) return ua.list[idx] } + +// Parse 解析UA +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:31 2023/4/20 +func (ua *userAgent) Parse(inputUA string) *UAInfo { + uaInstance := user_agent.New(inputUA) + uaInfo := &UAInfo{ + Mozilla: uaInstance.Mozilla(), + Platform: uaInstance.Platform(), + OS: uaInstance.OS(), + Localization: uaInstance.Localization(), + Model: uaInstance.Model(), + Browser: &BrowserInfo{ + Engine: "", + EngineVersion: "", + Name: "", + Version: "", + }, + OSInfo: &OSInfo{ + FullName: uaInstance.OSInfo().FullName, + Name: uaInstance.OSInfo().Name, + Version: uaInstance.OSInfo().Version, + }, + Bot: false, + Mobile: false, + Undecided: false, + } + uaInfo.Browser.Engine, uaInfo.Browser.EngineVersion = uaInstance.Engine() + uaInfo.Browser.Name, uaInfo.Browser.Version = uaInstance.Browser() + return uaInfo +} + +// UAInfo ua解析结果 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:34 2023/4/20 +type UAInfo struct { + Mozilla string `json:"mozilla"` + Platform string `json:"platform"` + OS string `json:"os"` + OSInfo *OSInfo `json:"os_info"` + Localization string `json:"localization"` + Model string `json:"model"` + Browser *BrowserInfo `json:"browser"` + Bot bool `json:"bot"` + Mobile bool `json:"mobile"` + Undecided bool `json:"undecided"` +} + +// BrowserInfo 浏览器信息 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:34 2023/4/20 +type BrowserInfo struct { + Engine string `json:"engine"` // 浏览器引擎 + EngineVersion string `json:"engine_version"` // 浏览器引擎版本 + Name string `json:"name"` // 浏览器名称 + Version string `json:"version"` // 浏览器版本 +} + +// OSInfo 系统信息 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:34 2023/4/20 +type OSInfo struct { + FullName string `json:"full_name"` // 操作系统全称 + Name string `json:"name"` // 操作系统名称 + Version string `json:"version"` // 操作系统版本 +} From 8dbec65861cda0296f01ae765203d27152705e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 4 May 2023 18:40:25 +0800 Subject: [PATCH 22/29] =?UTF-8?q?=E8=A7=84=E5=88=92string=E8=BD=AC?= =?UTF-8?q?=E4=BB=BB=E6=84=8F=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- init.go | 4 ++ string_convert.go | 169 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 string_convert.go diff --git a/init.go b/init.go index 3455b98..37e40a3 100644 --- a/init.go +++ b/init.go @@ -42,6 +42,8 @@ var ( Console *console // Browser 浏览器操作实例 Browser *browser + // StringConvert 字符串转为任意类型 + StringConvert *stringConvert ) func init() { @@ -70,4 +72,6 @@ func init() { "linux": "xdg-open", }, } + // StringConvert + StringConvert = &stringConvert{} } diff --git a/string_convert.go b/string_convert.go new file mode 100644 index 0000000..b6774f8 --- /dev/null +++ b/string_convert.go @@ -0,0 +1,169 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-05-04 18:25 +package util + +import ( + "github.com/pkg/errors" +) + +type stringConvert struct { +} + +// ToFloat32 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:26 2023/5/4 +func (sc *stringConvert) ToFloat32(str string) (float32, error) { + return 0, nil +} + +// ToFloat64 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:26 2023/5/4 +func (sc *stringConvert) ToFloat64(str string) (float64, error) { + return 0, nil +} + +// ToDouble 转double +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:33 2023/5/4 +func (sc *stringConvert) ToDouble(str string) (float64, error) { + return sc.ToFloat64(str) +} + +// ToNumber 转数字, 使用最高精度的float64 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:33 2023/5/4 +func (sc *stringConvert) ToNumber(str string) (float64, error) { + return sc.ToFloat64(str) +} + +// ToInt8 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToInt8(str string) (int8, error) { + return 0, nil +} + +// ToInt16 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToInt16(str string) (int16, error) { + return 0, nil +} + +// ToInt32 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToInt32(str string) (int32, error) { + return 0, nil +} + +// ToInt64 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToInt64(str string) (int64, error) { + return 0, nil +} + +// ToUint8 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:30 2023/5/4 +func (sc *stringConvert) ToUint8(str string) (uint8, error) { + return 0, nil +} + +// ToUint16 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToUint16(str string) (uint16, error) { + return 0, nil +} + +// ToUint32 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToUint32(str string) (uint32, error) { + return 0, nil +} + +// ToUint64 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToUint64(str string) (uint64, error) { + return 0, nil +} + +// ToUint ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToUint(str string) (uint, error) { + return 0, nil +} + +// ToBool ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:34 2023/5/4 +func (sc *stringConvert) ToBool(str string) (bool, error) { + return false, nil +} + +// ToObject ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:35 2023/5/4 +func (sc *stringConvert) ToObject(str string) (map[string]interface{}, error) { + var ( + err error + result map[string]interface{} + ) + if err = JSON.UnmarshalWithNumber([]byte(str), &result); nil != err { + return nil, err + } + return result, nil +} + +// ToStruct ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:38 2023/5/4 +func (sc *stringConvert) ToStruct(str string, receiver interface{}) error { + if nil == receiver { + return errors.New("receiver is nil") + } + return JSON.UnmarshalWithNumber([]byte(str), receiver) +} From 0c884c1f65920f90e06ca17b61ff010ceb16c24b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 4 May 2023 19:06:10 +0800 Subject: [PATCH 23/29] =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E8=BD=ACint?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E7=9A=84slice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- string_convert.go | 156 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/string_convert.go b/string_convert.go index b6774f8..b270bc9 100644 --- a/string_convert.go +++ b/string_convert.go @@ -8,6 +8,8 @@ package util import ( + "strings" + "github.com/pkg/errors" ) @@ -86,6 +88,10 @@ func (sc *stringConvert) ToInt64(str string) (int64, error) { return 0, nil } +func (sc *stringConvert) ToInt(str string) (int, error) { + return 0, nil +} + // ToUint8 ... // // Author : go_developer@163.com<白茶清欢> @@ -167,3 +173,153 @@ func (sc *stringConvert) ToStruct(str string, receiver interface{}) error { } return JSON.UnmarshalWithNumber([]byte(str), receiver) } + +// ToInt8Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:55 2023/5/4 +func (sc *stringConvert) ToInt8Slice(str string, splitChar ...string) ([]int8, error) { + var ( + err error + res []int8 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]int8, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToInt8(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToInt16Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:01 2023/5/4 +func (sc *stringConvert) ToInt16Slice(str string, splitChar ...string) ([]int16, error) { + var ( + err error + res []int16 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]int16, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToInt16(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToInt32Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:03 2023/5/4 +func (sc *stringConvert) ToInt32Slice(str string, splitChar ...string) ([]int32, error) { + var ( + err error + res []int32 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]int32, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToInt32(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToInt64Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:04 2023/5/4 +func (sc *stringConvert) ToInt64Slice(str string, splitChar ...string) ([]int64, error) { + var ( + err error + res []int64 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]int64, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToInt64(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToIntSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:04 2023/5/4 +func (sc *stringConvert) ToIntSlice(str string, splitChar ...string) ([]int, error) { + var ( + err error + res []int + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]int, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToInt(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} From 6741dcf03374eb585e38f1e48d0e13ebda070139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 5 May 2023 10:48:55 +0800 Subject: [PATCH 24/29] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- string_convert.go | 96 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 13 deletions(-) diff --git a/string_convert.go b/string_convert.go index b270bc9..767b26e 100644 --- a/string_convert.go +++ b/string_convert.go @@ -22,7 +22,12 @@ type stringConvert struct { // // Date : 18:26 2023/5/4 func (sc *stringConvert) ToFloat32(str string) (float32, error) { - return 0, nil + var ( + err error + res float32 + ) + err = ConvertAssign(&res, str) + return res, err } // ToFloat64 ... @@ -31,7 +36,12 @@ func (sc *stringConvert) ToFloat32(str string) (float32, error) { // // Date : 18:26 2023/5/4 func (sc *stringConvert) ToFloat64(str string) (float64, error) { - return 0, nil + var ( + err error + res float64 + ) + err = ConvertAssign(&res, str) + return res, err } // ToDouble 转double @@ -58,7 +68,12 @@ func (sc *stringConvert) ToNumber(str string) (float64, error) { // // Date : 18:29 2023/5/4 func (sc *stringConvert) ToInt8(str string) (int8, error) { - return 0, nil + var ( + err error + res int8 + ) + err = ConvertAssign(&res, str) + return res, err } // ToInt16 ... @@ -67,7 +82,12 @@ func (sc *stringConvert) ToInt8(str string) (int8, error) { // // Date : 18:29 2023/5/4 func (sc *stringConvert) ToInt16(str string) (int16, error) { - return 0, nil + var ( + err error + res int16 + ) + err = ConvertAssign(&res, str) + return res, err } // ToInt32 ... @@ -76,7 +96,12 @@ func (sc *stringConvert) ToInt16(str string) (int16, error) { // // Date : 18:29 2023/5/4 func (sc *stringConvert) ToInt32(str string) (int32, error) { - return 0, nil + var ( + err error + res int32 + ) + err = ConvertAssign(&res, str) + return res, err } // ToInt64 ... @@ -85,11 +110,26 @@ func (sc *stringConvert) ToInt32(str string) (int32, error) { // // Date : 18:29 2023/5/4 func (sc *stringConvert) ToInt64(str string) (int64, error) { - return 0, nil + var ( + err error + res int64 + ) + err = ConvertAssign(&res, str) + return res, err } +// ToInt ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:47 2023/5/5 func (sc *stringConvert) ToInt(str string) (int, error) { - return 0, nil + var ( + err error + res int + ) + err = ConvertAssign(&res, str) + return res, err } // ToUint8 ... @@ -98,7 +138,12 @@ func (sc *stringConvert) ToInt(str string) (int, error) { // // Date : 18:30 2023/5/4 func (sc *stringConvert) ToUint8(str string) (uint8, error) { - return 0, nil + var ( + err error + res uint8 + ) + err = ConvertAssign(&res, str) + return res, err } // ToUint16 ... @@ -107,7 +152,12 @@ func (sc *stringConvert) ToUint8(str string) (uint8, error) { // // Date : 18:29 2023/5/4 func (sc *stringConvert) ToUint16(str string) (uint16, error) { - return 0, nil + var ( + err error + res uint16 + ) + err = ConvertAssign(&res, str) + return res, err } // ToUint32 ... @@ -116,7 +166,12 @@ func (sc *stringConvert) ToUint16(str string) (uint16, error) { // // Date : 18:29 2023/5/4 func (sc *stringConvert) ToUint32(str string) (uint32, error) { - return 0, nil + var ( + err error + res uint32 + ) + err = ConvertAssign(&res, str) + return res, err } // ToUint64 ... @@ -125,7 +180,12 @@ func (sc *stringConvert) ToUint32(str string) (uint32, error) { // // Date : 18:29 2023/5/4 func (sc *stringConvert) ToUint64(str string) (uint64, error) { - return 0, nil + var ( + err error + res uint64 + ) + err = ConvertAssign(&res, str) + return res, err } // ToUint ... @@ -134,7 +194,12 @@ func (sc *stringConvert) ToUint64(str string) (uint64, error) { // // Date : 18:29 2023/5/4 func (sc *stringConvert) ToUint(str string) (uint, error) { - return 0, nil + var ( + err error + res uint + ) + err = ConvertAssign(&res, str) + return res, err } // ToBool ... @@ -143,7 +208,12 @@ func (sc *stringConvert) ToUint(str string) (uint, error) { // // Date : 18:34 2023/5/4 func (sc *stringConvert) ToBool(str string) (bool, error) { - return false, nil + var ( + err error + res bool + ) + err = ConvertAssign(&res, str) + return res, err } // ToObject ... From 96532aff0019e6c95923344f6d6f9258c1f71a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 5 May 2023 10:59:24 +0800 Subject: [PATCH 25/29] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E7=89=88string2other?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- string_convert.go | 258 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 258 insertions(+) diff --git a/string_convert.go b/string_convert.go index 767b26e..3ac30ee 100644 --- a/string_convert.go +++ b/string_convert.go @@ -393,3 +393,261 @@ func (sc *stringConvert) ToIntSlice(str string, splitChar ...string) ([]int, err } return res, nil } + +// ToUint8Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:50 2023/5/5 +func (sc *stringConvert) ToUint8Slice(str string, splitChar ...string) ([]uint8, error) { + var ( + err error + res []uint8 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint8, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint8(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToUint16Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:52 2023/5/5 +func (sc *stringConvert) ToUint16Slice(str string, splitChar ...string) ([]uint16, error) { + var ( + err error + res []uint16 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint16, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint16(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToUint32Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:53 2023/5/5 +func (sc *stringConvert) ToUint32Slice(str string, splitChar ...string) ([]uint32, error) { + var ( + err error + res []uint32 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint32, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint32(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToUint64Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:54 2023/5/5 +func (sc *stringConvert) ToUint64Slice(str string, splitChar ...string) ([]uint64, error) { + var ( + err error + res []uint64 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint64, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint64(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToUintSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:54 2023/5/5 +func (sc *stringConvert) ToUintSlice(str string, splitChar ...string) ([]uint, error) { + var ( + err error + res []uint + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToBoolSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:55 2023/5/5 +func (sc *stringConvert) ToBoolSlice(str string, splitChar ...string) ([]bool, error) { + var ( + err error + res []bool + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]bool, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToBool(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToFloat32Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:56 2023/5/5 +func (sc *stringConvert) ToFloat32Slice(str string, splitChar ...string) ([]float32, error) { + var ( + err error + res []float32 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]float32, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToFloat32(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToFloat64Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:56 2023/5/5 +func (sc *stringConvert) ToFloat64Slice(str string, splitChar ...string) ([]float64, error) { + var ( + err error + res []float64 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]float64, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToFloat64(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToDoubleSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:57 2023/5/5 +func (sc *stringConvert) ToDoubleSlice(str string, splitChar ...string) ([]float64, error) { + return sc.ToFloat64Slice(str, splitChar...) +} + +// ToNumberSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:57 2023/5/5 +func (sc *stringConvert) ToNumberSlice(str string, splitChar ...string) ([]float64, error) { + return sc.ToFloat64Slice(str, splitChar...) +} From a0e8f3b7c22e35cbe2b6aeb59ab11196df13d92b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 25 Jun 2023 18:05:43 +0800 Subject: [PATCH 26/29] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8B=BC=E9=9F=B3?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 + util.go | 208 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 211 insertions(+) create mode 100644 util.go diff --git a/go.mod b/go.mod index f37080e..e654957 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/mozillazg/go-pinyin v0.20.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect diff --git a/go.sum b/go.sum index 5b898a0..80ce228 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ= +github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc= github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4= github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= diff --git a/util.go b/util.go new file mode 100644 index 0000000..8456fdd --- /dev/null +++ b/util.go @@ -0,0 +1,208 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-06-25 17:22 +package util + +import "github.com/mozillazg/go-pinyin" + +// PinYinArg 设置选项方法 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:33 2023/6/25 +type PinYinArg func(arg *pinyin.Args) + +// 拼音风格的设置 + +// PinYinStyleNormal ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:37 2023/6/25 +func PinYinStyleNormal() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.Normal + } +} + +// PinYinStyleToneStand 声调风格1,拼音声调在韵母第一个字母上。如: zhōng guó +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:38 2023/6/25 +func PinYinStyleToneStand() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.Tone + } +} + +// PinYinStyleToneAfterYunMu 声调风格2,即拼音声调在各个韵母之后,用数字 [1-4] 进行表示。如: zho1ng guo2 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:39 2023/6/25 +func PinYinStyleToneAfterYunMu() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.Tone2 + } +} + +// PinYinStyleToneAtEnding 声调风格3,即拼音声调在各个拼音之后,用数字 [1-4] 进行表示。如: zhong1 guo2 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:40 2023/6/25 +func PinYinStyleToneAtEnding() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.Tone3 + } +} + +// PinYinStyleShengMu 声母风格,只返回各个拼音的声母部分。如: zh g 。注意:不是所有的拼音都有声母 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:41 2023/6/25 +func PinYinStyleShengMu() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.Initials + } +} + +// PinYinStyleFirstLetter 首字母风格,只返回拼音的首字母部分。如: z g +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:42 2023/6/25 +func PinYinStyleFirstLetter() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.FirstLetter + } +} + +// PinYinStyleYunMu 韵母风格,只返回各个拼音的韵母部分,不带声调。如: ong uo +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:43 2023/6/25 +func PinYinStyleYunMu() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.Finals + } +} + +// PinYinStyleToneYunMu 韵母风格1,带声调,声调在韵母第一个字母上。如: ōng uó +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:44 2023/6/25 +func PinYinStyleToneYunMu() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.FinalsTone + } +} + +// PinYinStyleToneYunMuAtStart 韵母风格2,带声调,声调在各个韵母之后,用数字 [1-4] 进行表示。如: o1ng uo2 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:45 2023/6/25 +func PinYinStyleToneYunMuAtStart() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.FinalsTone2 + } +} + +// PinYinStyleToneYunMuAtEnding 韵母风格3,带声调,声调在各个拼音之后,用数字 [1-4] 进行表示。如: ong1 uo2 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:40 2023/6/25 +func PinYinStyleToneYunMuAtEnding() PinYinArg { + return func(arg *pinyin.Args) { + arg.Style = pinyin.FinalsTone3 + } +} + +// 拼音风格设置结束 + +// PinYinStyleWithSeparator 设置拼音的分隔符 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:59 2023/6/25 +func PinYinStyleWithSeparator(separator string) PinYinArg { + return func(arg *pinyin.Args) { + if len(separator) == 0 { + return + } + arg.Separator = separator + } +} + +// PinYinStyleWithHeteronym 开启多音字模式 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:03 2023/6/25 +func PinYinStyleWithHeteronym() PinYinArg { + return func(arg *pinyin.Args) { + arg.Heteronym = true + } +} + +// PinYin 汉字转拼音 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:23 2023/6/25 +type PinYin struct { +} + +// Convert 汉字生成拼音 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:26 2023/6/25 +func (py *PinYin) Convert(text string, argFuncList ...PinYinArg) string { + arg := pinyin.NewArgs() + arg.Separator = " " + for _, argFunc := range argFuncList { + argFunc(&arg) + } + + return pinyin.Slug(text, arg) +} + +// ConvertSingle 每个字一个读音, 支持多音字 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:02 2023/6/25 +func (py *PinYin) ConvertSingle(text string, argFuncList ...PinYinArg) []string { + arg := pinyin.NewArgs() + arg.Separator = " " + for _, argFunc := range argFuncList { + argFunc(&arg) + } + + return pinyin.LazyPinyin(text, arg) +} + +// ConvertWithHeteronym 多音字模式 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:02 2023/6/25 +func (py *PinYin) ConvertWithHeteronym(text string, argFuncList ...PinYinArg) [][]string { + arg := pinyin.NewArgs() + arg.Separator = " " + for _, argFunc := range argFuncList { + argFunc(&arg) + } + return pinyin.Pinyin(text, arg) +} From 0c25220b9a00d8fadd625a1388d427f0a0ec2920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 1 Jul 2023 22:53:14 +0800 Subject: [PATCH 27/29] add letter list --- go.mod | 2 +- string.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index e654957..bacb0f7 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/go-ini/ini v1.67.0 github.com/mitchellh/go-homedir v1.1.0 + github.com/mozillazg/go-pinyin v0.20.0 github.com/mssola/user_agent v0.6.0 github.com/pkg/errors v0.9.1 github.com/spaolacci/murmur3 v1.1.0 @@ -18,7 +19,6 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/mozillazg/go-pinyin v0.20.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect diff --git a/string.go b/string.go index de97f97..dd76bcb 100644 --- a/string.go +++ b/string.go @@ -17,6 +17,14 @@ import ( "github.com/axgle/mahonia" ) +var ( + // LETTER_LIST 字母列表 + LETTER_LIST = []string{ + "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", + "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", + } +) + // stringOperate ... // // Author : go_developer@163.com<白茶清欢> From 7ed8ccbd87370c60bceae77ce5bcf0843f94f289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 1 Jul 2023 23:30:50 +0800 Subject: [PATCH 28/29] update --- init.go | 3 +++ util.go | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/init.go b/init.go index 37e40a3..68b7fd9 100644 --- a/init.go +++ b/init.go @@ -44,6 +44,8 @@ var ( Browser *browser // StringConvert 字符串转为任意类型 StringConvert *stringConvert + // PinYin 汉字转拼音 + PinYin *pinYin ) func init() { @@ -74,4 +76,5 @@ func init() { } // StringConvert StringConvert = &stringConvert{} + PinYin = &pinYin{} } diff --git a/util.go b/util.go index 8456fdd..4c08359 100644 --- a/util.go +++ b/util.go @@ -160,7 +160,7 @@ func PinYinStyleWithHeteronym() PinYinArg { // Author : go_developer@163.com<白茶清欢> // // Date : 17:23 2023/6/25 -type PinYin struct { +type pinYin struct { } // Convert 汉字生成拼音 @@ -168,7 +168,7 @@ type PinYin struct { // Author : go_developer@163.com<白茶清欢> // // Date : 17:26 2023/6/25 -func (py *PinYin) Convert(text string, argFuncList ...PinYinArg) string { +func (py *pinYin) Convert(text string, argFuncList ...PinYinArg) string { arg := pinyin.NewArgs() arg.Separator = " " for _, argFunc := range argFuncList { @@ -183,7 +183,7 @@ func (py *PinYin) Convert(text string, argFuncList ...PinYinArg) string { // Author : go_developer@163.com<白茶清欢> // // Date : 18:02 2023/6/25 -func (py *PinYin) ConvertSingle(text string, argFuncList ...PinYinArg) []string { +func (py *pinYin) ConvertSingle(text string, argFuncList ...PinYinArg) []string { arg := pinyin.NewArgs() arg.Separator = " " for _, argFunc := range argFuncList { @@ -198,7 +198,7 @@ func (py *PinYin) ConvertSingle(text string, argFuncList ...PinYinArg) []string // Author : go_developer@163.com<白茶清欢> // // Date : 18:02 2023/6/25 -func (py *PinYin) ConvertWithHeteronym(text string, argFuncList ...PinYinArg) [][]string { +func (py *pinYin) ConvertWithHeteronym(text string, argFuncList ...PinYinArg) [][]string { arg := pinyin.NewArgs() arg.Separator = " " for _, argFunc := range argFuncList { From 4a2cf51a9c7aedcd09510d1f17e0a6728c6ac7b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 22 Jul 2023 21:59:15 +0800 Subject: [PATCH 29/29] =?UTF-8?q?=E5=A2=9E=E5=8A=A0JSON=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E4=BB=A5=E5=8F=8Amap=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json.go | 29 +++++++++++++++++++++++++++++ map.go | 13 +++++++++++++ 2 files changed, 42 insertions(+) diff --git a/json.go b/json.go index c360d40..26e8fdb 100644 --- a/json.go +++ b/json.go @@ -46,6 +46,35 @@ func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiv return decoder.Decode(receiver) } +// UnmarshalWithNumberForString 字符串转结构体 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:50 2023/7/22 +func (oj *ownJSON) UnmarshalWithNumberForString(input string, receiver interface{}) error { + return oj.UnmarshalWithNumber([]byte(input), receiver) +} + +// MarshalForByte 序列化并返回字节数组 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:56 2023/7/22 +func (oj *ownJSON) MarshalForByte(input interface{}) []byte { + byteData, _ := json.Marshal(input) + return byteData +} + +// MarshalForString 序列化并返回字符串 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:56 2023/7/22 +func (oj *ownJSON) MarshalForString(input interface{}) string { + byteData := oj.MarshalForByte(input) + return string(byteData) +} + // ConsoleOutput ... // // Author : go_developer@163.com<白茶清欢> diff --git a/map.go b/map.go index 0c65279..d60d5ed 100644 --- a/map.go +++ b/map.go @@ -214,3 +214,16 @@ func (om *ownMap) GetDataWithReceiver(source map[interface{}]interface{}, key in } return ConvertAssign(receiver, source[key]) } + +// ToStruct map转结构体 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:48 2023/7/22 +func (om *ownMap) ToStruct(source map[string]interface{}, receiver interface{}) error { + if nil == source { + return nil + } + byteData := JSON.MarshalForByte(source) + return JSON.UnmarshalWithNumber(byteData, receiver) +}