-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.go
215 lines (193 loc) · 4.61 KB
/
generator.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package multiswagger
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
"github.com/goadesign/goa/design"
"github.com/goadesign/goa/goagen/codegen"
"github.com/goadesign/goa/goagen/gen_swagger"
"github.com/goadesign/goa/goagen/utils"
)
// Generator is the swagger code generator.
type Generator struct {
API *design.APIDefinition // The API definition
OutDir string // Path to output directory
genfiles []string // Generated files
}
// Generate is the generator entry point called by the meta generator.
func Generate() (files []string, err error) {
var outDir, ver string
set := flag.NewFlagSet("swagger", flag.PanicOnError)
set.StringVar(&outDir, "out", "", "")
set.StringVar(&ver, "version", "", "")
set.String("design", "", "")
set.Parse(os.Args[1:])
if err := codegen.CheckVersion(ver); err != nil {
return nil, err
}
g := &Generator{OutDir: outDir, API: design.Design}
return g.Generate()
}
// Generate produces the skeleton main.
func (g *Generator) Generate() (_ []string, err error) {
go utils.Catch(nil, func() { g.Cleanup() })
defer func() {
if err != nil {
g.Cleanup()
}
}()
swagger, err := genswagger.New(g.API)
if err != nil {
return nil, err
}
swaggerDir := filepath.Join(g.OutDir, "swagger")
os.RemoveAll(swaggerDir)
if err = os.MkdirAll(swaggerDir, 0755); err != nil {
return nil, err
}
g.genfiles = append(g.genfiles, swaggerDir)
walk(swagger, appendKeys)
for key = range keys {
s, err := genswagger.New(g.API)
if err != nil {
return nil, err
}
extracted := walk(s, extract)
suffix := "." + key
// JSON
rawJSON, err := json.Marshal(extracted)
if err != nil {
return nil, err
}
swaggerFile := filepath.Join(swaggerDir, fmt.Sprintf("swagger%s.json", suffix))
if err := ioutil.WriteFile(swaggerFile, rawJSON, 0644); err != nil {
return nil, err
}
g.genfiles = append(g.genfiles, swaggerFile)
// YAML
var yamlSource interface{}
if err = json.Unmarshal(rawJSON, &yamlSource); err != nil {
return nil, err
}
rawYAML, err := yaml.Marshal(yamlSource)
if err != nil {
return nil, err
}
swaggerFile = filepath.Join(swaggerDir, fmt.Sprintf("swagger%s.yaml", suffix))
if err := ioutil.WriteFile(swaggerFile, rawYAML, 0644); err != nil {
return nil, err
}
g.genfiles = append(g.genfiles, swaggerFile)
}
return g.genfiles, nil
}
// Cleanup removes all the files generated by this generator during the last invokation of Generate.
func (g *Generator) Cleanup() {
for _, f := range g.genfiles {
os.Remove(f)
}
g.genfiles = nil
}
var (
keys = make(map[string]bool)
key string
)
func appendKeys(data *string) {
var unmarshaled interface{}
if err := json.Unmarshal([]byte(*data), &unmarshaled); err != nil {
return
}
asserted := unmarshaled.(map[string]interface{})
for k := range asserted {
keys[k] = true
}
}
func extract(data *string) {
if *data == "" {
return
}
var unmarshaled interface{}
if err := json.Unmarshal([]byte(*data), &unmarshaled); err != nil {
return
}
if len(keys) == 0 {
return
}
asserted := unmarshaled.(map[string]interface{})
value, ok := asserted[key]
if ok != true {
return
}
casted, ok := value.(string)
if ok != true {
return
}
*data = casted
}
func walk(s *genswagger.Swagger, function func(*string)) *genswagger.Swagger {
if s.Info != nil {
function(&s.Info.Description)
}
if s.Paths != nil {
for _, v := range s.Paths {
path, ok := v.(*genswagger.Path)
if ok != true {
continue
}
if path.Get != nil {
function(&path.Get.Description)
}
if path.Put != nil {
function(&path.Put.Description)
}
if path.Post != nil {
function(&path.Post.Description)
}
if path.Delete != nil {
function(&path.Delete.Description)
}
if path.Options != nil {
function(&path.Options.Description)
}
if path.Head != nil {
function(&path.Head.Description)
}
if path.Patch != nil {
function(&path.Patch.Description)
}
if path.Parameters != nil {
for _, parameter := range path.Parameters {
function(¶meter.Description)
}
}
}
}
if s.Parameters != nil {
for _, parameter := range s.Parameters {
function(¶meter.Description)
}
}
if s.Responses != nil {
for _, response := range s.Responses {
function(&response.Description)
}
}
if s.SecurityDefinitions != nil {
for _, security := range s.SecurityDefinitions {
function(&security.Description)
}
}
if s.Tags != nil {
for _, tag := range s.Tags {
function(&tag.Description)
}
}
if s.ExternalDocs != nil {
function(&s.ExternalDocs.Description)
}
return s
}