forked from yvasiyarov/swagger
/
generator.go
217 lines (181 loc) · 6 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
216
217
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"go/ast"
"log"
"os"
"path"
"regexp"
"strings"
"github.com/yvasiyarov/swagger/markup"
"github.com/yvasiyarov/swagger/parser"
)
const (
AVAILABLE_FORMATS = "go|swagger|asciidoc|markdown|confluence"
)
var apiPackage = flag.String("apiPackage", "", "The package that implements the API controllers, relative to $GOPATH/src")
var mainApiFile = flag.String("mainApiFile", "", "The file that contains the general API annotations, relative to $GOPATH/src")
var outputFormat = flag.String("format", "go", "Output format type for the generated files: "+AVAILABLE_FORMATS)
var outputSpec = flag.String("output", "", "Output (path) for the generated file(s)")
var controllerClass = flag.String("controllerClass", "", "Speed up parsing by specifying which receiver objects have the controller methods")
var generatedFileTemplate = `
package main
//This file is generated automatically. Do not try to edit it manually.
var resourceListingJson = {{resourceListing}}
var apiDescriptionsJson = {{apiDescriptions}}
`
// It must return true if funcDeclaration is controller. We will try to parse only comments before controllers
func IsController(funcDeclaration *ast.FuncDecl) bool {
if len(*controllerClass) == 0 {
// Search every method
return true
}
if funcDeclaration.Recv != nil && len(funcDeclaration.Recv.List) > 0 {
if starExpression, ok := funcDeclaration.Recv.List[0].Type.(*ast.StarExpr); ok {
receiverName := fmt.Sprint(starExpression.X)
matched, err := regexp.MatchString(string(*controllerClass), receiverName)
if err != nil {
log.Fatalf("The -controllerClass argument is not a valid regular expression: %v\n", err)
}
return matched
}
}
return false
}
func generateSwaggerDocs(parser *parser.Parser) error {
fd, err := os.Create(path.Join(*outputSpec, "docs.go"))
if err != nil {
return fmt.Errorf("Can not create document file: %v\n", err)
}
defer fd.Close()
var apiDescriptions bytes.Buffer
for apiKey, apiDescription := range parser.TopLevelApis {
apiDescriptions.WriteString("\"" + apiKey + "\":")
apiDescriptions.WriteString("`")
json, err := json.MarshalIndent(apiDescription, "", " ")
if err != nil {
return fmt.Errorf("Can not serialise []ApiDescription to JSON: %v\n", err)
}
apiDescriptions.Write(json)
apiDescriptions.WriteString("`,")
}
doc := strings.Replace(generatedFileTemplate, "{{resourceListing}}", "`"+string(parser.GetResourceListingJson())+"`", -1)
doc = strings.Replace(doc, "{{apiDescriptions}}", "map[string]string{"+apiDescriptions.String()+"}", -1)
fd.WriteString(doc)
return nil
}
func generateSwaggerUiFiles(parser *parser.Parser) error {
fd, err := os.Create(path.Join(*outputSpec, "index.json"))
if err != nil {
return fmt.Errorf("Can not create the master index.json file: %v\n", err)
}
defer fd.Close()
fd.WriteString(string(parser.GetResourceListingJson()))
for apiKey, apiDescription := range parser.TopLevelApis {
err = os.MkdirAll(path.Join(*outputSpec, apiKey), 0777)
if err != nil {
return err
}
fd, err = os.Create(path.Join(*outputSpec, apiKey, "index.json"))
if err != nil {
return fmt.Errorf("Can not create the %s/index.json file: %v\n", apiKey, err)
}
defer fd.Close()
json, err := json.MarshalIndent(apiDescription, "", " ")
if err != nil {
return fmt.Errorf("Can not serialise []ApiDescription to JSON: %v\n", err)
}
fd.Write(json)
log.Printf("Wrote %v/index.json", apiKey)
}
return nil
}
func InitParser() *parser.Parser {
parser := parser.NewParser()
parser.BasePath = "{{.}}"
parser.IsController = IsController
parser.TypesImplementingMarshalInterface["NullString"] = "string"
parser.TypesImplementingMarshalInterface["NullInt64"] = "int"
parser.TypesImplementingMarshalInterface["NullFloat64"] = "float"
parser.TypesImplementingMarshalInterface["NullBool"] = "bool"
return parser
}
type GeneratorParams struct {
ApiPackage, MainApiFile, OutputFormat, OutputSpec, ControllerClass string
}
func Generate(params GeneratorParams) error {
parser := InitParser()
gopath := os.Getenv("GOPATH")
if gopath == "" {
return errors.New("Please, set $GOPATH environment variable\n")
}
log.Println("Start parsing")
//Support gopaths with multiple directories
dirs := strings.Split(gopath, ":")
found := false
for _, d := range dirs {
apifile := path.Join(d, "src", params.MainApiFile)
if _, err := os.Stat(apifile); err == nil {
parser.ParseGeneralApiInfo(apifile)
found = true
}
}
if found == false {
apifile := path.Join(gopath, "src", params.MainApiFile)
return fmt.Errorf("Could not find apifile %s to parse\n", apifile)
}
parser.ParseApi(params.ApiPackage)
log.Println("Finish parsing")
var err error
confirmMsg := ""
format := strings.ToLower(params.OutputFormat)
switch format {
case "go":
err = generateSwaggerDocs(parser)
confirmMsg = "Doc file generated"
case "asciidoc":
err = markup.GenerateMarkup(parser, new(markup.MarkupAsciiDoc), ¶ms.OutputSpec, ".adoc")
confirmMsg = "AsciiDoc file generated"
case "markdown":
err = markup.GenerateMarkup(parser, new(markup.MarkupMarkDown), ¶ms.OutputSpec, ".md")
confirmMsg = "MarkDown file generated"
case "confluence":
err = markup.GenerateMarkup(parser, new(markup.MarkupConfluence), ¶ms.OutputSpec, ".confluence")
confirmMsg = "Confluence file generated"
case "swagger":
err = generateSwaggerUiFiles(parser)
confirmMsg = "Swagger UI files generated"
default:
err = fmt.Errorf("Invalid -format specified. Must be one of %v.", AVAILABLE_FORMATS)
}
if err != nil {
return err
}
log.Println(confirmMsg)
return nil
}
func main() {
flag.Parse()
if *mainApiFile == "" {
*mainApiFile = *apiPackage + "/main.go"
}
if *apiPackage == "" {
flag.PrintDefaults()
return
}
params := GeneratorParams{
ApiPackage: *apiPackage,
MainApiFile: *mainApiFile,
OutputFormat: *outputFormat,
OutputSpec: *outputSpec,
ControllerClass: *controllerClass,
}
err := Generate(params)
if err != nil {
log.Fatal(err.Error())
}
}