-
Notifications
You must be signed in to change notification settings - Fork 6
/
swagger.go
179 lines (164 loc) · 4.46 KB
/
swagger.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
package swagger
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"github.com/go-openapi/spec"
)
// Generator handles common code generation operations when generating a file in a Go package.
type Generator struct {
BasePath string
buf bytes.Buffer
}
// Printf writes a formatted string to the buffer.
func (g *Generator) Printf(format string, args ...interface{}) {
fmt.Fprintf(&g.buf, format, args...)
}
// Write bytes to the buffer.
func (g *Generator) Write(p []byte) (n int, err error) {
return g.buf.Write(p)
}
// WriteFile writes the buffer to a gofmt-ed file.
// The file will be located at {(Generator).BasePath}/{path}.
func (g *Generator) WriteFile(path string) error {
if len(path) == 0 || path[0] == '/' {
return fmt.Errorf("path must be relative")
}
fileBytes := g.buf.Bytes()
if strings.HasSuffix(path, ".go") {
formattedFileBytes, err := format.Source(fileBytes)
if err != nil {
// This will error if the code isn't valid so let's write it out so we can debug
f, createErr := os.Create("badcode.txt")
if createErr != nil {
return createErr
}
if _, writeErr := f.Write(g.buf.Bytes()); writeErr != nil {
return writeErr
}
return fmt.Errorf("INTERNAL ERROR: %s. The invalid code was written to badcode.txt", err)
}
fileBytes = formattedFileBytes
}
absPath := filepath.Join(g.BasePath, path)
dir := filepath.Dir(absPath)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
fileMode := os.FileMode(0644)
if strings.HasSuffix(path, ".sh") {
fileMode = os.FileMode(0755) // chmod +x
}
return ioutil.WriteFile(absPath, fileBytes, fileMode)
}
// ImportStatements takes a list of import strings and converts them to a formatted imports
func ImportStatements(imports []string) string {
if len(imports) == 0 {
return ""
}
remoteImports := []string{}
output := "import (\n"
for _, importStr := range imports {
if strings.Contains(importStr, ".") {
remoteImports = append(remoteImports, importStr)
} else {
if strings.Contains(importStr, `"`) {
output += fmt.Sprintf("\t%s\n", importStr)
} else {
output += fmt.Sprintf("\t\"%s\"\n", importStr)
}
}
}
if len(remoteImports) > 0 {
output += "\n"
}
for _, importStr := range remoteImports {
if strings.Contains(importStr, `"`) {
output += fmt.Sprintf("\t%s\n", importStr)
} else {
output += fmt.Sprintf("\t\"%s\"\n", importStr)
}
}
output += ")\n\n"
return output
}
// SortedPathItemKeys sorts the keys of a map[string]spec.PathItem.
func SortedPathItemKeys(m map[string]spec.PathItem) []string {
sortedKeys := []string{}
for k := range m {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
return sortedKeys
}
// SortedOperationsKeys sorts the keys of a map[string]*spec.Operation.
func SortedOperationsKeys(m map[string]*spec.Operation) []string {
sortedKeys := []string{}
for k := range m {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
return sortedKeys
}
// SortedStatusCodeKeys sorts the keys of a map[int]spec.Response.
func SortedStatusCodeKeys(m map[int]spec.Response) []int {
sortedKeys := []int{}
for k := range m {
sortedKeys = append(sortedKeys, k)
}
sort.Ints(sortedKeys)
return sortedKeys
}
// SortedResponses sorts the keys of a map[string[spec].Response
func SortedResponses(m map[string]spec.Response) []string {
sortedKeys := []string{}
for k := range m {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
return sortedKeys
}
// SortedSchemaProperties sorts the properties of a schema
func SortedSchemaProperties(schema spec.Schema) []string {
sortedKeys := []string{}
for k := range schema.Properties {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
return sortedKeys
}
// Capitalize the first character of a string.
func Capitalize(input string) string {
return strings.ToUpper(input[0:1]) + input[1:]
}
// PathItemOperations converts a spec.PathItem to a map from HTTP operation (e.g., "GET") to spec.Operation.
func PathItemOperations(item spec.PathItem) map[string]*spec.Operation {
ops := make(map[string]*spec.Operation)
if item.Get != nil {
ops["GET"] = item.Get
}
if item.Put != nil {
ops["PUT"] = item.Put
}
if item.Post != nil {
ops["POST"] = item.Post
}
if item.Delete != nil {
ops["DELETE"] = item.Delete
}
if item.Options != nil {
ops["OPTIONS"] = item.Options
}
if item.Head != nil {
ops["HEAD"] = item.Head
}
if item.Patch != nil {
ops["PATCH"] = item.Patch
}
return ops
}