Skip to content

Commit 1411880

Browse files
kkirschewing328
authored andcommitted
Use golang's provided method names (gin) (#2983)
* Use golang's provided method names (gin) This commit modifies the gin template for the router to leverage the http constants for method types as defined by RFC 7231 section 4.3. These are documented on: https://golang.org/pkg/net/http/#pkg-constants This removes the need for the `strings` dependency and does not require any new dependencies, as `net/http` is already imported. * Remove strings dependency which is no longer used * Update samples
1 parent e3bc228 commit 1411880

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

modules/openapi-generator/src/main/resources/go-gin-server/routers.mustache

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package {{packageName}}
33

44
import (
55
"net/http"
6-
"strings"
76

87
"github.com/gin-gonic/gin"
98
)
@@ -28,13 +27,13 @@ func NewRouter() *gin.Engine {
2827
router := gin.Default()
2928
for _, route := range routes {
3029
switch route.Method {
31-
case "GET":
30+
case http.MethodGet:
3231
router.GET(route.Pattern, route.HandlerFunc)
33-
case "POST":
32+
case http.MethodPost:
3433
router.POST(route.Pattern, route.HandlerFunc)
35-
case "PUT":
34+
case http.MethodPut:
3635
router.PUT(route.Pattern, route.HandlerFunc)
37-
case "DELETE":
36+
case http.MethodDelete:
3837
router.DELETE(route.Pattern, route.HandlerFunc)
3938
}
4039
}
@@ -50,14 +49,14 @@ func Index(c *gin.Context) {
5049
var routes = Routes{
5150
{
5251
"Index",
53-
"GET",
52+
http.MethodGet,
5453
"{{{basePathWithoutHost}}}/",
5554
Index,
5655
},{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}
5756

5857
{
5958
"{{operationId}}",
60-
strings.ToUpper("{{httpMethod}}"),
59+
http.Method{{httpMethod}},
6160
"{{{basePathWithoutHost}}}{{{path}}}",
6261
{{operationId}},
6362
},{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}

samples/server/petstore/go-gin-api-server/go/routers.go

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ package petstoreserver
1111

1212
import (
1313
"net/http"
14-
"strings"
1514

1615
"github.com/gin-gonic/gin"
1716
)
@@ -36,13 +35,13 @@ func NewRouter() *gin.Engine {
3635
router := gin.Default()
3736
for _, route := range routes {
3837
switch route.Method {
39-
case "GET":
38+
case http.MethodGet:
4039
router.GET(route.Pattern, route.HandlerFunc)
41-
case "POST":
40+
case http.MethodPost:
4241
router.POST(route.Pattern, route.HandlerFunc)
43-
case "PUT":
42+
case http.MethodPut:
4443
router.PUT(route.Pattern, route.HandlerFunc)
45-
case "DELETE":
44+
case http.MethodDelete:
4645
router.DELETE(route.Pattern, route.HandlerFunc)
4746
}
4847
}
@@ -58,147 +57,147 @@ func Index(c *gin.Context) {
5857
var routes = Routes{
5958
{
6059
"Index",
61-
"GET",
60+
http.MethodGet,
6261
"/v2/",
6362
Index,
6463
},
6564

6665
{
6766
"AddPet",
68-
strings.ToUpper("Post"),
67+
http.MethodPost,
6968
"/v2/pet",
7069
AddPet,
7170
},
7271

7372
{
7473
"DeletePet",
75-
strings.ToUpper("Delete"),
74+
http.MethodDelete,
7675
"/v2/pet/:petId",
7776
DeletePet,
7877
},
7978

8079
{
8180
"FindPetsByStatus",
82-
strings.ToUpper("Get"),
81+
http.MethodGet,
8382
"/v2/pet/findByStatus",
8483
FindPetsByStatus,
8584
},
8685

8786
{
8887
"FindPetsByTags",
89-
strings.ToUpper("Get"),
88+
http.MethodGet,
9089
"/v2/pet/findByTags",
9190
FindPetsByTags,
9291
},
9392

9493
{
9594
"GetPetById",
96-
strings.ToUpper("Get"),
95+
http.MethodGet,
9796
"/v2/pet/:petId",
9897
GetPetById,
9998
},
10099

101100
{
102101
"UpdatePet",
103-
strings.ToUpper("Put"),
102+
http.MethodPut,
104103
"/v2/pet",
105104
UpdatePet,
106105
},
107106

108107
{
109108
"UpdatePetWithForm",
110-
strings.ToUpper("Post"),
109+
http.MethodPost,
111110
"/v2/pet/:petId",
112111
UpdatePetWithForm,
113112
},
114113

115114
{
116115
"UploadFile",
117-
strings.ToUpper("Post"),
116+
http.MethodPost,
118117
"/v2/pet/:petId/uploadImage",
119118
UploadFile,
120119
},
121120

122121
{
123122
"DeleteOrder",
124-
strings.ToUpper("Delete"),
123+
http.MethodDelete,
125124
"/v2/store/order/:orderId",
126125
DeleteOrder,
127126
},
128127

129128
{
130129
"GetInventory",
131-
strings.ToUpper("Get"),
130+
http.MethodGet,
132131
"/v2/store/inventory",
133132
GetInventory,
134133
},
135134

136135
{
137136
"GetOrderById",
138-
strings.ToUpper("Get"),
137+
http.MethodGet,
139138
"/v2/store/order/:orderId",
140139
GetOrderById,
141140
},
142141

143142
{
144143
"PlaceOrder",
145-
strings.ToUpper("Post"),
144+
http.MethodPost,
146145
"/v2/store/order",
147146
PlaceOrder,
148147
},
149148

150149
{
151150
"CreateUser",
152-
strings.ToUpper("Post"),
151+
http.MethodPost,
153152
"/v2/user",
154153
CreateUser,
155154
},
156155

157156
{
158157
"CreateUsersWithArrayInput",
159-
strings.ToUpper("Post"),
158+
http.MethodPost,
160159
"/v2/user/createWithArray",
161160
CreateUsersWithArrayInput,
162161
},
163162

164163
{
165164
"CreateUsersWithListInput",
166-
strings.ToUpper("Post"),
165+
http.MethodPost,
167166
"/v2/user/createWithList",
168167
CreateUsersWithListInput,
169168
},
170169

171170
{
172171
"DeleteUser",
173-
strings.ToUpper("Delete"),
172+
http.MethodDelete,
174173
"/v2/user/:username",
175174
DeleteUser,
176175
},
177176

178177
{
179178
"GetUserByName",
180-
strings.ToUpper("Get"),
179+
http.MethodGet,
181180
"/v2/user/:username",
182181
GetUserByName,
183182
},
184183

185184
{
186185
"LoginUser",
187-
strings.ToUpper("Get"),
186+
http.MethodGet,
188187
"/v2/user/login",
189188
LoginUser,
190189
},
191190

192191
{
193192
"LogoutUser",
194-
strings.ToUpper("Get"),
193+
http.MethodGet,
195194
"/v2/user/logout",
196195
LogoutUser,
197196
},
198197

199198
{
200199
"UpdateUser",
201-
strings.ToUpper("Put"),
200+
http.MethodPut,
202201
"/v2/user/:username",
203202
UpdateUser,
204203
},

0 commit comments

Comments
 (0)