mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-10 07:42:27 +08:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c4b512084 |
@@ -20,7 +20,7 @@ func ToBool(s string) (bool, error) {
|
|||||||
return strconv.ParseBool(s)
|
return strconv.ParseBool(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToBool convert interface to bytes
|
// ToBytes convert interface to bytes
|
||||||
func ToBytes(data interface{}) ([]byte, error) {
|
func ToBytes(data interface{}) ([]byte, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
enc := gob.NewEncoder(&buf)
|
enc := gob.NewEncoder(&buf)
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ func AesCbcEncrypt(data, key []byte) []byte {
|
|||||||
return encrypted
|
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
|
// len(key) should be 16, 24 or 32
|
||||||
func AesCbcDecrypt(encrypted, key []byte) []byte {
|
func AesCbcDecrypt(encrypted, key []byte) []byte {
|
||||||
block, _ := aes.NewCipher(key)
|
block, _ := aes.NewCipher(key)
|
||||||
|
|||||||
@@ -21,13 +21,13 @@ func Base64StdEncode(s string) string {
|
|||||||
return base64.StdEncoding.EncodeToString([]byte(s))
|
return base64.StdEncoding.EncodeToString([]byte(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Base64StdEncode decode a base64 encoded string
|
// Base64StdDecode decode a base64 encoded string
|
||||||
func Base64StdDecode(s string) string {
|
func Base64StdDecode(s string) string {
|
||||||
b, _ := base64.StdEncoding.DecodeString(s)
|
b, _ := base64.StdEncoding.DecodeString(s)
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Md5Str return the md5 value of string
|
// Md5String return the md5 value of string
|
||||||
func Md5String(s string) string {
|
func Md5String(s string) string {
|
||||||
h := md5.New()
|
h := md5.New()
|
||||||
h.Write([]byte(s))
|
h.Write([]byte(s))
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
// Use of this source code is governed by MIT license.
|
// Use of this source code is governed by MIT license.
|
||||||
|
|
||||||
// Package datetime implements some functions to format date and time.
|
// Package datetime implements some functions to format date and time.
|
||||||
|
|
||||||
// Note:
|
// Note:
|
||||||
// 1. `format` param in FormatTimeToStr function should be as flow:
|
// 1. `format` param in FormatTimeToStr function should be as flow:
|
||||||
//"yyyy-mm-dd hh:mm:ss"
|
//"yyyy-mm-dd hh:mm:ss"
|
||||||
@@ -23,7 +22,6 @@
|
|||||||
//"mm"
|
//"mm"
|
||||||
//"hh:mm:ss"
|
//"hh:mm:ss"
|
||||||
//"mm:ss"
|
//"mm:ss"
|
||||||
|
|
||||||
package datetime
|
package datetime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
// Use of this source code is governed by MIT license.
|
// Use of this source code is governed by MIT license.
|
||||||
|
|
||||||
// Package fileutil implements some basic functions for file operations
|
// Package fileutil implements some basic functions for file operations
|
||||||
|
|
||||||
package fileutil
|
package fileutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -12,7 +11,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsFileExists checks if a file or directory exists
|
// IsExist checks if a file or directory exists
|
||||||
func IsExist(path string) bool {
|
func IsExist(path string) bool {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -35,7 +34,7 @@ func CreateFile(path string) bool {
|
|||||||
return true
|
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 {
|
func IsDir(path string) bool {
|
||||||
file, err := os.Stat(path)
|
file, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -70,9 +69,8 @@ func CopyFile(srcFilePath string, dstFilePath string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
return nil
|
return nil
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package fileutil
|
package fileutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/utils"
|
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIsExist(t *testing.T) {
|
func TestIsExist(t *testing.T) {
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package formatter
|
package formatter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/utils"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestComma(t *testing.T) {
|
func TestComma(t *testing.T) {
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ func numString(value interface{}) string {
|
|||||||
switch reflect.TypeOf(value).Kind() {
|
switch reflect.TypeOf(value).Kind() {
|
||||||
case reflect.Int, reflect.Int64, reflect.Float32, reflect.Float64:
|
case reflect.Int, reflect.Int64, reflect.Float32, reflect.Float64:
|
||||||
return fmt.Sprintf("%v", value)
|
return fmt.Sprintf("%v", value)
|
||||||
case reflect.String: {
|
case reflect.String:
|
||||||
|
{
|
||||||
sv := fmt.Sprintf("%v", value)
|
sv := fmt.Sprintf("%v", value)
|
||||||
if strings.Contains(sv, ".") {
|
if strings.Contains(sv, ".") {
|
||||||
_, err := strconv.ParseFloat(sv, 64)
|
_, err := strconv.ParseFloat(sv, 64)
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ func GetPublicIpInfo() (*PublicIpInfo, error) {
|
|||||||
return &ip, nil
|
return &ip, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PublicIpInfo public ip info: country, region, isp, city, lat, lon, ip
|
||||||
type PublicIpInfo struct {
|
type PublicIpInfo struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Country string `json:"country"`
|
Country string `json:"country"`
|
||||||
|
|||||||
@@ -2,9 +2,10 @@ package netutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/utils"
|
|
||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetInternalIp(t *testing.T) {
|
func TestGetInternalIp(t *testing.T) {
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"github.com/duke-git/lancet/utils"
|
|
||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHttpGet(t *testing.T) {
|
func TestHttpGet(t *testing.T) {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// LogFailedTestInfo log test failed info for internal use
|
||||||
func LogFailedTestInfo(t *testing.T, testCase, input, expected, result interface{}) {
|
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)
|
errInfo := fmt.Sprintf("Test case %v: input is %+v, expected %v, but result is %v", testCase, input, expected, result)
|
||||||
t.Error(errInfo)
|
t.Error(errInfo)
|
||||||
|
|||||||
@@ -191,4 +191,3 @@ func IsWeakPassword(password string) bool {
|
|||||||
|
|
||||||
return (num || letter) && !special
|
return (num || letter) && !special
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user