mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-14 05:03:49 +08:00
golangci-lint run
This commit is contained in:
@@ -12,37 +12,38 @@ import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type fileInfo struct { //文件信息
|
||||
// 文件信息
|
||||
type fileInfo struct {
|
||||
name string
|
||||
size int64
|
||||
}
|
||||
|
||||
// WalkDir 递归获取目录下文件的名称和大小
|
||||
func WalkDir(dirname string) (error, []fileInfo) {
|
||||
op, err := filepath.Abs(dirname) //获取目录的绝对路径
|
||||
op, err := filepath.Abs(dirname) // 获取目录的绝对路径
|
||||
if nil != err {
|
||||
return err, nil
|
||||
}
|
||||
files, err := os.ReadDir(op) //获取目录下所有文件的信息,包括文件和文件夹
|
||||
files, err := os.ReadDir(op) // 获取目录下所有文件的信息,包括文件和文件夹
|
||||
if nil != err {
|
||||
return err, nil
|
||||
}
|
||||
|
||||
var fileInfos []fileInfo //返回值,存储读取的文件信息
|
||||
var fileInfos []fileInfo // 返回值,存储读取的文件信息
|
||||
for _, f := range files {
|
||||
if f.IsDir() { // 如果是目录,那么就递归调用
|
||||
err, fs := WalkDir(op + `/` + f.Name()) //路径分隔符,linux 和 windows 不同
|
||||
err, fs := WalkDir(op + `/` + f.Name()) // 路径分隔符,linux 和 windows 不同
|
||||
if nil != err {
|
||||
return err, nil
|
||||
}
|
||||
fileInfos = append(fileInfos, fs...) //将 slice 添加到 slice
|
||||
fileInfos = append(fileInfos, fs...) // 将 slice 添加到 slice
|
||||
} else {
|
||||
info, err := f.Info()
|
||||
if nil != err {
|
||||
return err, nil
|
||||
}
|
||||
fi := fileInfo{op + `/` + f.Name(), info.Size()}
|
||||
fileInfos = append(fileInfos, fi) //slice 中添加成员
|
||||
fileInfos = append(fileInfos, fi) // slice 中添加成员
|
||||
}
|
||||
}
|
||||
return nil, fileInfos
|
||||
|
||||
@@ -32,8 +32,8 @@ func GenLabel(basic string, appendId int64) string {
|
||||
}
|
||||
|
||||
// GetIdLabel 获取指定Id的树标签
|
||||
func GetIdLabel(Id int64) string {
|
||||
return fmt.Sprintf("%v%v%v", treeBeginCut, Id, treeEndCut)
|
||||
func GetIdLabel(id int64) string {
|
||||
return fmt.Sprintf("%v%v%v", treeBeginCut, id, treeEndCut)
|
||||
}
|
||||
|
||||
// GetIds 获取上级ID集合
|
||||
|
||||
68
server/utility/validate/filter_test.go
Normal file
68
server/utility/validate/filter_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package validate_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"hotgo/utility/validate"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// MockFilter 是 Filter 接口的模拟实现。
|
||||
type MockFilter struct {
|
||||
Foo string
|
||||
Bar int
|
||||
}
|
||||
|
||||
func (f *MockFilter) Filter(ctx context.Context) error {
|
||||
// 模拟过滤逻辑
|
||||
|
||||
// 过滤出错的例子
|
||||
if f.Foo == "" {
|
||||
return gerror.New("Foo 字段是必需的")
|
||||
}
|
||||
|
||||
// 过滤操作的例子
|
||||
f.Bar += 10
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestPreFilter(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
input := &MockFilter{
|
||||
Foo: "test",
|
||||
Bar: 5,
|
||||
}
|
||||
|
||||
err := validate.PreFilter(ctx, input)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.AssertNil(err)
|
||||
})
|
||||
|
||||
t.Logf("input:%+v", input)
|
||||
|
||||
// 验证过滤结果
|
||||
expectedBar := 15
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(input.Bar, expectedBar)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPreFilter_Error(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
input := &MockFilter{
|
||||
Foo: "",
|
||||
Bar: 5,
|
||||
}
|
||||
|
||||
err := validate.PreFilter(ctx, input)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
|
||||
expectedError := "Foo 字段是必需的"
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(err.Error(), expectedError)
|
||||
})
|
||||
}
|
||||
@@ -42,15 +42,15 @@ func IsIp(ip string) bool {
|
||||
}
|
||||
|
||||
// IsPublicIp 是否是公网IP
|
||||
func IsPublicIp(Ip string) bool {
|
||||
ip := net.ParseIP(Ip)
|
||||
func IsPublicIp(ip string) bool {
|
||||
i := net.ParseIP(ip)
|
||||
|
||||
if ip.IsLoopback() || ip.IsPrivate() || ip.IsMulticast() || ip.IsUnspecified() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
|
||||
if i.IsLoopback() || i.IsPrivate() || i.IsMulticast() || i.IsUnspecified() || i.IsLinkLocalUnicast() || i.IsLinkLocalMulticast() {
|
||||
return false
|
||||
}
|
||||
|
||||
if ip4 := ip.To4(); ip4 != nil {
|
||||
return !ip.Equal(net.IPv4bcast)
|
||||
if ip4 := i.To4(); ip4 != nil {
|
||||
return !i.Equal(net.IPv4bcast)
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func IsMobile(mobile string) bool {
|
||||
|
||||
// IsEmail 是否为邮箱地址
|
||||
func IsEmail(email string) bool {
|
||||
//pattern := `\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*` //匹配电子邮箱
|
||||
// pattern := `\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*` //匹配电子邮箱
|
||||
pattern := `^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z].){1,4}[a-z]{2,4}$`
|
||||
reg := regexp.MustCompile(pattern)
|
||||
return reg.MatchString(email)
|
||||
|
||||
Reference in New Issue
Block a user