-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgenerate.go
130 lines (106 loc) · 3.68 KB
/
generate.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
package swagno
import (
"encoding/json"
"log"
"strings"
"github.com/go-swagno/swagno/components/definition"
"github.com/go-swagno/swagno/components/endpoint"
"github.com/go-swagno/swagno/components/http/response"
"github.com/go-swagno/swagno/components/mime"
"github.com/go-swagno/swagno/components/parameter"
)
func appendResponses(sourceResponses map[string]endpoint.JsonResponse, additionalResponses []response.Response) map[string]endpoint.JsonResponse {
responseGenerator := response.NewResponseGenerator()
for _, resp := range additionalResponses {
var responseSchema *parameter.JsonResponseSchema
switch respType := resp.(type) {
case response.CustomResponse:
responseSchema = responseGenerator.Generate(respType.Model)
case response.Response:
responseSchema = responseGenerator.Generate(respType)
}
sourceResponses[resp.ReturnCode()] = endpoint.JsonResponse{
Description: resp.Description(),
Schema: responseSchema,
}
}
return sourceResponses
}
func (s *Swagger) generateSwaggerJson() {
if len(s.endpoints) == 0 {
log.Println("No endpoints found")
return
}
// generate definition object of swagger json: https://swagger.io/specification/v2/#definitions-object
s.generateSwaggerDefinition()
// convert all user EndPoint models to 'path' fields of swagger json
// https://swagger.io/specification/v2/#paths-object
for _, e := range s.endpoints {
path := e.Path()
if s.Paths[path] == nil {
s.Paths[path] = make(map[string]endpoint.JsonEndPoint)
}
method := strings.ToLower(string(e.Method()))
for _, param := range e.Params() {
if param.Location() == parameter.Form {
endpoint.WithConsume([]mime.MIME{mime.MULTIFORM})(e)
break
}
}
parameters := make([]parameter.JsonParameter, 0)
for _, param := range e.Params() {
pj := param.AsJson()
if pj.In != parameter.Query.String() {
pj.Type = ""
}
parameters = append(parameters, param.AsJson())
}
if bjp := e.BodyJsonParameter(); bjp != nil {
parameters = append(parameters, *bjp)
}
// Creates the schema defintion for all successful return and error objects, and then links them in the responses section
responses := map[string]endpoint.JsonResponse{}
responses = appendResponses(responses, e.SuccessfulReturns())
responses = appendResponses(responses, e.Errors())
// add each endpoint to paths field of swagger
je := e.AsJson()
je.OperationId = method + "-" + path
je.Parameters = parameters
je.Responses = responses
s.Paths[path][method] = je
}
}
// ToJSON converts the Swagger object into its JSON representation formatted as bytes.
// It returns a slice of bytes containing the Swagger documentation in JSON format.
func (s *Swagger) ToJson() (jsonDocs []byte, err error) {
s.generateSwaggerJson()
return json.MarshalIndent(s, "", " ")
}
// MustToJson same thing as ToJson except for it doesn't return an error.
func (s Swagger) MustToJson() (jsonDocs []byte) {
s.generateSwaggerJson()
json, err := json.MarshalIndent(s, "", " ")
if err != nil {
log.Printf("Error while generating swagger json: %s", err)
}
return json
}
// generate "definitions" keys from endpoints: https://swagger.io/specification/v2/#definitions-object
func (s *Swagger) generateSwaggerDefinition() {
for _, endpoint := range s.endpoints {
if endpoint.Body != nil {
s.createDefinition(endpoint.Body)
}
s.createDefinitions(endpoint.SuccessfulReturns())
s.createDefinitions(endpoint.Errors())
}
}
func (s *Swagger) createDefinitions(r []response.Response) {
for _, obj := range r {
s.createDefinition(obj)
}
}
func (s *Swagger) createDefinition(t interface{}) {
generator := definition.NewDefinitionGenerator((*s).Definitions)
generator.CreateDefinition(t)
}