From 14118807baf0d34132eeb8cb0b3abd1657dfa0dd Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Wed, 29 May 2019 13:48:23 -0400 Subject: [PATCH] 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 --- .../resources/go-gin-server/routers.mustache | 13 +++-- .../petstore/go-gin-api-server/go/routers.go | 51 +++++++++---------- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go-gin-server/routers.mustache b/modules/openapi-generator/src/main/resources/go-gin-server/routers.mustache index babf63dd262..8eb63de4ea0 100644 --- a/modules/openapi-generator/src/main/resources/go-gin-server/routers.mustache +++ b/modules/openapi-generator/src/main/resources/go-gin-server/routers.mustache @@ -3,7 +3,6 @@ package {{packageName}} import ( "net/http" - "strings" "github.com/gin-gonic/gin" ) @@ -28,13 +27,13 @@ func NewRouter() *gin.Engine { router := gin.Default() for _, route := range routes { switch route.Method { - case "GET": + case http.MethodGet: router.GET(route.Pattern, route.HandlerFunc) - case "POST": + case http.MethodPost: router.POST(route.Pattern, route.HandlerFunc) - case "PUT": + case http.MethodPut: router.PUT(route.Pattern, route.HandlerFunc) - case "DELETE": + case http.MethodDelete: router.DELETE(route.Pattern, route.HandlerFunc) } } @@ -50,14 +49,14 @@ func Index(c *gin.Context) { var routes = Routes{ { "Index", - "GET", + http.MethodGet, "{{{basePathWithoutHost}}}/", Index, },{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}} { "{{operationId}}", - strings.ToUpper("{{httpMethod}}"), + http.Method{{httpMethod}}, "{{{basePathWithoutHost}}}{{{path}}}", {{operationId}}, },{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}} diff --git a/samples/server/petstore/go-gin-api-server/go/routers.go b/samples/server/petstore/go-gin-api-server/go/routers.go index a36d0b09731..0a1432a6269 100644 --- a/samples/server/petstore/go-gin-api-server/go/routers.go +++ b/samples/server/petstore/go-gin-api-server/go/routers.go @@ -11,7 +11,6 @@ package petstoreserver import ( "net/http" - "strings" "github.com/gin-gonic/gin" ) @@ -36,13 +35,13 @@ func NewRouter() *gin.Engine { router := gin.Default() for _, route := range routes { switch route.Method { - case "GET": + case http.MethodGet: router.GET(route.Pattern, route.HandlerFunc) - case "POST": + case http.MethodPost: router.POST(route.Pattern, route.HandlerFunc) - case "PUT": + case http.MethodPut: router.PUT(route.Pattern, route.HandlerFunc) - case "DELETE": + case http.MethodDelete: router.DELETE(route.Pattern, route.HandlerFunc) } } @@ -58,147 +57,147 @@ func Index(c *gin.Context) { var routes = Routes{ { "Index", - "GET", + http.MethodGet, "/v2/", Index, }, { "AddPet", - strings.ToUpper("Post"), + http.MethodPost, "/v2/pet", AddPet, }, { "DeletePet", - strings.ToUpper("Delete"), + http.MethodDelete, "/v2/pet/:petId", DeletePet, }, { "FindPetsByStatus", - strings.ToUpper("Get"), + http.MethodGet, "/v2/pet/findByStatus", FindPetsByStatus, }, { "FindPetsByTags", - strings.ToUpper("Get"), + http.MethodGet, "/v2/pet/findByTags", FindPetsByTags, }, { "GetPetById", - strings.ToUpper("Get"), + http.MethodGet, "/v2/pet/:petId", GetPetById, }, { "UpdatePet", - strings.ToUpper("Put"), + http.MethodPut, "/v2/pet", UpdatePet, }, { "UpdatePetWithForm", - strings.ToUpper("Post"), + http.MethodPost, "/v2/pet/:petId", UpdatePetWithForm, }, { "UploadFile", - strings.ToUpper("Post"), + http.MethodPost, "/v2/pet/:petId/uploadImage", UploadFile, }, { "DeleteOrder", - strings.ToUpper("Delete"), + http.MethodDelete, "/v2/store/order/:orderId", DeleteOrder, }, { "GetInventory", - strings.ToUpper("Get"), + http.MethodGet, "/v2/store/inventory", GetInventory, }, { "GetOrderById", - strings.ToUpper("Get"), + http.MethodGet, "/v2/store/order/:orderId", GetOrderById, }, { "PlaceOrder", - strings.ToUpper("Post"), + http.MethodPost, "/v2/store/order", PlaceOrder, }, { "CreateUser", - strings.ToUpper("Post"), + http.MethodPost, "/v2/user", CreateUser, }, { "CreateUsersWithArrayInput", - strings.ToUpper("Post"), + http.MethodPost, "/v2/user/createWithArray", CreateUsersWithArrayInput, }, { "CreateUsersWithListInput", - strings.ToUpper("Post"), + http.MethodPost, "/v2/user/createWithList", CreateUsersWithListInput, }, { "DeleteUser", - strings.ToUpper("Delete"), + http.MethodDelete, "/v2/user/:username", DeleteUser, }, { "GetUserByName", - strings.ToUpper("Get"), + http.MethodGet, "/v2/user/:username", GetUserByName, }, { "LoginUser", - strings.ToUpper("Get"), + http.MethodGet, "/v2/user/login", LoginUser, }, { "LogoutUser", - strings.ToUpper("Get"), + http.MethodGet, "/v2/user/logout", LogoutUser, }, { "UpdateUser", - strings.ToUpper("Put"), + http.MethodPut, "/v2/user/:username", UpdateUser, },