Skip to content

Commit

Permalink
refactor: 更新最新版至 go1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Mar 8, 2024
1 parent 8988f8c commit 2d3c4b8
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 35 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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) 的过滤器。

Expand Down
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
24 changes: 10 additions & 14 deletions validator/number.go → validator/cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -21,34 +16,35 @@ 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")
}

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

// HTTPStatus 是否为有效的 HTTP 状态码
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)
}
File renamed without changes.
6 changes: 2 additions & 4 deletions validator/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 声明不在枚举中的验证规则
Expand Down

0 comments on commit 2d3c4b8

Please sign in to comment.