diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 466d2e0..3cdc635 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -34,7 +34,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@v3 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -45,7 +45,7 @@ jobs: # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild - uses: github/codeql-action/autobuild@v2 + uses: github/codeql-action/autobuild@v3 # ℹ️ Command-line programs to run using the OS shell. # 📚 https://git.io/JvXDl @@ -59,4 +59,4 @@ jobs: # make release - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@v3 diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index ba4d50a..bf05f88 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.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: Check out code into the Go module directory @@ -25,7 +25,7 @@ jobs: - name: Vet run: go vet -v ./... - + - name: Test run: go test -race -v -coverprofile='coverage.txt' -covermode=atomic ./... diff --git a/go.mod b/go.mod index 979e39b..86406ce 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,9 @@ module github.com/issue9/mux/v7 require ( github.com/issue9/assert/v4 v4.1.1 github.com/issue9/errwrap v0.3.2 - github.com/issue9/sliceutil v0.16.0 - github.com/issue9/source v0.8.3 + github.com/issue9/source v0.9.0 ) require golang.org/x/mod v0.16.0 // indirect -go 1.18 +go 1.21 diff --git a/go.sum b/go.sum index 93858d6..cf0f6dd 100644 --- a/go.sum +++ b/go.sum @@ -2,9 +2,7 @@ github.com/issue9/assert/v4 v4.1.1 h1:OhPE8SB8n/qZCNGLQa+6MQtr/B3oON0JAVj68k8jJl github.com/issue9/assert/v4 v4.1.1/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4= github.com/issue9/errwrap v0.3.2 h1:7KEme9Pfe75M+sIMcPCn/DV90wjnOcRbO4DXVAHj3Fw= github.com/issue9/errwrap v0.3.2/go.mod h1:KcCLuUGiffjooLCUjL89r1cyO8/HT/VRcQrneO53N3A= -github.com/issue9/sliceutil v0.16.0 h1:+gyumWF126mprQLiUi14s6SAeucMg1vuyPEh66VTtC8= -github.com/issue9/sliceutil v0.16.0/go.mod h1:ldun6sT4/bOJxuMtOXhtc6P7GCwE7L+avV86HNks7qk= -github.com/issue9/source v0.8.3 h1:MH883uljg0fupE/qkqJm3RqA5/l40KR+HsR7Mp3zjYY= -github.com/issue9/source v0.8.3/go.mod h1:H0Joo5go08sIMODLvQXpWwiWpGrH2MiTMFhKBZhVbH8= +github.com/issue9/source v0.9.0 h1:bGaoCXRGiZKZnjfZGGKqZKFm1wScPdpRQtyIuBwBfNs= +github.com/issue9/source v0.9.0/go.mod h1:C458HN4j4rA3wWLcO6XiB3PM3yFStdCDnX7OxMr9+yU= golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= diff --git a/group/group.go b/group/group.go index 5a05481..29b1406 100644 --- a/group/group.go +++ b/group/group.go @@ -8,8 +8,7 @@ package group import ( "fmt" "net/http" - - "github.com/issue9/sliceutil" + "slices" "github.com/issue9/mux/v7" "github.com/issue9/mux/v7/internal/options" @@ -107,9 +106,7 @@ func (g *GroupOf[T]) Add(matcher Matcher, r *mux.RouterOf[T]) { } // 重名检测 - if sliceutil.Exists(g.routers, func(rr *routerOf[T], _ int) bool { - return rr.r.Name() == r.Name() - }) { + if slices.IndexFunc(g.routers, func(rr *routerOf[T]) bool { return rr.r.Name() == r.Name() }) >= 0 { panic(fmt.Sprintf("已经存在名为 %s 的路由", r.Name())) } @@ -148,9 +145,7 @@ func (g *GroupOf[T]) Routers() []*mux.RouterOf[T] { } func (g *GroupOf[T]) Remove(name string) { - g.routers = sliceutil.Delete(g.routers, func(r *routerOf[T], _ int) bool { - return r.r.Name() == name - }) + g.routers = slices.DeleteFunc(g.routers, func(r *routerOf[T]) bool { return r.r.Name() == name }) } func (g *GroupOf[T]) Routes() map[string]map[string][]string { diff --git a/internal/options/cors.go b/internal/options/cors.go index 912cc43..bd3328d 100644 --- a/internal/options/cors.go +++ b/internal/options/cors.go @@ -7,11 +7,10 @@ package options import ( "errors" "net/http" + "slices" "strconv" "strings" - "github.com/issue9/sliceutil" - "github.com/issue9/mux/v7/types" ) @@ -85,8 +84,7 @@ func (c *CORS) Handle(node types.Node, wh http.Header, r *http.Request) { if preflight { // Access-Control-Allow-Methods - methods := node.Methods() - if !inStrings(methods, reqMethod) { + if slices.Index(node.Methods(), reqMethod) < 0 { return } wh.Set("Access-Control-Allow-Methods", node.AllowHeader()) @@ -111,7 +109,7 @@ func (c *CORS) Handle(node types.Node, wh http.Header, r *http.Request) { allowOrigin := "*" if !c.anyOrigins { origin := r.Header.Get("Origin") - if !inStrings(c.Origins, origin) { + if slices.Index(c.Origins, origin) < 0 { return } allowOrigin = origin @@ -142,15 +140,10 @@ func (c *CORS) headerIsAllowed(r *http.Request) bool { headers := strings.Split(h, ",") for _, v := range headers { - v = strings.TrimSpace(v) - if !inStrings(c.AllowHeaders, v) { + if slices.Index(c.AllowHeaders, strings.TrimSpace(v)) < 0 { return false } } return true } - -func inStrings(strs []string, s string) bool { - return sliceutil.Exists(strs, func(e string, _ int) bool { return e == s }) -} diff --git a/router.go b/router.go index 61ca18d..0148671 100644 --- a/router.go +++ b/router.go @@ -257,7 +257,7 @@ func (p *PrefixOf[T]) URL(strict bool, pattern string, params map[string]string) return p.router.URL(strict, p.prefix+pattern, params) } -// Prefix 在现有 PrefixOf 的基础上声明一个新的 PrefixOf 实例 +// Prefix 在现有 PrefixOf 的基础上声明一个新的 [PrefixOf] 实例 // // m 中间件函数,按顺序调用,会继承 p 的中间件并按在 m 之前; func (p *PrefixOf[T]) Prefix(prefix string, m ...types.MiddlewareOf[T]) *PrefixOf[T] { diff --git a/router_test.go b/router_test.go index cd5302c..af98485 100644 --- a/router_test.go +++ b/router_test.go @@ -6,13 +6,13 @@ package mux_test import ( "net/http" + "slices" "sort" "strings" "testing" "github.com/issue9/assert/v4" "github.com/issue9/assert/v4/rest" - "github.com/issue9/sliceutil" "github.com/issue9/mux/v7" "github.com/issue9/mux/v7/examples/std" @@ -360,7 +360,7 @@ func TestPrefixOf(t *testing.T) { // remove p.Remove("/h/any", http.MethodPut, http.MethodGet) - methods := sliceutil.Delete(mux.Methods(), func(s string, _ int) bool { + methods := slices.DeleteFunc(mux.Methods(), func(s string) bool { return s == http.MethodGet || s == http.MethodPut || s == http.MethodHead // 删除了 GET,HEAD 也会删除。 }) sort.Strings(methods) // TODO(go1.21): slices.Sort