Skip to content

Commit

Permalink
feat(cmd/web/enum): 添加生成 openapi 文档的参数 -openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Jan 7, 2025
1 parent 062fbc9 commit 22c1b9f
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 33 deletions.
21 changes: 12 additions & 9 deletions cmd/web/enum/enum.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2018-2024 caixw
// SPDX-FileCopyrightText: 2018-2025 caixw
//
// SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -43,12 +43,13 @@ const (
flags:
{{flags}}`)

outUsage = web.StringPhrase("set output file")
fhUsage = web.StringPhrase("set file header")
typeUsage = web.StringPhrase("set the enum type")
inputUsage = web.StringPhrase("set input file")
filterUsage = web.StringPhrase("gen filter method")
sqlUsage = web.StringPhrase("gen sql method")
outUsage = web.StringPhrase("set output file")
fhUsage = web.StringPhrase("set file header")
typeUsage = web.StringPhrase("set the enum type")
inputUsage = web.StringPhrase("set input file")
filterUsage = web.StringPhrase("gen filter method")
sqlUsage = web.StringPhrase("gen sql method")
openapiUsage = web.StringPhrase("gen openapi method")
)

func Init(opt *cmdopt.CmdOpt, p *localeutil.Printer) {
Expand All @@ -59,6 +60,7 @@ func Init(opt *cmdopt.CmdOpt, p *localeutil.Printer) {
i := fs.String("i", "", inputUsage.LocaleString(p))
filterM := fs.Bool("filter", true, filterUsage.LocaleString(p))
sqlM := fs.Bool("sql", true, sqlUsage.LocaleString(p))
openapiM := fs.Bool("openapi", true, openapiUsage.LocaleString(p))

return func(io.Writer) error {
if *i == "" {
Expand All @@ -75,7 +77,7 @@ func Init(opt *cmdopt.CmdOpt, p *localeutil.Printer) {
for i, name := range ts {
ts[i] = strings.TrimSpace(name)
}
return dump(*h, *i, *o, ts, *filterM, *sqlM)
return dump(*h, *i, *o, ts, *filterM, *sqlM, *openapiM)
}
})
}
Expand Down Expand Up @@ -141,7 +143,7 @@ func checkType(pkg *types.Package, t string) (types.Type, error) {
return nil, ErrNotAllowedType
}

func dump(header, input, output string, ts []string, filter, sql bool) error {
func dump(header, input, output string, ts []string, filter, sql, openapi bool) error {
input = strings.TrimLeft(input, "./\\")
fset := token.NewFileSet()
imp := importer.ForCompiler(fset, "source", nil)
Expand All @@ -161,6 +163,7 @@ func dump(header, input, output string, ts []string, filter, sql bool) error {
Enums: make([]*enum, 0, len(vals)),
Filter: filter,
SQL: sql,
OpenAPI: openapi,
}

for k, v := range vals {
Expand Down
4 changes: 2 additions & 2 deletions cmd/web/enum/enum_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2018-2024 caixw
// SPDX-FileCopyrightText: 2018-2025 caixw
//
// SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -70,7 +70,7 @@ func TestCheckType(t *testing.T) {
func TestDump(t *testing.T) {
a := assert.New(t, false)

err := dump("header", "./testdir/testdir.go", "./testdir/testdata.out", []string{"t1", "t3"}, true, true)
err := dump("header", "./testdir/testdir.go", "./testdir/testdata.out", []string{"t1", "t3"}, true, true, true)
a.NotError(err)
}

Expand Down
14 changes: 13 additions & 1 deletion cmd/web/enum/tpl.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2018-2024 caixw
// SPDX-FileCopyrightText: 2018-2025 caixw
//
// SPDX-License-Identifier: MIT

Expand All @@ -13,6 +13,7 @@ import(
{{if .SQL}}"database/sql/driver"{{end}}
{{if .Filter}}"github.com/issue9/web/filter"{{end}}
{{if .OpenAPI}}"github.com/issue9/web/openapi"{{end}}
"github.com/issue9/web/locales"
)
Expand Down Expand Up @@ -119,6 +120,17 @@ var(
)
{{end}}
{{if $.OpenAPI}}
func ({{.Name}}) OpenAPISchema(s *openapi.Schema) {
s.Type = openapi.TypeString
s.Enum = []any{
{{- range .Values -}}
{{.Name}}.String(),
{{- end -}}
}
}
{{end}}
//--------------------- end {{.Name}} --------------------
{{end}}
Expand Down
4 changes: 2 additions & 2 deletions cmd/web/enum/type.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2018-2024 caixw
// SPDX-FileCopyrightText: 2018-2025 caixw
//
// SPDX-License-Identifier: MIT

Expand All @@ -12,7 +12,7 @@ type data struct {
Enums []*enum

// 过滤方法
Filter, SQL bool
Filter, SQL, OpenAPI bool
}

// enum 枚举类型的数据
Expand Down
4 changes: 2 additions & 2 deletions cmd/web/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ require (
github.com/issue9/errwrap v0.3.2 // indirect
github.com/issue9/mux/v9 v9.1.2 // indirect
github.com/issue9/query/v3 v3.1.3 // indirect
github.com/issue9/scheduled v0.21.3 // indirect
github.com/issue9/scheduled v0.22.0 // indirect
github.com/issue9/sliceutil v0.17.0 // indirect
github.com/jellydator/ttlcache/v3 v3.3.0 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/tools v0.28.0 // indirect
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
)
8 changes: 4 additions & 4 deletions cmd/web/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ github.com/issue9/query/v3 v3.1.3 h1:Y6ETEYXxaKqhpM4lXPKCffhJ72VuKQbrAwgwHlacu0Y
github.com/issue9/query/v3 v3.1.3/go.mod h1:a/W/+7iel9K+5rRT4AFAKR8+OJeV5axeF6tK9My4lNA=
github.com/issue9/rands/v3 v3.0.1 h1:EnX9WNushGgHCzoL/R5eBPaLfvjLO/c7CGHNgLK0JhY=
github.com/issue9/rands/v3 v3.0.1/go.mod h1:n4mM2ts7NCpuxHwS9zorPITJBWEUGksXg6cTOH6yqS0=
github.com/issue9/scheduled v0.21.3 h1:22R1jl8rI+wrepHKZA1GfpZ6rpj5DLxVj69f0Vzrwfc=
github.com/issue9/scheduled v0.21.3/go.mod h1:o9Z7g50Ld1pOozYQ4RqRLKDrLcQP/dOhSd84A0c6d2w=
github.com/issue9/scheduled v0.22.0 h1:qGk4+IbaxozIEaYX9mWEM1C//Zg/iI8DVT6tYIS/ibU=
github.com/issue9/scheduled v0.22.0/go.mod h1:wVGvPy6kxstLwkfQIYEfpORl+gNKTW68ydFA4d78fMQ=
github.com/issue9/sliceutil v0.17.0 h1:EtmVlldkAyGxS0O2TnxcAu3pgLJw74I5eh9DHT7TOZs=
github.com/issue9/sliceutil v0.17.0/go.mod h1:CVpH4f228pICY7JImlztUPe/zf2w0EicEleU9YfFe00=
github.com/issue9/source v0.11.7 h1:wyZv2MExD1kem7FGxIy6/iSgDHqNpLox6dAZfP7VzKM=
Expand Down Expand Up @@ -75,8 +75,8 @@ golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
Expand Down
3 changes: 3 additions & 0 deletions cmd/web/locales/und.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ messages:
- key: gen htmldoc
message:
msg: gen htmldoc
- key: gen openapi method
message:
msg: gen openapi method
- key: gen sql method
message:
msg: gen sql method
Expand Down
3 changes: 3 additions & 0 deletions cmd/web/locales/zh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ messages:
- key: gen htmldoc
message:
msg: 根据结构体中的字段生成 html 文档
- key: gen openapi method
message:
msg: 生成适配 github.com/issue9/web/openapi 的相关方法
- key: gen sql method
message:
msg: 生成适配 database/sql 的相关方法
Expand Down
2 changes: 1 addition & 1 deletion filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (ctx *Context) NewFilterContext(exitAtError bool) *FilterContext {
// New 声明验证的子对象
//
// name 为 f 中验证对象的整体名称;
// f 为验证方法,其原型为 func(fp *FilterContext)
// f 为验证方法,其原型为 func(c *FilterContext)
// 往 c 参数写入的信息,其字段名均会以 name 作为前缀写入到当前对象 v 中。
// c 的各种属性均继承自 v。
func (v *FilterContext) New(name string, f func(c *FilterContext)) *FilterContext {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/klauspost/compress v1.17.11
github.com/puzpuzpuz/xsync/v3 v3.4.0
github.com/wk8/go-ordered-map/v2 v2.1.8
golang.org/x/crypto v0.31.0
golang.org/x/crypto v0.32.0
golang.org/x/text v0.21.0
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -44,6 +44,6 @@ require (
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/sys v0.29.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZ
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da h1:noIWHXmPHxILtqtCOPIhSt0ABwskkZKjD3bXGnZGpNY=
Expand Down
2 changes: 1 addition & 1 deletion locales/zh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ messages:
msg: 无效的值
- key: keep alive for %s
message:
msg: 向 %s 的用户发送心跳包
msg: 向 %s 的用户发送心跳包
- key: no available peer
message:
msg: 没有有效的节点
Expand Down
8 changes: 4 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ type (
InternalServer struct {
server Server

name string
id string
version string

locale *locale.Locale
Expand Down Expand Up @@ -233,7 +233,7 @@ type (
// 与 新实现的 Location 返回不同值的情况。
func InternalNewServer(
s Server,
name, ver string,
id, ver string,
loc *time.Location,
logs *Logs,
idgen func() string,
Expand All @@ -248,7 +248,7 @@ func InternalNewServer(
is := &InternalServer{
server: s,

name: name,
id: id,
version: ver,

locale: l,
Expand Down Expand Up @@ -287,7 +287,7 @@ func (s *InternalServer) Config() *config.Config { return s.locale.Config() }

func (s *InternalServer) Locale() Locale { return s.locale }

func (s *InternalServer) ID() string { return s.name }
func (s *InternalServer) ID() string { return s.id }

func (s *InternalServer) Version() string { return s.version }

Expand Down
1 change: 1 addition & 0 deletions server/config/CONFIG.html
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ <h2 id="mimetypeConfig">mimetypeConfig</h2>
<li>form
<li>html
<li>gob
<li>yaml
<li>nop 没有具体实现的方法,对于上传等需要自行处理的情况可以指定此值。
</ul>
</td></tr>
Expand Down
2 changes: 1 addition & 1 deletion web.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

// Version 当前框架的版本
const Version = "0.100.8"
const Version = "0.100.9"

type (
Logger = logs.Logger
Expand Down

0 comments on commit 22c1b9f

Please sign in to comment.