@@ -16,6 +16,9 @@ import (
16
16
// API("API name", func() {
17
17
// Title("title") // API title used in documentation
18
18
// Description("description") // API description used in documentation
19
+ // VersionParam("version") // Path param that captures targeted version, can appear 0 or more times
20
+ // VersionHeader("X-Api-Version") // Request header that captures targeted version, can appear 0 or more times
21
+ // VersionQuery("version") // Querystring value that captures targeted version, can appear 0 or more times
19
22
// TermsOfService("terms")
20
23
// Contact(func() { // API Contact information
21
24
// Name("contact name")
@@ -32,7 +35,7 @@ import (
32
35
// })
33
36
// Host("goa.design") // API hostname
34
37
// Scheme("http")
35
- // BasePath("/base/:param") // Common base path to all API actions
38
+ // BasePath("/base/:version/: param") // Common base path to all API actions
36
39
// BaseParams(func() { // Common parameters to all API actions
37
40
// Param("param")
38
41
// })
@@ -78,7 +81,22 @@ func API(name string, dsl func()) *design.APIDefinition {
78
81
79
82
// Version is the top level design language function which defines the API global property values
80
83
// for a given version. The DSL used to define the property values is identical to the one used by
81
- // the API function.
84
+ // the API function. Here is an example that shows a *subset* of the Version
85
+ // DSL (see the API function for all the other possible functions).
86
+ //
87
+ // Version("2.0", func() {
88
+ // Title("API v2") // API version 2.0 title used in documentation
89
+ // Description("This is v2") // API version description used in documentation
90
+ // Docs(func() {
91
+ // Description("v2 docs")
92
+ // URL("v2 doc URL")
93
+ // })
94
+ // BasePath("/v2") // Common base path to all actions exposed by this API version
95
+ // VersionHeader("X-Api-Version") // Usually only useful if BasePath is same as API
96
+ // VersionQuery("version") // Generated code considers header first if specified then querystring
97
+ // VersionQuery("v") // Multiple version headers or querystrings may be specified
98
+ // VersionQuery("version", "v") // Equivalent to the two lines above
99
+ // })
82
100
func Version (ver string , dsl func ()) * design.APIVersionDefinition {
83
101
verdef := & design.APIVersionDefinition {Version : ver , DSLFunc : dsl }
84
102
if _ , ok := design .Design .APIVersions [ver ]; ok {
@@ -95,6 +113,66 @@ func Version(ver string, dsl func()) *design.APIVersionDefinition {
95
113
return verdef
96
114
}
97
115
116
+ // VersionParam defines the name of the request path parameter that contains the targeted API version.
117
+ // Multiple names may be specified in which case the value of the first to correspond to the name
118
+ // of a param is used.
119
+ func VersionParam (names ... string ) {
120
+ if api , ok := apiDefinition (true ); ok {
121
+ for _ , n := range names {
122
+ found := false
123
+ for _ , n2 := range api .VersionParams {
124
+ if n == n2 {
125
+ found = true
126
+ break
127
+ }
128
+ }
129
+ if ! found {
130
+ api .VersionParams = append (api .VersionParams , n )
131
+ }
132
+ }
133
+ }
134
+ }
135
+
136
+ // VersionHeader defines the name of the HTTP request header that contains the targeted API version.
137
+ // Multiple names may be specified in which case the value of the first to correspond to the name
138
+ // of a header is used.
139
+ func VersionHeader (names ... string ) {
140
+ if api , ok := apiDefinition (true ); ok {
141
+ for _ , n := range names {
142
+ found := false
143
+ for _ , n2 := range api .VersionHeaders {
144
+ if n == n2 {
145
+ found = true
146
+ break
147
+ }
148
+ }
149
+ if ! found {
150
+ api .VersionHeaders = append (api .VersionHeaders , n )
151
+ }
152
+ }
153
+ }
154
+ }
155
+
156
+ // VersionQuery defines the name of the querystring that contains the targeted API version.
157
+ // Multiple names may be specified in which case the value of the first to correspond to the name
158
+ // of a querystring is used.
159
+ func VersionQuery (names ... string ) {
160
+ if api , ok := apiDefinition (true ); ok {
161
+ for _ , n := range names {
162
+ found := false
163
+ for _ , n2 := range api .VersionQueries {
164
+ if n == n2 {
165
+ found = true
166
+ break
167
+ }
168
+ }
169
+ if ! found {
170
+ api .VersionQueries = append (api .VersionQueries , n )
171
+ }
172
+ }
173
+ }
174
+ }
175
+
98
176
// Description sets the definition description.
99
177
// Description can be called inside API, Resource, Action or MediaType.
100
178
func Description (d string ) {
0 commit comments