From 4591c3f87f289fd8d2beb380f84690d0b9ca99e9 Mon Sep 17 00:00:00 2001 From: tanqiangyes <826285820@qq.com> Date: Wed, 11 Oct 2023 14:53:31 +0800 Subject: [PATCH] Dev (#5) * add ci and set revive * fix ci question --------- Co-authored-by: tanqiang --- .github/workflows/go.yml | 4 ---- arrays_benchmark_test.go | 4 ++-- converter.go | 6 +++--- converter_example_test.go | 2 +- patterns.go | 2 +- types.go | 4 ++-- validator.go | 16 +++++----------- validator_test.go | 32 ++++++++++++++++---------------- 8 files changed, 30 insertions(+), 40 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index a481e73..c9fccb9 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -17,7 +17,6 @@ jobs: - uses: actions/setup-go@v3 with: go-version: 1.18 - stable: false - uses: actions/checkout@v4 - name: golangci-lint uses: golangci/golangci-lint-action@v3 @@ -42,9 +41,6 @@ jobs: # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. # skip-build-cache: true - - # optionally use a specific version of Go rather than the latest one - go_version: '1.20' build: runs-on: ubuntu-latest steps: diff --git a/arrays_benchmark_test.go b/arrays_benchmark_test.go index ebe660e..9182090 100644 --- a/arrays_benchmark_test.go +++ b/arrays_benchmark_test.go @@ -25,8 +25,8 @@ func BenchmarkEach(b *testing.B) { data := randomArray(1000000) b.ResetTimer() for n := 0; n < b.N; n++ { - var fn Iterator[int] = func(value int, index int) { - value = value + index + var fn Iterator[int] = func(_ int, _ int) { + // do nothing } Each(data, fn) } diff --git a/converter.go b/converter.go index 3c1f3f5..ebadfb5 100644 --- a/converter.go +++ b/converter.go @@ -37,13 +37,13 @@ func ToNumber[T any, U constraints.Float | constraints.Integer](value T) (res U, res = U(val.Float()) case reflect.String: if IsInt[string](val.String()) { - resInt, err := strconv.ParseInt(val.String(), 0, 64) - if err != nil { + resInt, err1 := strconv.ParseInt(val.String(), 0, 64) + if err1 != nil { res = 0 } else { res = U(resInt) } - } else if resFloat, err := strconv.ParseFloat(val.String(), 64); err == nil { + } else if resFloat, err1 := strconv.ParseFloat(val.String(), 64); err1 == nil { res = U(resFloat) } else { err = fmt.Errorf("ToInt: invalid numeric format %v", value) diff --git a/converter_example_test.go b/converter_example_test.go index 7dc1cfa..0f7b6fb 100644 --- a/converter_example_test.go +++ b/converter_example_test.go @@ -17,7 +17,7 @@ func ExampleToInt() { _, _ = ToInt("false") // 0, error } -func ExampleToFloat() { +func ExampleToNumber() { _, _ = ToNumber[string, float64]("-124.2e123") // -124.2e123, nil _, _ = ToNumber[string, float64]("false") // 0, error } diff --git a/patterns.go b/patterns.go index bafc376..af91bc2 100644 --- a/patterns.go +++ b/patterns.go @@ -67,7 +67,7 @@ const ( var ( userRegexp = regexp.MustCompile("^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~.-]+$") - hostRegexp = regexp.MustCompile("^[^\\s]+\\.[^\\s]+$") + hostRegexp = regexp.MustCompile(`^[^\\s]+\\.[^\\s]+$`) userDotRegexp = regexp.MustCompile("(^[.]{1})|([.]{1}$)|([.]{2,})") rxEmail = regexp.MustCompile(Email) rxCreditCard = regexp.MustCompile(CreditCard) diff --git a/types.go b/types.go index 770a584..cc59e27 100644 --- a/types.go +++ b/types.go @@ -75,8 +75,8 @@ var ParamTagMap = map[string]ParamValidator[string]{ // ParamTagRegexMap maps param tags to their respective regexes. var ParamTagRegexMap = map[string]*regexp.Regexp{ - "range": regexp.MustCompile("^range\\((\\d+(?:.\\d+)?)\\|(\\d+(?:.\\d+)?)\\)$"), - "length": regexp.MustCompile("^length\\((\\d+)\\|(\\d+)\\)$"), + "range": regexp.MustCompile(`^range\\((\\d+(?:.\\d+)?)\\|(\\d+(?:.\\d+)?)\\)$`), + "length": regexp.MustCompile(`^length\\((\\d+)\\|(\\d+)\\)$`), "runelength": regexp.MustCompile("^runelength\\((\\d+)\\|(\\d+)\\)$"), "stringlength": regexp.MustCompile("^stringlength\\((\\d+)\\|(\\d+)\\)$"), "in": regexp.MustCompile(`^in\((.*)\)`), diff --git a/validator.go b/validator.go index d620639..8616da3 100644 --- a/validator.go +++ b/validator.go @@ -25,9 +25,9 @@ import ( var ( fieldsRequiredByDefault bool nilPtrAllowedByRequired = false - notNumberRegexp = regexp.MustCompile("[^0-9]+") - whiteSpacesAndMinus = regexp.MustCompile(`[\s-]+`) - paramsRegexp = regexp.MustCompile(`\(.*\)$`) + // notNumberRegexp = regexp.MustCompile("[^0-9]+") + whiteSpacesAndMinus = regexp.MustCompile(`[\s-]+`) + paramsRegexp = regexp.MustCompile(`\(.*\)$`) ) const maxURLRuneCount = 2083 @@ -612,20 +612,14 @@ func IsWinFilePath[T ~string](str T) bool { if rxARWinPath.MatchString(string(str)) { // check windows path limit see: // http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#maxpath - if len(str[3:]) > 32767 { - return false - } - return true + return len(str[3:]) <= 32767 } return false } // IsUnixFilePath checks both relative & absolute paths in Unix func IsUnixFilePath[T ~string](str T) bool { - if rxARUnixPath.MatchString(string(str)) { - return true - } - return false + return rxARUnixPath.MatchString(string(str)) } // IsDataURI checks if a string is base64 encoded data URI such as an image diff --git a/validator_test.go b/validator_test.go index 5988940..f7f9190 100644 --- a/validator_test.go +++ b/validator_test.go @@ -3513,22 +3513,22 @@ func TestValidateStructParamValidatorInt(t *testing.T) { t.Errorf("Test failed: nil") } - type Test3 struct { - Int int `valid:"in(1|10),int"` - Int8 int8 `valid:"in(1|10),int8"` - Int16 int16 `valid:"in(1|10),int16"` - Int32 int32 `valid:"in(1|10),int32"` - Int64 int64 `valid:"in(1|10),int64"` - - Uint uint `valid:"in(1|10),uint"` - Uint8 uint8 `valid:"in(1|10),uint8"` - Uint16 uint16 `valid:"in(1|10),uint16"` - Uint32 uint32 `valid:"in(1|10),uint32"` - Uint64 uint64 `valid:"in(1|10),uint64"` - - Float32 float32 `valid:"in(1|10),float32"` - Float64 float64 `valid:"in(1|10),float64"` - } + // type Test3 struct { + // Int int `valid:"in(1|10),int"` + // Int8 int8 `valid:"in(1|10),int8"` + // Int16 int16 `valid:"in(1|10),int16"` + // Int32 int32 `valid:"in(1|10),int32"` + // Int64 int64 `valid:"in(1|10),int64"` + // + // Uint uint `valid:"in(1|10),uint"` + // Uint8 uint8 `valid:"in(1|10),uint8"` + // Uint16 uint16 `valid:"in(1|10),uint16"` + // Uint32 uint32 `valid:"in(1|10),uint32"` + // Uint64 uint64 `valid:"in(1|10),uint64"` + // + // Float32 float32 `valid:"in(1|10),float32"` + // Float64 float64 `valid:"in(1|10),float64"` + // } test3Ok1 := &Test2{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} test3Ok2 := &Test2{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}