golangci-lint run

This commit is contained in:
孟帅
2023-07-24 09:35:30 +08:00
parent 071b6224c9
commit 996ed818ee
61 changed files with 527 additions and 271 deletions

View 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)
})
}

View File

@@ -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)