From 0c4b5120843fd2d67902ea38c68faf1b29de8b02 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 29 Nov 2021 13:01:46 +0800 Subject: [PATCH] fmt: fix some gofmt issue --- convertor/convertor.go | 2 +- cryptor/aes.go | 2 +- cryptor/basic.go | 4 ++-- datetime/datetime.go | 2 -- fileutil/file.go | 8 +++----- fileutil/file_test.go | 3 ++- formatter/formatter.go | 2 +- formatter/formatter_test.go | 25 +++++++++++++------------ formatter/formatter_util.go | 27 ++++++++++++++------------- netutil/net.go | 1 + netutil/net_test.go | 3 ++- netutil/request_test.go | 3 ++- utils/utils.go | 1 + validator/validator.go | 7 +++---- 14 files changed, 46 insertions(+), 44 deletions(-) diff --git a/convertor/convertor.go b/convertor/convertor.go index 5de9203..0569729 100644 --- a/convertor/convertor.go +++ b/convertor/convertor.go @@ -20,7 +20,7 @@ func ToBool(s string) (bool, error) { return strconv.ParseBool(s) } -// ToBool convert interface to bytes +// ToBytes convert interface to bytes func ToBytes(data interface{}) ([]byte, error) { var buf bytes.Buffer enc := gob.NewEncoder(&buf) diff --git a/cryptor/aes.go b/cryptor/aes.go index ef7016a..58995b0 100644 --- a/cryptor/aes.go +++ b/cryptor/aes.go @@ -65,7 +65,7 @@ func AesCbcEncrypt(data, key []byte) []byte { return encrypted } -// AesEcbDecrypt decrypt data with key use AES CBC algorithm +// AesCbcDecrypt decrypt data with key use AES CBC algorithm // len(key) should be 16, 24 or 32 func AesCbcDecrypt(encrypted, key []byte) []byte { block, _ := aes.NewCipher(key) diff --git a/cryptor/basic.go b/cryptor/basic.go index e0a4d3e..16942c4 100644 --- a/cryptor/basic.go +++ b/cryptor/basic.go @@ -21,13 +21,13 @@ func Base64StdEncode(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) } -// Base64StdEncode decode a base64 encoded string +// Base64StdDecode decode a base64 encoded string func Base64StdDecode(s string) string { b, _ := base64.StdEncoding.DecodeString(s) return string(b) } -// Md5Str return the md5 value of string +// Md5String return the md5 value of string func Md5String(s string) string { h := md5.New() h.Write([]byte(s)) diff --git a/datetime/datetime.go b/datetime/datetime.go index 8e12621..f37e3fb 100644 --- a/datetime/datetime.go +++ b/datetime/datetime.go @@ -2,7 +2,6 @@ // Use of this source code is governed by MIT license. // Package datetime implements some functions to format date and time. - // Note: // 1. `format` param in FormatTimeToStr function should be as flow: //"yyyy-mm-dd hh:mm:ss" @@ -23,7 +22,6 @@ //"mm" //"hh:mm:ss" //"mm:ss" - package datetime import ( diff --git a/fileutil/file.go b/fileutil/file.go index d057551..bbc9a62 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -2,7 +2,6 @@ // Use of this source code is governed by MIT license. // Package fileutil implements some basic functions for file operations - package fileutil import ( @@ -12,7 +11,7 @@ import ( "os" ) -// IsFileExists checks if a file or directory exists +// IsExist checks if a file or directory exists func IsExist(path string) bool { _, err := os.Stat(path) if err == nil { @@ -35,7 +34,7 @@ func CreateFile(path string) bool { return true } -// IsFileExists checks if the path is directy or not +// IsDir checks if the path is directory or not func IsDir(path string) bool { file, err := os.Stat(path) if err != nil { @@ -70,9 +69,8 @@ func CopyFile(srcFilePath string, dstFilePath string) error { if err != nil { if err == io.EOF { return nil - } else { - return err } + return err } } } diff --git a/fileutil/file_test.go b/fileutil/file_test.go index edc8ca9..e7f2bb1 100644 --- a/fileutil/file_test.go +++ b/fileutil/file_test.go @@ -1,10 +1,11 @@ package fileutil import ( - "github.com/duke-git/lancet/utils" "os" "reflect" "testing" + + "github.com/duke-git/lancet/utils" ) func TestIsExist(t *testing.T) { diff --git a/formatter/formatter.go b/formatter/formatter.go index a9bf1a2..9d5687b 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -14,4 +14,4 @@ func Comma(v interface{}, symbol string) string { return symbol + commaString(s[:dotIndex]) + s[dotIndex:] } return symbol + commaString(s) -} \ No newline at end of file +} diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index 2dee50d..f29713c 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -1,25 +1,26 @@ package formatter import ( - "github.com/duke-git/lancet/utils" "testing" + + "github.com/duke-git/lancet/utils" ) func TestComma(t *testing.T) { - comma(t, "", "","") - comma(t, "aa", "","") - comma(t, "123", "","123") - comma(t, "12345", "","12,345") - comma(t, 12345, "","12,345") - comma(t, 12345, "$","$12,345") - comma(t, 12345, "¥","¥12,345") - comma(t, 12345.6789, "","12,345.6789") + comma(t, "", "", "") + comma(t, "aa", "", "") + comma(t, "123", "", "123") + comma(t, "12345", "", "12,345") + comma(t, 12345, "", "12,345") + comma(t, 12345, "$", "$12,345") + comma(t, 12345, "¥", "¥12,345") + comma(t, 12345.6789, "", "12,345.6789") } -func comma(t *testing.T, test interface{}, symbol string, expected interface{}) { - res:= Comma(test, symbol) +func comma(t *testing.T, test interface{}, symbol string, expected interface{}) { + res := Comma(test, symbol) if res != expected { utils.LogFailedTestInfo(t, "Comma", test, expected, res) t.FailNow() } -} \ No newline at end of file +} diff --git a/formatter/formatter_util.go b/formatter/formatter_util.go index ff7f6ff..4928147 100644 --- a/formatter/formatter_util.go +++ b/formatter/formatter_util.go @@ -18,22 +18,23 @@ func numString(value interface{}) string { switch reflect.TypeOf(value).Kind() { case reflect.Int, reflect.Int64, reflect.Float32, reflect.Float64: return fmt.Sprintf("%v", value) - case reflect.String: { - sv := fmt.Sprintf("%v", value) - if strings.Contains(sv, ".") { - _, err := strconv.ParseFloat(sv, 64) - if err == nil { - return sv - } - }else { - _, err := strconv.ParseInt(sv, 10, 64) - if err == nil { - return sv + case reflect.String: + { + sv := fmt.Sprintf("%v", value) + if strings.Contains(sv, ".") { + _, err := strconv.ParseFloat(sv, 64) + if err == nil { + return sv + } + } else { + _, err := strconv.ParseInt(sv, 10, 64) + if err == nil { + return sv + } } } - } default: return "" } return "" -} \ No newline at end of file +} diff --git a/netutil/net.go b/netutil/net.go index 132d9b4..d531061 100644 --- a/netutil/net.go +++ b/netutil/net.go @@ -47,6 +47,7 @@ func GetPublicIpInfo() (*PublicIpInfo, error) { return &ip, nil } +// PublicIpInfo public ip info: country, region, isp, city, lat, lon, ip type PublicIpInfo struct { Status string `json:"status"` Country string `json:"country"` diff --git a/netutil/net_test.go b/netutil/net_test.go index b8faec9..5150d9b 100644 --- a/netutil/net_test.go +++ b/netutil/net_test.go @@ -2,9 +2,10 @@ package netutil import ( "fmt" - "github.com/duke-git/lancet/utils" "net" "testing" + + "github.com/duke-git/lancet/utils" ) func TestGetInternalIp(t *testing.T) { diff --git a/netutil/request_test.go b/netutil/request_test.go index 9527200..3871993 100644 --- a/netutil/request_test.go +++ b/netutil/request_test.go @@ -4,9 +4,10 @@ import ( "encoding/json" "fmt" "io/ioutil" - "github.com/duke-git/lancet/utils" "log" "testing" + + "github.com/duke-git/lancet/utils" ) func TestHttpGet(t *testing.T) { diff --git a/utils/utils.go b/utils/utils.go index 0da9844..9fddbbc 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -9,6 +9,7 @@ import ( "testing" ) +// LogFailedTestInfo log test failed info for internal use func LogFailedTestInfo(t *testing.T, testCase, input, expected, result interface{}) { errInfo := fmt.Sprintf("Test case %v: input is %+v, expected %v, but result is %v", testCase, input, expected, result) t.Error(errInfo) diff --git a/validator/validator.go b/validator/validator.go index 3c01cfd..2f2e13b 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -123,9 +123,9 @@ func IsChinesePhone(phone string) bool { // IsCreditCard check if the string is credit card. func IsCreditCard(creditCart string) bool { - pattern := `^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$` - reg := regexp.MustCompile(pattern) - return reg.MatchString(creditCart) + pattern := `^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$` + reg := regexp.MustCompile(pattern) + return reg.MatchString(creditCart) } // IsBase64 check if the string is base64 string. @@ -191,4 +191,3 @@ func IsWeakPassword(password string) bool { return (num || letter) && !special } -