11 Star 11 Fork 0

Gitee 极速下载/goa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/goadesign/goa
克隆/下载
openapi.go 1.62 KB
一键复制 编辑 原始数据 按行查看 历史
package codegen
import (
"encoding/json"
"path/filepath"
"text/template"
"gopkg.in/yaml.v2"
"goa.design/goa/codegen"
"goa.design/goa/expr"
"goa.design/goa/http/codegen/openapi"
)
// OpenAPIFiles returns the files for the OpenAPIFile spec of the given HTTP API.
func OpenAPIFiles(root *expr.RootExpr) ([]*codegen.File, error) {
// Only create a OpenAPI specification if there are HTTP services.
if len(root.API.HTTP.Services) == 0 {
return nil, nil
}
jsonPath := filepath.Join(codegen.Gendir, "http", "openapi.json")
yamlPath := filepath.Join(codegen.Gendir, "http", "openapi.yaml")
var (
jsonSection *codegen.SectionTemplate
yamlSection *codegen.SectionTemplate
)
{
spec, err := openapi.NewV2(root, root.API.Servers[0].Hosts[0])
if err != nil {
return nil, err
}
jsonSection = &codegen.SectionTemplate{
Name: "openapi",
FuncMap: template.FuncMap{"toJSON": toJSON},
Source: "{{ toJSON .}}",
Data: spec,
}
yamlSection = &codegen.SectionTemplate{
Name: "openapi",
FuncMap: template.FuncMap{"toYAML": toYAML},
Source: "{{ toYAML .}}",
Data: spec,
}
}
return []*codegen.File{
{
Path: jsonPath,
SectionTemplates: []*codegen.SectionTemplate{jsonSection},
},
{
Path: yamlPath,
SectionTemplates: []*codegen.SectionTemplate{yamlSection},
},
}, nil
}
func toJSON(d interface{}) string {
b, err := json.Marshal(d)
if err != nil {
panic("openapi: " + err.Error()) // bug
}
return string(b)
}
func toYAML(d interface{}) string {
b, err := yaml.Marshal(d)
if err != nil {
panic("openapi: " + err.Error()) // bug
}
return string(b)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/goa.git
git@gitee.com:mirrors/goa.git
mirrors
goa
goa
v2.1.2

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385