From fce39068cae571edd8bcd916ffe4d2f7620df61a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 8 Feb 2025 18:52:12 +0800 Subject: [PATCH] save code --- define/openapi.go | 2 +- generate.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/define/openapi.go b/define/openapi.go index e8da7ae..a7e816b 100644 --- a/define/openapi.go +++ b/define/openapi.go @@ -122,7 +122,7 @@ type Schema struct { Discriminator *SchemaDiscriminator `json:"discriminator,omitempty"` // 说白了, 就是一个字段可能是不同的数据结构。。。 ReadOnly bool `json:"readOnly"` // 仅与 Schema "properties" 定义有关。 声明此属性是 "readonly" 的。这意味着它可以作为 response 的一部分但不应该作为 request 的一部分被发送。如果一个 property 的 readOnly 被标记为 true 且在 required 列表中,required 将只作用于 response。一个 property 的 readOnly 和 writeOnly 不允许同时被标记为 true。默认值是 false。 WriteOnly bool `json:"writeOnly"` // 仅与 Schema "properties" 定义有关。声明此 property 为 "write only"。所以它可以作为 request 的一部分而不应该作为 response 的一部分被发送。如果一个 property 的 writeOnly 被标记为 true 且在 required 列表中,required 将只作用于 request。一个 property 的 readOnly 和 writeOnly 不能同时被标记为 true。默认值是 false。 - Xml *XML `json:"xml,omitempty"` // 这只能用于 properties schemas,在 root schemas 中没有效果。 + Xml *XML `json:"xml,omitempty"` // 这只能用于 properties schemas,在root schemas 中没有效果。 ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"` // 此 schema 附加的外部文档。 Example string `json:"example,omitempty"` // 一个用于示范此 schema实例的示例,可以是任意格式。为了表达无法用 JSON 或 YAML 格式呈现的示例,可以使用 string 类型的值,且在必要的地方需要使用字符转义。 Deprecated bool `json:"deprecated"` // 表示一个 schema 是废弃的,应该逐渐被放弃使用。默认值是 false. diff --git a/generate.go b/generate.go index 1beafcc..aa69328 100644 --- a/generate.go +++ b/generate.go @@ -10,6 +10,7 @@ package api_doc import ( "git.zhangdeman.cn/gateway/api-doc/define" "git.zhangdeman.cn/zhangdeman/consts" + "reflect" "strings" ) @@ -149,7 +150,7 @@ type PathItemOperationConfig struct { // &define.UriBaseConfig{ // Uri: "/foo/bar", // Method: http.MethodPost, -// ContentType: "application/json", +// ContentType: ["application/json"], // TagList: []string{"测试标签"}, // Summary: "这是一份示例基础配置", // Description: "这是一份示例基础配置", @@ -161,3 +162,45 @@ type PathItemOperationConfig struct { func (g *Generate) AddApi(baseCfg *define.UriBaseConfig, paramList []*define.ParamConfig, resultList []*define.ResultConfig) error { return nil } + +// AddComponentsSchema 添加schema +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:25 2025/2/8 +func (g *Generate) AddComponentsSchema(pkgPath string, inputType reflect.Type) { + if nil == g.docData.Components { + g.docData.Components = &define.Components{ + Schemas: map[string]*define.Schema{}, + } + } + if nil == g.docData.Components.Schemas { + g.docData.Components.Schemas = make(map[string]*define.Schema) + } + if len(pkgPath) == 0 { + pkgPath = inputType.PkgPath() + } + schemaPrefix := "" + if "" != pkgPath { + schemaPrefix = pkgPath + "." + } + if inputType.Kind() == reflect.Ptr { + inputType = inputType.Elem() + } + if inputType.Kind() == reflect.Map { + // map, 直接添加公共 + if _, exist := g.docData.Components.Schemas[schemaPrefix+inputType.Name()]; !exist { + g.docData.Components.Schemas[schemaPrefix+inputType.Name()] = &define.Schema{ + Type: consts.SwaggerDataTypeObject, + } + } + return + } + // 数组 + if inputType.Kind() == reflect.Slice { + } + // 结构体 + if inputType.Kind() == reflect.Struct { + + } +}