-
-
Notifications
You must be signed in to change notification settings - Fork 559
/
files.go
56 lines (49 loc) Β· 1.22 KB
/
files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package openapiv3
import (
"encoding/json"
"path/filepath"
"text/template"
"gopkg.in/yaml.v3"
"goa.design/goa/v3/codegen"
"goa.design/goa/v3/expr"
)
// Files returns the OpenAPI v3 specification files in JSON and YAML formats.
func Files(root *expr.RootExpr) ([]*codegen.File, error) {
spec := New(root)
jsonSection := &codegen.SectionTemplate{
Name: "openapi_v3",
FuncMap: template.FuncMap{"toJSON": toJSON},
Source: "{{ toJSON .}}",
Data: spec,
}
yamlSection := &codegen.SectionTemplate{
Name: "openapi_v3",
FuncMap: template.FuncMap{"toYAML": toYAML},
Source: "{{ toYAML .}}",
Data: spec,
}
return []*codegen.File{
{
Path: filepath.Join(codegen.Gendir, "http", "openapi3.json"),
SectionTemplates: []*codegen.SectionTemplate{jsonSection},
},
{
Path: filepath.Join(codegen.Gendir, "http", "openapi3.yaml"),
SectionTemplates: []*codegen.SectionTemplate{yamlSection},
},
}, nil
}
func toJSON(d any) string {
b, err := json.Marshal(d)
if err != nil {
panic("openapi: " + err.Error()) // bug
}
return string(b)
}
func toYAML(d any) string {
b, err := yaml.Marshal(d)
if err != nil {
panic("openapi: " + err.Error()) // bug
}
return string(b)
}