Skip to content

Commit

Permalink
chore: 最低版本更新到 go1.21
Browse files Browse the repository at this point in the history
去掉不必要的依赖 sliceutil
  • Loading branch information
caixw committed Mar 29, 2024
1 parent a759b9a commit 3c2a5a8
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 36 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -59,4 +59,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
8 changes: 4 additions & 4 deletions .github/workflows/go.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: Check out code into the Go module directory
Expand All @@ -25,7 +25,7 @@ jobs:

- name: Vet
run: go vet -v ./...

- name: Test
run: go test -race -v -coverprofile='coverage.txt' -covermode=atomic ./...

Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
11 changes: 3 additions & 8 deletions group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()))
}

Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 4 additions & 11 deletions internal/options/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ package options
import (
"errors"
"net/http"
"slices"
"strconv"
"strings"

"github.com/issue9/sliceutil"

"github.com/issue9/mux/v7/types"
)

Expand Down Expand Up @@ -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())
Expand All @@ -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
Expand Down Expand Up @@ -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 })
}
2 changes: 1 addition & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down
4 changes: 2 additions & 2 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3c2a5a8

Please sign in to comment.