diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8e0f912..0fdfdfe 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,12 +6,12 @@ jobs: test: name: Test runs-on: ${{ matrix.os }} - + strategy: matrix: os: [ubuntu-latest, macOS-latest, windows-latest] - go: ['1.18.x', '1.22.x'] - + go: ['1.21.x', '1.22.x'] + steps: - name: Set git to use LF @@ -23,7 +23,7 @@ jobs: uses: actions/checkout@v4 - name: Set up Go ${{ matrix.go }} - uses: actions/setup-go@v4 + uses: actions/setup-go@v5 with: go-version: ${{ matrix.go }} id: go diff --git a/README.md b/README.md index 268ab5d..4c35848 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ # validator -[![Go](https://github.com/issue9/validator/workflows/Test/badge.svg)](https://github.com/issue9/validator/actions?query=workflow%3ATest) -[![Go Report Card](https://goreportcard.com/badge/github.com/issue9/validator)](https://goreportcard.com/report/github.com/issue9/validator) -![License](https://img.shields.io/github/license/issue9/validator) -[![Go version](https://img.shields.io/github/go-mod/go-version/issue9/validator)](https://golang.org) -[![codecov](https://codecov.io/gh/issue9/validator/branch/master/graph/badge.svg)](https://codecov.io/gh/issue9/validator) -[![PkgGoDev](https://pkg.go.dev/badge/github.com/issue9/validator)](https://pkg.go.dev/github.com/issue9/validator) +[![Go](https://github.com/issue9/webfilter/workflows/Test/badge.svg)](https://github.com/issue9/webfilter/actions?query=workflow%3ATest) +[![Go Report Card](https://goreportcard.com/badge/github.com/issue9/webfilter)](https://goreportcard.com/report/github.com/issue9/webfilter) +![License](https://img.shields.io/github/license/issue9/webfilter) +[![Go version](https://img.shields.io/github/go-mod/go-version/issue9/webfilter)](https://golang.org) +[![codecov](https://codecov.io/gh/issue9/webfilter/branch/master/graph/badge.svg)](https://codecov.io/gh/issue9/webfilter) +[![PkgGoDev](https://pkg.go.dev/badge/github.com/issue9/webfilter)](https://pkg.go.dev/github.com/issue9/webfilter) 符合 [web/filter](https://github.com/issue9/web) 的过滤器。 diff --git a/go.mod b/go.mod index ff3f34f..8e39dcb 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,5 @@ module github.com/issue9/filter -go 1.18 +go 1.21 -require ( - github.com/issue9/assert/v4 v4.1.1 - github.com/issue9/sliceutil v0.15.1 -) +require github.com/issue9/assert/v4 v4.1.1 diff --git a/go.sum b/go.sum index fd7d3e2..e03cb71 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,2 @@ github.com/issue9/assert/v4 v4.1.1 h1:OhPE8SB8n/qZCNGLQa+6MQtr/B3oON0JAVj68k8jJlc= github.com/issue9/assert/v4 v4.1.1/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4= -github.com/issue9/sliceutil v0.15.1 h1:mV1VlQSO5E8sh2ab6pypLg7TdgGZlDb30E8quli01xY= -github.com/issue9/sliceutil v0.15.1/go.mod h1:ldun6sT4/bOJxuMtOXhtc6P7GCwE7L+avV86HNks7qk= diff --git a/validator/number.go b/validator/cmp.go similarity index 58% rename from validator/number.go rename to validator/cmp.go index 2be5238..7ef1b48 100644 --- a/validator/number.go +++ b/validator/cmp.go @@ -4,15 +4,10 @@ package validator -// TODO(go1.21): 改为 cmd.Ordered -type Number interface { - float32 | float64 | - ~int | ~int8 | ~int16 | ~int32 | ~int64 | - ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 -} +import "cmp" // Between 判断数值区间 (min, max) -func Between[T Number](min, max T) func(T) bool { +func Between[T cmp.Ordered](min, max T) func(T) bool { if max < min { panic("max 必须大于等于 min") } @@ -21,7 +16,7 @@ func Between[T Number](min, max T) func(T) bool { } // BetweenEqual 判断数值区间 [min, max] -func BetweenEqual[T Number](min, max T) func(T) bool { +func BetweenEqual[T cmp.Ordered](min, max T) func(T) bool { if max < min { panic("max 必须大于等于 min") } @@ -29,19 +24,19 @@ func BetweenEqual[T Number](min, max T) func(T) bool { return func(val T) bool { return val >= min && val <= max } } -func Less[T Number](num T) func(T) bool { +func Less[T cmp.Ordered](num T) func(T) bool { return func(t T) bool { return t < num } } -func LessEqual[T Number](num T) func(T) bool { +func LessEqual[T cmp.Ordered](num T) func(T) bool { return func(t T) bool { return t <= num } } -func Great[T Number](num T) func(T) bool { +func Great[T cmp.Ordered](num T) func(T) bool { return func(t T) bool { return t > num } } -func GreatEqual[T Number](num T) func(T) bool { +func GreatEqual[T cmp.Ordered](num T) func(T) bool { return func(t T) bool { return t >= num } } @@ -49,6 +44,7 @@ func GreatEqual[T Number](num T) func(T) bool { func HTTPStatus(s int) bool { return BetweenEqual(100, 599)(s) } // ZeroOr 判断值为零值或是非零情况下符合 v 的要求 -func ZeroOr[T Number](v func(T) bool) func(T) bool { - return Or(func(v T) bool { return v == 0 }, v) +func ZeroOr[T cmp.Ordered](v func(T) bool) func(T) bool { + var zero T + return Or(func(v T) bool { return v == zero }, v) } diff --git a/validator/number_test.go b/validator/cmp_test.go similarity index 100% rename from validator/number_test.go rename to validator/cmp_test.go diff --git a/validator/slice.go b/validator/slice.go index 74d4b1b..25b33ff 100644 --- a/validator/slice.go +++ b/validator/slice.go @@ -4,15 +4,13 @@ package validator -import "github.com/issue9/sliceutil" +import "slices" // In 声明枚举类型的验证规则 // // 要求验证的值必须包含在 element 元素中,如果不存在,则返回 msg 的内容。 func In[T comparable](element ...T) func(T) bool { - return func(v T) bool { - return sliceutil.Exists(element, func(elem T, _ int) bool { return elem == v }) - } + return func(v T) bool { return slices.Index(element, v) >= 0 } } // NotIn 声明不在枚举中的验证规则