-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathapi.go
279 lines (268 loc) Β· 6.36 KB
/
api.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package dsl
import (
"goa.design/goa/eval"
"goa.design/goa/expr"
)
// API defines a network service API. It provides the API name, description and other global
// properties. There may only be one API declaration in a given design package.
//
// API is a top level DSL. API takes two arguments: the name of the API and the
// defining DSL.
//
// The API properties are leveraged by the OpenAPI specification. The server
// expressions are also used by the server and the client tool code generators.
//
// Example:
//
// var _ = API("adder", func() {
// Title("title") // Title used in documentation
// Description("description") // Description used in documentation
// Version("2.0") // Version of API
// TermsOfService("terms") // Terms of use
// Contact(func() { // Contact info
// Name("contact name")
// Email("contact email")
// URL("contact URL")
// })
// License(func() { // License
// Name("license name")
// URL("license URL")
// })
// Docs(func() { // Documentation links
// Description("doc description")
// URL("doc URL")
// })
// Server("addersvr", func() {
// Host("development", func() {
// URI("http://localhost:80")
// URI("grpc://localhost:8080")
// })
// })
// }
//
func API(name string, fn func()) *expr.APIExpr {
if name == "" {
eval.ReportError("API first argument cannot be empty")
return nil
}
if _, ok := eval.Current().(eval.TopExpr); !ok {
eval.IncompatibleDSL()
return nil
}
expr.Root.API = expr.NewAPIExpr(name, fn)
return expr.Root.API
}
// Title sets the API title. It is used by the generated OpenAPI specification.
//
// Title must appear in a API expression.
//
// Title accepts a single string argument.
//
// Example:
//
// var _ = API("divider", func() {
// Title("divider API")
// })
//
func Title(val string) {
if s, ok := eval.Current().(*expr.APIExpr); ok {
s.Title = val
return
}
eval.IncompatibleDSL()
}
// Version specifies the API version. One design describes one version.
//
// Version must appear in a API expression.
//
// Version accepts a single string argument.
//
// Example:
//
// var _ = API("divider", func() {
// Version("1.0")
// })
//
func Version(ver string) {
if s, ok := eval.Current().(*expr.APIExpr); ok {
s.Version = ver
return
}
eval.IncompatibleDSL()
}
// Contact sets the API contact information.
//
// Contact must appear in a API expression.
//
// Contact takes a single argument which is the defining DSL.
//
// Example:
//
// var _ = API("divider", func() {
// Contact(func() {
// Name("support")
// Email("[email protected]")
// URL("https://goa.design")
// })
// })
//
func Contact(fn func()) {
contact := new(expr.ContactExpr)
if !eval.Execute(fn, contact) {
return
}
if a, ok := eval.Current().(*expr.APIExpr); ok {
a.Contact = contact
return
}
eval.IncompatibleDSL()
}
// License sets the API license information.
//
// License must appear in a API expression.
//
// License takes a single argument which is the defining DSL.
//
// Example:
//
// var _ = API("divider", func() {
// License(func() {
// Name("MIT")
// URL("https://github.com/goadesign/goa/blob/master/LICENSE")
// })
// })
//
func License(fn func()) {
license := new(expr.LicenseExpr)
if !eval.Execute(fn, license) {
return
}
if a, ok := eval.Current().(*expr.APIExpr); ok {
a.License = license
return
}
eval.IncompatibleDSL()
}
// Docs provides external documentation URLs. It is used by the generated
// OpenAPI specification.
//
// Docs must appear in an API, Service, Method or Attribute expr.
//
// Docs takes a single argument which is the defining DSL.
//
// Example:
//
// var _ = API("cellar", func() {
// Docs(func() {
// Description("Additional documentation")
// URL("https://goa.design")
// })
// })
//
func Docs(fn func()) {
docs := new(expr.DocsExpr)
if !eval.Execute(fn, docs) {
return
}
switch e := eval.Current().(type) {
case *expr.APIExpr:
e.Docs = docs
case *expr.ServiceExpr:
e.Docs = docs
case *expr.MethodExpr:
e.Docs = docs
case *expr.AttributeExpr:
e.Docs = docs
case *expr.HTTPFileServerExpr:
e.Docs = docs
default:
eval.IncompatibleDSL()
}
}
// TermsOfService describes the API terms of services or links to them.
//
// TermsOfService must appear in a API expression.
//
// TermsOfService takes a single argument which is the TOS text or URL.
//
// Example:
//
// var _ = API("github", func() {
// TermsOfService("https://help.github.com/articles/github-terms-of-API/"
// })
//
func TermsOfService(terms string) {
if s, ok := eval.Current().(*expr.APIExpr); ok {
s.TermsOfService = terms
return
}
eval.IncompatibleDSL()
}
// Name sets the contact or license name.
//
// Name must appear in a Contact or License expression.
//
// Name takes a single argument which is the contact or license name.
//
// Example:
//
// var _ = API("divider", func() {
// License(func() {
// Name("MIT")
// URL("https://github.com/goadesign/goa/blob/master/LICENSE")
// })
// })
//
func Name(name string) {
switch def := eval.Current().(type) {
case *expr.ContactExpr:
def.Name = name
case *expr.LicenseExpr:
def.Name = name
default:
eval.IncompatibleDSL()
}
}
// Email sets the contact email.
//
// Email must appear in a Contact expression.
//
// Email takes a single argument which is the email address.
//
// Example:
//
// var _ = API("divider", func() {
// Contact(func() {
// Email("[email protected]")
// })
// })
//
func Email(email string) {
if c, ok := eval.Current().(*expr.ContactExpr); ok {
c.Email = email
}
}
// URL sets the contact, license or external documentation URL.
//
// URL must appear in Contact, License or Docs.
//
// URL accepts a single argument which is the URL.
//
// Example:
//
// Docs(func() {
// URL("https://goa.design")
// })
//
func URL(url string) {
switch def := eval.Current().(type) {
case *expr.ContactExpr:
def.URL = url
case *expr.LicenseExpr:
def.URL = url
case *expr.DocsExpr:
def.URL = url
default:
eval.IncompatibleDSL()
}
}