Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[go-server] Change Routes type from []Route to map[string]Route #15084

Merged
merged 3 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ func New{{classname}}Controller(s {{classname}}Servicer, opts ...{{classname}}Op

// Routes returns all the api routes for the {{classname}}Controller
func (c *{{classname}}Controller) Routes() Routes {
return Routes{ {{#operations}}{{#operation}}
{
"{{operationId}}",
return Routes{
{{#operations}}
{{#operation}}
"{{operationId}}": Route{
strings.ToUpper("{{httpMethod}}"),
"{{{basePathWithoutHost}}}{{{path}}}",
c.{{operationId}},
},{{/operation}}{{/operations}}
},
{{/operation}}
{{/operations}}
}
}{{#operations}}{{#operation}}

Expand Down Expand Up @@ -234,5 +237,4 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code,{{#addResponseHeaders}} result.Headers,{{/addResponseHeaders}} w)

}{{/operation}}{{/operations}}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{>partial_header}}
package {{packageName}}

// ImplResponse response defines an error code with the associated body
// ImplResponse defines an implementation response with error code and the associated body
type ImplResponse struct {
Code int
{{#addResponseHeaders}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ import (

// A Route defines the parameters for an api endpoint
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}

// Routes are a collection of defined api endpoints
type Routes []Route
// Routes is a map of defined api endpoints
type Routes map[string]Route

// Router defines the required methods for retrieving api routes
type Router interface {
Expand All @@ -60,20 +59,20 @@ func NewRouter(routers ...Router) {{#routers}}{{#mux}}*mux.Router{{/mux}}{{#chi}
{{/chi}}
{{/routers}}
for _, api := range routers {
for _, route := range api.Routes() {
for name, route := range api.Routes() {
var handler http.Handler
handler = route.HandlerFunc
{{#routers}}
{{#mux}}
handler = Logger(handler, route.Name)
handler = Logger(handler, name)
{{#featureCORS}}
handler = handlers.CORS()(handler)
{{/featureCORS}}

router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Name(name).
Handler(handler)
{{/mux}}
{{#chi}}
Expand All @@ -90,11 +89,9 @@ func NewRouter(routers ...Router) {{#routers}}{{#mux}}*mux.Router{{/mux}}{{#chi}
func EncodeJSONResponse(i interface{}, status *int,{{#addResponseHeaders}} headers map[string][]string,{{/addResponseHeaders}} w http.ResponseWriter) error {
{{#addResponseHeaders}}
wHeader := w.Header()
if headers != nil {
for key, values := range headers {
for _, value := range values {
wHeader.Add(key, value)
}
for key, values := range headers {
for _, value := range values {
wHeader.Add(key, value)
}
}
wHeader.Set("Content-Type", "application/json; charset=UTF-8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func (s *{{classname}}Service) {{nickname}}(ctx context.Context{{#allParams}}, {

{{#responses}}
{{#dataType}}
//TODO: Uncomment the next line to return response Response({{code}}, {{dataType}}{}) or use other options such as http.Ok ...
//return Response({{code}}, {{dataType}}{}), nil
// TODO: Uncomment the next line to return response Response({{code}}, {{dataType}}{}) or use other options such as http.Ok ...
// return Response({{code}}, {{dataType}}{}), nil

{{/dataType}}
{{^dataType}}
//TODO: Uncomment the next line to return response Response({{code}}, {{dataType}}{}) or use other options such as http.Ok ...
//return Response({{code}}, nil),nil
// TODO: Uncomment the next line to return response Response({{code}}, {{dataType}}{}) or use other options such as http.Ok ...
// return Response({{code}}, nil),nil

{{/dataType}}
{{/responses}}
Expand Down
34 changes: 9 additions & 25 deletions samples/server/petstore/go-api-server/go/api_pet.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,51 +49,43 @@ func NewPetApiController(s PetApiServicer, opts ...PetApiOption) Router {

// Routes returns all the api routes for the PetApiController
func (c *PetApiController) Routes() Routes {
return Routes{
{
"AddPet",
return Routes{
"AddPet": Route{
strings.ToUpper("Post"),
"/v2/pet",
c.AddPet,
},
{
"DeletePet",
"DeletePet": Route{
strings.ToUpper("Delete"),
"/v2/pet/{petId}",
c.DeletePet,
},
{
"FindPetsByStatus",
"FindPetsByStatus": Route{
strings.ToUpper("Get"),
"/v2/pet/findByStatus",
c.FindPetsByStatus,
},
{
"FindPetsByTags",
"FindPetsByTags": Route{
strings.ToUpper("Get"),
"/v2/pet/findByTags",
c.FindPetsByTags,
},
{
"GetPetById",
"GetPetById": Route{
strings.ToUpper("Get"),
"/v2/pet/{petId}",
c.GetPetById,
},
{
"UpdatePet",
"UpdatePet": Route{
strings.ToUpper("Put"),
"/v2/pet",
c.UpdatePet,
},
{
"UpdatePetWithForm",
"UpdatePetWithForm": Route{
strings.ToUpper("Post"),
"/v2/pet/{petId}",
c.UpdatePetWithForm,
},
{
"UploadFile",
"UploadFile": Route{
strings.ToUpper("Post"),
"/v2/pet/{petId}/uploadImage",
c.UploadFile,
Expand Down Expand Up @@ -122,7 +114,6 @@ func (c *PetApiController) AddPet(w http.ResponseWriter, r *http.Request) {
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)

}

// DeletePet - Deletes a pet
Expand All @@ -143,7 +134,6 @@ func (c *PetApiController) DeletePet(w http.ResponseWriter, r *http.Request) {
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)

}

// FindPetsByStatus - Finds Pets by status
Expand All @@ -158,7 +148,6 @@ func (c *PetApiController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)

}

// FindPetsByTags - Finds Pets by tags
Expand All @@ -174,7 +163,6 @@ func (c *PetApiController) FindPetsByTags(w http.ResponseWriter, r *http.Request
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)

}

// GetPetById - Find pet by ID
Expand All @@ -194,7 +182,6 @@ func (c *PetApiController) GetPetById(w http.ResponseWriter, r *http.Request) {
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)

}

// UpdatePet - Update an existing pet
Expand All @@ -218,7 +205,6 @@ func (c *PetApiController) UpdatePet(w http.ResponseWriter, r *http.Request) {
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)

}

// UpdatePetWithForm - Updates a pet in the store with form data
Expand All @@ -244,7 +230,6 @@ func (c *PetApiController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)

}

// UploadFile - uploads an image
Expand Down Expand Up @@ -275,5 +260,4 @@ func (c *PetApiController) UploadFile(w http.ResponseWriter, r *http.Request) {
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)

}
64 changes: 32 additions & 32 deletions samples/server/petstore/go-api-server/go/api_pet_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func (s *PetApiService) AddPet(ctx context.Context, pet Pet) (ImplResponse, erro
// TODO - update AddPet with the required logic for this service method.
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(200, Pet{}) or use other options such as http.Ok ...
//return Response(200, Pet{}), nil
// TODO: Uncomment the next line to return response Response(200, Pet{}) or use other options such as http.Ok ...
// return Response(200, Pet{}), nil

//TODO: Uncomment the next line to return response Response(405, {}) or use other options such as http.Ok ...
//return Response(405, nil),nil
// TODO: Uncomment the next line to return response Response(405, {}) or use other options such as http.Ok ...
// return Response(405, nil),nil

return Response(http.StatusNotImplemented, nil), errors.New("AddPet method not implemented")
}
Expand All @@ -46,8 +46,8 @@ func (s *PetApiService) DeletePet(ctx context.Context, petId int64, apiKey strin
// TODO - update DeletePet with the required logic for this service method.
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ...
//return Response(400, nil),nil
// TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ...
// return Response(400, nil),nil

return Response(http.StatusNotImplemented, nil), errors.New("DeletePet method not implemented")
}
Expand All @@ -57,11 +57,11 @@ func (s *PetApiService) FindPetsByStatus(ctx context.Context, status []string) (
// TODO - update FindPetsByStatus with the required logic for this service method.
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(200, []Pet{}) or use other options such as http.Ok ...
//return Response(200, []Pet{}), nil
// TODO: Uncomment the next line to return response Response(200, []Pet{}) or use other options such as http.Ok ...
// return Response(200, []Pet{}), nil

//TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ...
//return Response(400, nil),nil
// TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ...
// return Response(400, nil),nil

return Response(http.StatusNotImplemented, nil), errors.New("FindPetsByStatus method not implemented")
}
Expand All @@ -72,11 +72,11 @@ func (s *PetApiService) FindPetsByTags(ctx context.Context, tags []string) (Impl
// TODO - update FindPetsByTags with the required logic for this service method.
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(200, []Pet{}) or use other options such as http.Ok ...
//return Response(200, []Pet{}), nil
// TODO: Uncomment the next line to return response Response(200, []Pet{}) or use other options such as http.Ok ...
// return Response(200, []Pet{}), nil

//TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ...
//return Response(400, nil),nil
// TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ...
// return Response(400, nil),nil

return Response(http.StatusNotImplemented, nil), errors.New("FindPetsByTags method not implemented")
}
Expand All @@ -86,14 +86,14 @@ func (s *PetApiService) GetPetById(ctx context.Context, petId int64) (ImplRespon
// TODO - update GetPetById with the required logic for this service method.
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(200, Pet{}) or use other options such as http.Ok ...
//return Response(200, Pet{}), nil
// TODO: Uncomment the next line to return response Response(200, Pet{}) or use other options such as http.Ok ...
// return Response(200, Pet{}), nil

//TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ...
//return Response(400, nil),nil
// TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ...
// return Response(400, nil),nil

//TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ...
//return Response(404, nil),nil
// TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ...
// return Response(404, nil),nil

return Response(http.StatusNotImplemented, nil), errors.New("GetPetById method not implemented")
}
Expand All @@ -103,17 +103,17 @@ func (s *PetApiService) UpdatePet(ctx context.Context, pet Pet) (ImplResponse, e
// TODO - update UpdatePet with the required logic for this service method.
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(200, Pet{}) or use other options such as http.Ok ...
//return Response(200, Pet{}), nil
// TODO: Uncomment the next line to return response Response(200, Pet{}) or use other options such as http.Ok ...
// return Response(200, Pet{}), nil

//TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ...
//return Response(400, nil),nil
// TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ...
// return Response(400, nil),nil

//TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ...
//return Response(404, nil),nil
// TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ...
// return Response(404, nil),nil

//TODO: Uncomment the next line to return response Response(405, {}) or use other options such as http.Ok ...
//return Response(405, nil),nil
// TODO: Uncomment the next line to return response Response(405, {}) or use other options such as http.Ok ...
// return Response(405, nil),nil

return Response(http.StatusNotImplemented, nil), errors.New("UpdatePet method not implemented")
}
Expand All @@ -123,8 +123,8 @@ func (s *PetApiService) UpdatePetWithForm(ctx context.Context, petId int64, name
// TODO - update UpdatePetWithForm with the required logic for this service method.
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(405, {}) or use other options such as http.Ok ...
//return Response(405, nil),nil
// TODO: Uncomment the next line to return response Response(405, {}) or use other options such as http.Ok ...
// return Response(405, nil),nil

return Response(http.StatusNotImplemented, nil), errors.New("UpdatePetWithForm method not implemented")
}
Expand All @@ -134,8 +134,8 @@ func (s *PetApiService) UploadFile(ctx context.Context, petId int64, additionalM
// TODO - update UploadFile with the required logic for this service method.
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(200, ApiResponse{}) or use other options such as http.Ok ...
//return Response(200, ApiResponse{}), nil
// TODO: Uncomment the next line to return response Response(200, ApiResponse{}) or use other options such as http.Ok ...
// return Response(200, ApiResponse{}), nil

return Response(http.StatusNotImplemented, nil), errors.New("UploadFile method not implemented")
}
Loading