From 9a32c07b5fbffac7edd813644ef86607261d9955 Mon Sep 17 00:00:00 2001 From: caixw Date: Fri, 8 Mar 2024 15:51:43 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4=E9=83=A8?= =?UTF-8?q?=E5=88=86=E7=BF=BB=E8=AF=91=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- cmd/web/go.mod | 2 +- cmd/web/go.sum | 4 ++-- cmd/web/locales/und.yaml | 3 --- cmd/web/locales/zh-CN.yaml | 3 --- cmd/web/restdoc/parser/restdoc.go | 3 ++- go.mod | 2 +- go.sum | 4 ++-- locales/locales.go | 13 +++++++++---- locales/und.yaml | 9 ++++++--- locales/zh-CN.yaml | 9 ++++++--- server/codec.go | 4 ++-- server/http.go | 4 ++-- server/logs.go | 4 ++-- server/micro.go | 6 +++--- web.go | 2 +- 16 files changed, 42 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 3cd9d4bb..315b4e81 100644 --- a/README.md +++ b/README.md @@ -63,10 +63,10 @@ Content-Type 则可以有向服务器指定提交内容的文档类型和字符 框架根据 [RFC7807](https://datatracker.ietf.org/doc/html/rfc7807) 提供了一种输出错误信息内容的机制。 -## 中间件 +## 插件 -- 提供了常用的中间件。 -- 提供了常用的验证方法。 +- 提供了常用的中间件和插件。 +- 提供了常用的验证和修正数据的方法。 ## 工具 diff --git a/cmd/web/go.mod b/cmd/web/go.mod index de24d6e0..8521f97e 100644 --- a/cmd/web/go.mod +++ b/cmd/web/go.mod @@ -7,7 +7,7 @@ require ( github.com/getkin/kin-openapi v0.123.0 github.com/issue9/assert/v4 v4.1.1 github.com/issue9/cmdopt v0.13.1 - github.com/issue9/localeutil v0.26.4 + github.com/issue9/localeutil v0.26.5 github.com/issue9/logs/v7 v7.5.0 github.com/issue9/query/v3 v3.1.3 github.com/issue9/source v0.8.2 diff --git a/cmd/web/go.sum b/cmd/web/go.sum index 7b6b135d..f4f48681 100644 --- a/cmd/web/go.sum +++ b/cmd/web/go.sum @@ -28,8 +28,8 @@ github.com/issue9/conv v1.3.5 h1:UWeA+Zqp5vjNDLrmhLhaXPjS1hL8gh4quX6Shk5njKQ= github.com/issue9/conv v1.3.5/go.mod h1:lkZYMyrmxy+HK//N4eLmwUl9mCwj0zgeTjnxgl/w7hM= 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/localeutil v0.26.4 h1:rfBC3YAt9PLHXHc5unreZpYjlEt+xCnVOUr7QDAveiw= -github.com/issue9/localeutil v0.26.4/go.mod h1:BJXJwcAT9CyyVZOlqfmq+B5FcPbqGxGjYnTYbVuiMM8= +github.com/issue9/localeutil v0.26.5 h1:e78b6cOOtgzfb4g4U9uPLC8QyK6Lux+s7ZiQe+6iM1A= +github.com/issue9/localeutil v0.26.5/go.mod h1:BJXJwcAT9CyyVZOlqfmq+B5FcPbqGxGjYnTYbVuiMM8= github.com/issue9/logs/v7 v7.5.0 h1:IAzqaRXuhNzTvoZVwFJWnWOxCiqhsiqxOt3GuAvJaWQ= github.com/issue9/logs/v7 v7.5.0/go.mod h1:zPhcpznbDmfApbtrZHeHDvngfNAcuNhgo9pJfKD8nXY= github.com/issue9/mux/v7 v7.4.1 h1:FG5iu44zd4GVIdzj7/oZgDYBaXnsRnrXWMZANcNhVIk= diff --git a/cmd/web/locales/und.yaml b/cmd/web/locales/und.yaml index d3ec9655..c2471813 100755 --- a/cmd/web/locales/und.yaml +++ b/cmd/web/locales/und.yaml @@ -121,9 +121,6 @@ messages: - key: ignore %s message: msg: ignore %s - - key: invalid format - message: - msg: invalid format - key: invalid http method %s message: msg: invalid http method %s diff --git a/cmd/web/locales/zh-CN.yaml b/cmd/web/locales/zh-CN.yaml index a5ca9af4..47479be1 100755 --- a/cmd/web/locales/zh-CN.yaml +++ b/cmd/web/locales/zh-CN.yaml @@ -144,9 +144,6 @@ messages: - key: ignore %s message: msg: 忽略 %s - - key: invalid format - message: - msg: 无效的格式 - key: invalid http method %s message: msg: 无效的请求方法 %s diff --git a/cmd/web/restdoc/parser/restdoc.go b/cmd/web/restdoc/parser/restdoc.go index 08d38372..e18a459e 100644 --- a/cmd/web/restdoc/parser/restdoc.go +++ b/cmd/web/restdoc/parser/restdoc.go @@ -15,6 +15,7 @@ import ( "github.com/getkin/kin-openapi/openapi3" "github.com/issue9/version" "github.com/issue9/web" + "github.com/issue9/web/locales" "github.com/issue9/web/cmd/web/git" "github.com/issue9/web/cmd/web/restdoc/openapi" @@ -384,7 +385,7 @@ func (p *Parser) parseOpenAPI(target *openapi.OpenAPI, suffix, filename string, modCache := filepath.Join(build.Default.GOPATH, "pkg", "mod") uri = &url.URL{Path: filepath.Join(modCache, words[0], words[1])} default: - p.l.Error(web.StringPhrase("invalid format"), filename, ln) + p.l.Error(locales.InvalidFormat, filename, ln) return } diff --git a/go.mod b/go.mod index 67bcf0a7..f4971f63 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/issue9/config v0.6.2 github.com/issue9/conv v1.3.5 github.com/issue9/errwrap v0.3.2 - github.com/issue9/localeutil v0.26.4 + github.com/issue9/localeutil v0.26.5 github.com/issue9/logs/v7 v7.5.0 github.com/issue9/mux/v7 v7.4.1 github.com/issue9/query/v3 v3.1.3 diff --git a/go.sum b/go.sum index ac805c2a..39ae1b06 100644 --- a/go.sum +++ b/go.sum @@ -20,8 +20,8 @@ github.com/issue9/conv v1.3.5 h1:UWeA+Zqp5vjNDLrmhLhaXPjS1hL8gh4quX6Shk5njKQ= github.com/issue9/conv v1.3.5/go.mod h1:lkZYMyrmxy+HK//N4eLmwUl9mCwj0zgeTjnxgl/w7hM= 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/localeutil v0.26.4 h1:rfBC3YAt9PLHXHc5unreZpYjlEt+xCnVOUr7QDAveiw= -github.com/issue9/localeutil v0.26.4/go.mod h1:BJXJwcAT9CyyVZOlqfmq+B5FcPbqGxGjYnTYbVuiMM8= +github.com/issue9/localeutil v0.26.5 h1:e78b6cOOtgzfb4g4U9uPLC8QyK6Lux+s7ZiQe+6iM1A= +github.com/issue9/localeutil v0.26.5/go.mod h1:BJXJwcAT9CyyVZOlqfmq+B5FcPbqGxGjYnTYbVuiMM8= github.com/issue9/logs/v7 v7.5.0 h1:IAzqaRXuhNzTvoZVwFJWnWOxCiqhsiqxOt3GuAvJaWQ= github.com/issue9/logs/v7 v7.5.0/go.mod h1:zPhcpznbDmfApbtrZHeHDvngfNAcuNhgo9pJfKD8nXY= github.com/issue9/mux/v7 v7.4.1 h1:FG5iu44zd4GVIdzj7/oZgDYBaXnsRnrXWMZANcNhVIk= diff --git a/locales/locales.go b/locales/locales.go index 503acdb0..761b2941 100644 --- a/locales/locales.go +++ b/locales/locales.go @@ -35,6 +35,7 @@ var Locales = []fs.FS{ // 一些多处用到的翻译项 const ( + InvalidFormat = localeutil.StringPhrase("invalid format") InvalidValue = localeutil.StringPhrase("invalid value") CanNotBeEmpty = localeutil.StringPhrase("can not be empty") DuplicateValue = localeutil.StringPhrase("duplicate value") @@ -43,17 +44,21 @@ const ( ) // ShouldGreatThan 返回必须大于 n 的翻译项 -func ShouldGreatThan(n int) localeutil.Stringer { - return localeutil.Phrase("should great than %d", n) -} +func ShouldGreatThan(n int) localeutil.Stringer { return localeutil.Phrase("should great than %d", n) } + +//---------------------------- 以下为本地化的错误实例 ----------------------------- var ( + errInvalidFormat = localeutil.Error("invalid format") errInvalidValue = localeutil.Error("invalid value") errCanNotBeEmpty = localeutil.Error("can not be empty") + errNotFound = localeutil.Error("not found") ) +func ErrInvalidFormat() error { return errInvalidFormat } + func ErrInvalidValue() error { return errInvalidValue } func ErrCanNotBeEmpty() error { return errCanNotBeEmpty } -func ErrNotFound(s string) error { return localeutil.Error("%s not found", s) } +func ErrNotFound() error { return errNotFound } diff --git a/locales/und.yaml b/locales/und.yaml index 90a4c39a..23156a7d 100755 --- a/locales/und.yaml +++ b/locales/und.yaml @@ -1,8 +1,5 @@ id: und messages: - - key: '%s not found' - message: - msg: '%s not found' - key: SSE keep alive cron message: msg: SSE keep alive cron @@ -30,6 +27,9 @@ messages: - key: invalid data %s message: msg: invalid data %s + - key: invalid format + message: + msg: invalid format - key: invalid sse format %s message: msg: invalid sse format %s @@ -39,6 +39,9 @@ messages: - key: no available peer message: msg: no available peer + - key: not found + message: + msg: not found - key: not found charset for %s message: msg: not found charset for %s diff --git a/locales/zh-CN.yaml b/locales/zh-CN.yaml index d2c80763..58480e8e 100644 --- a/locales/zh-CN.yaml +++ b/locales/zh-CN.yaml @@ -1,8 +1,5 @@ id: zh-CN messages: - - key: '%s not found' - message: - msg: '%s 不存在' - key: SSE keep alive cron message: msg: SSE 向已链接用户发送的心跳包 @@ -30,6 +27,9 @@ messages: - key: invalid data %s message: msg: invalid data %s + - key: invalid format + message: + msg: 无效的格式 - key: invalid sse format %s message: msg: '无效的 SSE 格式: %s' @@ -39,6 +39,9 @@ messages: - key: no available peer message: msg: 没有有效的节点 + - key: not found + message: + msg: 未找到 - key: not found charset for %s message: msg: 未找到符合报头 %s 的字符集 diff --git a/server/codec.go b/server/codec.go index 07a4bab0..ce87f8cf 100644 --- a/server/codec.go +++ b/server/codec.go @@ -206,7 +206,7 @@ func (conf *configOf[T]) sanitizeCompresses() *web.FieldError { enc, found := compressorFactory.get(e.ID) if !found { field := "compresses[" + strconv.Itoa(index) + "].id" - return web.NewFieldError(field, locales.ErrNotFound(e.ID)) + return web.NewFieldError(field, locales.ErrNotFound()) } conf.compressors = append(conf.compressors, &Compression{ @@ -230,7 +230,7 @@ func (conf *configOf[T]) sanitizeMimetypes() *web.FieldError { for index, item := range conf.Mimetypes { m, found := mimetypesFactory.get(item.Target) if !found { - return web.NewFieldError("mimetypes["+strconv.Itoa(index)+"].target", locales.ErrNotFound(item.Target)) + return web.NewFieldError("mimetypes["+strconv.Itoa(index)+"].target", locales.ErrNotFound()) } ms = append(ms, &Mimetype{ diff --git a/server/http.go b/server/http.go index 29e2d8e9..d4c52c4e 100644 --- a/server/http.go +++ b/server/http.go @@ -129,11 +129,11 @@ func exists(p string) bool { func (cert *certificateConfig) sanitize() *web.FieldError { if !exists(cert.Cert) { - return web.NewFieldError("cert", locales.ErrNotFound(cert.Cert)) + return web.NewFieldError("cert", locales.ErrNotFound()) } if !exists(cert.Key) { - return web.NewFieldError("key", locales.ErrNotFound(cert.Key)) + return web.NewFieldError("key", locales.ErrNotFound()) } return nil diff --git a/server/logs.go b/server/logs.go index 215f6e74..31cea7ab 100644 --- a/server/logs.go +++ b/server/logs.go @@ -262,7 +262,7 @@ func (conf *logsConfig) buildHandler() (logs.Handler, []func() error, *web.Field f, found := logHandlersFactory.get(item.Type) if !found { - return nil, nil, web.NewFieldError("handlers[0].type", locales.ErrNotFound(item.Type)) + return nil, nil, web.NewFieldError("handlers[0].type", locales.ErrNotFound()) } ww, c, err := f(item.Args) @@ -284,7 +284,7 @@ func (conf *logsConfig) buildHandler() (logs.Handler, []func() error, *web.Field f, found := logHandlersFactory.get(w.Type) if !found { - return nil, nil, web.NewFieldError(field+".type", locales.ErrNotFound(w.Type)) + return nil, nil, web.NewFieldError(field+".type", locales.ErrNotFound()) } ww, c, err := f(w.Args) diff --git a/server/micro.go b/server/micro.go index 32bc125a..ca513261 100644 --- a/server/micro.go +++ b/server/micro.go @@ -99,7 +99,7 @@ func (conf *configOf[T]) buildMicro(c web.Cache) *web.FieldError { for i, m := range conf.Mappers { mm, found := routerMatcherFactory.get(m.Matcher) if !found { - return web.NewFieldError("mappers["+strconv.Itoa(i)+"].matcher", locales.ErrNotFound(m.Matcher)) + return web.NewFieldError("mappers["+strconv.Itoa(i)+"].matcher", locales.ErrNotFound()) } conf.mapper[m.Name] = mm(strings.Split(m.Args, ",")...) } @@ -111,12 +111,12 @@ func (conf *configOf[T]) buildMicro(c web.Cache) *web.FieldError { func (r *registryConfig) build(c web.Cache) *web.FieldError { t, found := typeFactory.get(r.Type) if !found { - return web.NewFieldError("type", locales.ErrNotFound(r.Type)) + return web.NewFieldError("type", locales.ErrNotFound()) } s, found := strategyFactory.get(r.Strategy) if !found { - return web.NewFieldError("strategy", locales.ErrNotFound(r.Strategy)) + return web.NewFieldError("strategy", locales.ErrNotFound()) } r.s = s diff --git a/web.go b/web.go index 2987e269..97459d99 100644 --- a/web.go +++ b/web.go @@ -24,7 +24,7 @@ import ( ) // Version 当前框架的版本 -const Version = "0.87.3" +const Version = "0.87.4" type ( Logger = logs.Logger