mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
fmt: fix some gofmt issue
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -14,4 +14,4 @@ func Comma(v interface{}, symbol string) string {
|
||||
return symbol + commaString(s[:dotIndex]) + s[dotIndex:]
|
||||
}
|
||||
return symbol + commaString(s)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 ""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"`
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user