From 3ceadd199967b84e026658ed199b7788bf330857 Mon Sep 17 00:00:00 2001 From: caixw Date: Mon, 2 Dec 2024 15:03:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor(openapi):=20MarkdownProblems=20?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=20=20titleLevel=20=E7=A1=AE=E5=AE=9A?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E5=BF=BD=E7=95=A5=20detail?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- locales/zh.yaml | 1 + openapi/utils.go | 26 +++++++++++++++++++++++--- openapi/utils_test.go | 9 ++++----- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/locales/zh.yaml b/locales/zh.yaml index 443b3167..116de5e8 100644 --- a/locales/zh.yaml +++ b/locales/zh.yaml @@ -1,6 +1,7 @@ languages: - zh-Hans - cmn-Hans + - zh-CN messages: - key: SSE keep alive cron message: diff --git a/openapi/utils.go b/openapi/utils.go index ff31986b..75759ac2 100644 --- a/openapi/utils.go +++ b/openapi/utils.go @@ -96,11 +96,31 @@ func getPathParams(path string) []string { // MarkdownProblems 将 problems 的内容生成为 markdown // -// titleLevlel 标题的级别,1-6; -func MarkdownProblems(s web.Server, titleLevlel int) web.LocaleStringer { +// titleLevel 标题的级别,0-6: +// 如果取值为 0,表示以列表的形式输出,并忽略 detail 字段的内容。 +// 1-6 表示输出 detail 内容,并且将 type 和 title 作为标题; +func MarkdownProblems(s web.Server, titleLevel int, detail bool) web.LocaleStringer { + if detail { + return markdownProblemsWithDetail(s, titleLevel) + } else { + return markdownProblemsWithoutDetail(s) + } +} + +func markdownProblemsWithoutDetail(s web.Server) web.LocaleStringer { + buf := &errwrap.Buffer{} + args := make([]any, 0, 30) + s.Problems().Visit(func(status int, lp *web.LocaleProblem) { + buf.Printf("- %s", lp.Type()).WString(": %s\n\n") + args = append(args, lp.Title) + }) + return web.Phrase(buf.String(), args...) +} + +func markdownProblemsWithDetail(s web.Server, titleLevel int) web.LocaleStringer { buf := &errwrap.Buffer{} + ss := strings.Repeat("#", titleLevel) - ss := strings.Repeat("#", titleLevlel) args := make([]any, 0, 30) s.Problems().Visit(func(status int, lp *web.LocaleProblem) { buf.Printf("%s %s ", ss, lp.Type()). diff --git a/openapi/utils_test.go b/openapi/utils_test.go index 605bf62c..8a4d8b62 100644 --- a/openapi/utils_test.go +++ b/openapi/utils_test.go @@ -54,15 +54,14 @@ func TestMarkdownProblems(t *testing.T) { ss := newServer(a) p := ss.Locale().NewPrinter(language.SimplifiedChinese) - txt := MarkdownProblems(ss, 2) + txt := MarkdownProblems(ss, 2, true) lines := strings.Split(txt.LocaleString(p), "\n\n") a.Equal(lines[0], "## 400 Bad Request"). Equal(lines[1], "表示客户端错误,比如,错误的请求语法、无效的请求消息帧或欺骗性的请求路由等,服务器无法或不会处理该请求。"). Equal(lines[2], "## 401 Unauthorized") - txt = MarkdownProblems(ss, 3) + txt = MarkdownProblems(ss, 3, false) lines = strings.Split(txt.LocaleString(p), "\n\n") - a.Equal(lines[0], "### 400 Bad Request"). - Equal(lines[1], "表示客户端错误,比如,错误的请求语法、无效的请求消息帧或欺骗性的请求路由等,服务器无法或不会处理该请求。"). - Equal(lines[2], "### 401 Unauthorized") + a.Equal(lines[0], "- 400: Bad Request"). + Equal(lines[1], "- 401: Unauthorized") }