规划swagger文档逻辑

This commit is contained in:
白茶清欢 2024-12-24 10:34:28 +08:00
parent fb9e22ac66
commit d069ead94e
6 changed files with 66 additions and 29 deletions

View File

@ -7,18 +7,31 @@
// Date : 2024-08-14 15:20
package define
// DocParseResult 文档解析结果
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:30 2024/12/24
type DocParseResult struct {
Domain string `json:"domain"` // 域名
Description string `json:"description"` // 描述
UriList []string `json:"uri_list"` // 接口列表
}
// UriBaseConfig 添加接口时的基础配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:21 2024/8/14
type UriBaseConfig struct {
Uri string `json:"uri"` // 接口路由
Method string `json:"method"` // 接口请求方法
ContentType string `json:"content_type"` // 接口请求类型
TagList []string `json:"tag_list"` // 接口标签列表
Summary string `json:"summary"` // 接口摘要描述
Description string `json:"description"` // 接口详细描述
Uri string `json:"uri"` // 接口路由
Method string `json:"method"` // 接口请求方法
ContentType string `json:"content_type"` // 接口请求类型
TagList []string `json:"tag_list"` // 接口标签列表
Summary string `json:"summary"` // 接口摘要描述
Description string `json:"description"` // 接口详细描述
ParamList []*ParamConfig `json:"param_list"` // 参数列表
ResultList []*ResultConfig `json:"result_list"` // 返回值列表
}
// ParamConfig 参数配置

View File

@ -42,8 +42,16 @@ type SwaggerPathConfigParameter struct {
//
// Date : 16:57 2024/4/19
type SwaggerPathConfigResponse struct {
Description string `json:"description"` // 返回值描述
Schema map[string]string `json:"schema"` // 返回值结构
Description string `json:"description"` // 返回值描述
Schema *SwaggerPathConfigResponseSchema `json:"schema"` // 返回值结构
}
type SwaggerPathConfigResponseSchema struct {
Type string `json:"type"` // 数据类型
Items struct {
Ref string `json:"$ref"` // type = array 时, 引用的每一项数据结构
} `json:"items"`
Ref string `json:"$ref"` // 引用的数据结构
}
// SwaggerDefinition ...

5
go.mod
View File

@ -4,12 +4,13 @@ go 1.22.2
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125100843-b1b286c7a701
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240422034417-8c922be06d95
github.com/tidwall/gjson v1.16.0
)
require (
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 // indirect
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd // indirect
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
@ -17,5 +18,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mozillazg/go-pinyin v0.20.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

25
go.sum
View File

@ -1,27 +1,13 @@
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240425091616-00e0a924efcd h1:RBFDiEMI97fuzpyb5HBN4lu3UXTAGYo6nlGhV2gWq5U=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240425091616-00e0a924efcd/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240722080005-ca68a3ff8bc7 h1:8wJlcuJPhEHKdNIENvcxGiZwwOgjclvKwgKt/MU6WWI=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240722080005-ca68a3ff8bc7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240726024939-e424db29c5c4 h1:mibnyzYbZullK0aTHVASHl3UeoVr8IgytQZsuyv+yEM=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240726024939-e424db29c5c4/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240817091513-491f455a23c0 h1:U12XDtyRrmsqb/wRvRZG9+SBKMCGFNADpiLogsp5POw=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240817091513-491f455a23c0/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125100843-b1b286c7a701 h1:G+lGQmjMOBWGspZfijZvenGUAKpjBBrkRXLg3+GZp0U=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125100843-b1b286c7a701/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd h1:q7GG14qgXKB4MEXQFOe7/UYebsqMfPaSX80TcPdOosI=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd/go.mod h1:+D6uPSljwHywjVY5WSBY4TRVMj26TN5f5cFGEYMldjs=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 h1:GO3oZa5a2sqwAzGcLDJtQzmshSWRmoP7IDS8bwFqvC4=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240422034417-8c922be06d95 h1:3lO4ap9p7kEA+4yL5ojG9mAWsz5sY28Nu2tSzAZEehw=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240422034417-8c922be06d95/go.mod h1:Fo4XOiZPua4E4/Qzy3ZYS5zyd15bS/lsb3t6S6PQFGY=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
@ -38,8 +24,15 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/gjson v1.16.0 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg=
github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@ -8,6 +8,8 @@
package api_doc
import (
"errors"
"git.zhangdeman.cn/gateway/api-doc/define"
"git.zhangdeman.cn/zhangdeman/serialize"
"github.com/tidwall/gjson"
"strings"
@ -18,7 +20,7 @@ import (
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:54 2024/12/23
func Parse(docUrl string) (any, error) {
func Parse(docUrl string) (*define.DocParseResult, error) {
var (
err error
docContent []byte
@ -28,17 +30,30 @@ func Parse(docUrl string) (any, error) {
return nil, err
}
swaggerVersion := gjson.GetBytes(docContent, "swagger").String()
if "" == swaggerVersion || strings.HasPrefix(swaggerVersion, "3.") {
if "" == swaggerVersion || !strings.HasPrefix(swaggerVersion, "2.") {
// 未指定swagger版本或swagger版本3.x
return ParseOpenapi3(docContent)
}
return ParseSwagger2(docContent)
}
func ParseOpenapi3(docContent []byte) (any, error) {
// ParseSwagger2 解析swagger2.0版本文档
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:33 2024/12/24
func ParseSwagger2(docContent []byte) (*define.DocParseResult, error) {
var (
err error
swaggerDoc define.Swagger
)
if err = serialize.JSON.UnmarshalWithNumber(docContent, &swaggerDoc); nil != err {
return nil, errors.New("parse swagger json fail : " + err.Error())
}
return nil, nil
}
func ParseSwagger2(docContent []byte) (any, error) {
func ParseOpenapi3(docContent []byte) (*define.DocParseResult, error) {
return nil, nil
}

View File

@ -32,3 +32,8 @@ func Test_parser_Openapi3(t *testing.T) {
fmt.Println("解析成功")
}
}
func TestParseForSwagger(t *testing.T) {
docUrl := "https://git.zhangdeman.cn/swagger.v1.json"
_, _ = Parse(docUrl)
}