[go-server] Fix: error handling and linting (#18550)

* Update error.go and fix lint

* Regen

* Fix incorrect change

* Fix handler issue

* Regenerate
This commit is contained in:
Ween Jiann 2024-05-08 00:06:45 +08:00 committed by GitHub
parent 06499605e1
commit 8f6a2860bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
50 changed files with 219 additions and 194 deletions

View File

@ -47,7 +47,7 @@ func With{{classname}}ErrorHandler(h ErrorHandler) {{classname}}Option {
} }
// New{{classname}}Controller creates a default api controller // New{{classname}}Controller creates a default api controller
func New{{classname}}Controller(s {{classname}}Servicer, opts ...{{classname}}Option) Router { func New{{classname}}Controller(s {{classname}}Servicer, opts ...{{classname}}Option) *{{classname}}Controller {
controller := &{{classname}}Controller{ controller := &{{classname}}Controller{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -631,5 +631,5 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code,{{#addResponseHeaders}} result.Headers,{{/addResponseHeaders}} w) _ = EncodeJSONResponse(result.Body, &result.Code,{{#addResponseHeaders}} result.Headers,{{/addResponseHeaders}} w)
}{{/operation}}{{/operations}} }{{/operation}}{{/operations}}

View File

@ -25,9 +25,9 @@ func (e *ParsingError) Unwrap() error {
func (e *ParsingError) Error() string { func (e *ParsingError) Error() string {
if e.Param == "" { if e.Param == "" {
return e.Err.Error() return e.Err.Error()
} else {
return e.Param + ": " + e.Err.Error()
} }
return e.Param + ": " + e.Err.Error()
} }
// RequiredError indicates that an error has occurred when parsing request parameters // RequiredError indicates that an error has occurred when parsing request parameters
@ -45,15 +45,21 @@ type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error, result
// DefaultErrorHandler defines the default logic on how to handle errors from the controller. Any errors from parsing // DefaultErrorHandler defines the default logic on how to handle errors from the controller. Any errors from parsing
// request params will return a StatusBadRequest. Otherwise, the error code originating from the servicer will be used. // request params will return a StatusBadRequest. Otherwise, the error code originating from the servicer will be used.
func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error, result *ImplResponse) { func DefaultErrorHandler(w http.ResponseWriter, _ *http.Request, err error, result *ImplResponse) {
if _, ok := err.(*ParsingError); ok { var parsingErr *ParsingError
if ok := errors.As(err, &parsingErr); ok {
// Handle parsing errors // Handle parsing errors
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest),{{#addResponseHeaders}} map[string][]string{},{{/addResponseHeaders}} w) _ = EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest),{{#addResponseHeaders}} map[string][]string{},{{/addResponseHeaders}} w)
} else if _, ok := err.(*RequiredError); ok { return
}
var requiredErr *RequiredError
if ok := errors.As(err, &requiredErr); ok {
// Handle missing required errors // Handle missing required errors
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusUnprocessableEntity),{{#addResponseHeaders}} map[string][]string{},{{/addResponseHeaders}} w) _ = EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusUnprocessableEntity),{{#addResponseHeaders}} map[string][]string{},{{/addResponseHeaders}} w)
} else { return
}
// Handle all other errors // Handle all other errors
EncodeJSONResponse(err.Error(), &result.Code,{{#addResponseHeaders}} result.Headers,{{/addResponseHeaders}} w) _ = EncodeJSONResponse(err.Error(), &result.Code,{{#addResponseHeaders}} result.Headers,{{/addResponseHeaders}} w)
}
} }

View File

@ -56,7 +56,7 @@ func AssertRecurseValueRequired[T any](value reflect.Value, callback func(T) err
// If it is a slice we continue recursion // If it is a slice we continue recursion
case reflect.Slice: case reflect.Slice:
for i := 0; i < value.Len(); i += 1 { for i := 0; i < value.Len(); i++ {
if err := AssertRecurseValueRequired(value.Index(i), callback); err != nil { if err := AssertRecurseValueRequired(value.Index(i), callback); err != nil {
return err return err
} }

View File

@ -49,9 +49,9 @@ func New{{{classname}}}FromValue(v {{{format}}}{{^format}}{{dataType}}{{/format}
ev := {{{classname}}}(v) ev := {{{classname}}}(v)
if ev.IsValid() { if ev.IsValid() {
return ev, nil return ev, nil
} else {
return "", fmt.Errorf("invalid value '%v' for {{{classname}}}: valid values are %v", v, Allowed{{{classname}}}EnumValues)
} }
return "", fmt.Errorf("invalid value '%v' for {{{classname}}}: valid values are %v", v, Allowed{{{classname}}}EnumValues)
} }
{{/isEnum}}{{^isEnum}}{{#description}} {{/isEnum}}{{^isEnum}}{{#description}}

View File

@ -64,8 +64,7 @@ func NewRouter(routers ...Router) {{#routers}}{{#mux}}*mux.Router{{/mux}}{{#chi}
{{/routers}} {{/routers}}
for _, api := range routers { for _, api := range routers {
for {{#routers}}{{#mux}}name{{/mux}}{{#chi}}_{{/chi}}{{/routers}}, route := range api.Routes() { for {{#routers}}{{#mux}}name{{/mux}}{{#chi}}_{{/chi}}{{/routers}}, route := range api.Routes() {
var handler http.Handler var handler http.Handler = route.HandlerFunc
handler = route.HandlerFunc
{{#routers}} {{#routers}}
{{#mux}} {{#mux}}
handler = Logger(handler, name) handler = Logger(handler, name)
@ -373,7 +372,7 @@ func parseNumericArrayParameter[T Number](param, delim string, required bool, fn
} }
// parseQuery parses query paramaters and returns an error if any malformed value pairs are encountered. // parseQuery parses query parameters and returns an error if any malformed value pairs are encountered.
func parseQuery(rawQuery string) (url.Values, error) { func parseQuery(rawQuery string) (url.Values, error) {
return url.ParseQuery(rawQuery) return url.ParseQuery(rawQuery)
} }

View File

@ -15,7 +15,7 @@ type {{classname}}Service struct {
} }
// New{{classname}}Service creates a default api service // New{{classname}}Service creates a default api service
func New{{classname}}Service() {{classname}}Servicer { func New{{classname}}Service() *{{classname}}Service {
return &{{classname}}Service{} return &{{classname}}Service{}
}{{#operations}}{{#operation}} }{{#operations}}{{#operation}}

View File

@ -36,7 +36,7 @@ func WithPetAPIErrorHandler(h ErrorHandler) PetAPIOption {
} }
// NewPetAPIController creates a default api controller // NewPetAPIController creates a default api controller
func NewPetAPIController(s PetAPIServicer, opts ...PetAPIOption) Router { func NewPetAPIController(s PetAPIServicer, opts ...PetAPIOption) *PetAPIController {
controller := &PetAPIController{ controller := &PetAPIController{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -119,7 +119,7 @@ func (c *PetAPIController) AddPet(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// DeletePet - Deletes a pet // DeletePet - Deletes a pet
@ -140,7 +140,7 @@ func (c *PetAPIController) DeletePet(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// FindPetsByStatus - Finds Pets by status // FindPetsByStatus - Finds Pets by status
@ -161,7 +161,7 @@ func (c *PetAPIController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// FindPetsByTags - Finds Pets by tags // FindPetsByTags - Finds Pets by tags
@ -183,7 +183,7 @@ func (c *PetAPIController) FindPetsByTags(w http.ResponseWriter, r *http.Request
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetPetById - Find pet by ID // GetPetById - Find pet by ID
@ -203,7 +203,7 @@ func (c *PetAPIController) GetPetById(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UpdatePet - Update an existing pet // UpdatePet - Update an existing pet
@ -230,7 +230,7 @@ func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UpdatePetWithForm - Updates a pet in the store with form data // UpdatePetWithForm - Updates a pet in the store with form data
@ -260,7 +260,7 @@ func (c *PetAPIController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UploadFile - uploads an image // UploadFile - uploads an image
@ -299,5 +299,5 @@ func (c *PetAPIController) UploadFile(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }

View File

@ -24,7 +24,7 @@ type PetAPIService struct {
} }
// NewPetAPIService creates a default api service // NewPetAPIService creates a default api service
func NewPetAPIService() PetAPIServicer { func NewPetAPIService() *PetAPIService {
return &PetAPIService{} return &PetAPIService{}
} }

View File

@ -35,7 +35,7 @@ func WithStoreAPIErrorHandler(h ErrorHandler) StoreAPIOption {
} }
// NewStoreAPIController creates a default api controller // NewStoreAPIController creates a default api controller
func NewStoreAPIController(s StoreAPIServicer, opts ...StoreAPIOption) Router { func NewStoreAPIController(s StoreAPIServicer, opts ...StoreAPIOption) *StoreAPIController {
controller := &StoreAPIController{ controller := &StoreAPIController{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -88,7 +88,7 @@ func (c *StoreAPIController) DeleteOrder(w http.ResponseWriter, r *http.Request)
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetInventory - Returns pet inventories by status // GetInventory - Returns pet inventories by status
@ -100,7 +100,7 @@ func (c *StoreAPIController) GetInventory(w http.ResponseWriter, r *http.Request
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetOrderById - Find purchase order by ID // GetOrderById - Find purchase order by ID
@ -122,7 +122,7 @@ func (c *StoreAPIController) GetOrderById(w http.ResponseWriter, r *http.Request
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// PlaceOrder - Place an order for a pet // PlaceOrder - Place an order for a pet
@ -149,5 +149,5 @@ func (c *StoreAPIController) PlaceOrder(w http.ResponseWriter, r *http.Request)
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }

View File

@ -23,7 +23,7 @@ type StoreAPIService struct {
} }
// NewStoreAPIService creates a default api service // NewStoreAPIService creates a default api service
func NewStoreAPIService() StoreAPIServicer { func NewStoreAPIService() *StoreAPIService {
return &StoreAPIService{} return &StoreAPIService{}
} }

View File

@ -35,7 +35,7 @@ func WithUserAPIErrorHandler(h ErrorHandler) UserAPIOption {
} }
// NewUserAPIController creates a default api controller // NewUserAPIController creates a default api controller
func NewUserAPIController(s UserAPIServicer, opts ...UserAPIOption) Router { func NewUserAPIController(s UserAPIServicer, opts ...UserAPIOption) *UserAPIController {
controller := &UserAPIController{ controller := &UserAPIController{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -118,7 +118,7 @@ func (c *UserAPIController) CreateUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// CreateUsersWithArrayInput - Creates list of users with given input array // CreateUsersWithArrayInput - Creates list of users with given input array
@ -143,7 +143,7 @@ func (c *UserAPIController) CreateUsersWithArrayInput(w http.ResponseWriter, r *
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// CreateUsersWithListInput - Creates list of users with given input array // CreateUsersWithListInput - Creates list of users with given input array
@ -168,7 +168,7 @@ func (c *UserAPIController) CreateUsersWithListInput(w http.ResponseWriter, r *h
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// DeleteUser - Delete user // DeleteUser - Delete user
@ -204,7 +204,7 @@ func (c *UserAPIController) DeleteUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetUserByName - Get user by user name // GetUserByName - Get user by user name
@ -221,7 +221,7 @@ func (c *UserAPIController) GetUserByName(w http.ResponseWriter, r *http.Request
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// LoginUser - Logs user into the system // LoginUser - Logs user into the system
@ -326,7 +326,7 @@ func (c *UserAPIController) LoginUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// LogoutUser - Logs out current logged in user session // LogoutUser - Logs out current logged in user session
@ -338,7 +338,7 @@ func (c *UserAPIController) LogoutUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UpdateUser - Updated user // UpdateUser - Updated user
@ -370,5 +370,5 @@ func (c *UserAPIController) UpdateUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }

View File

@ -23,7 +23,7 @@ type UserAPIService struct {
} }
// NewUserAPIService creates a default api service // NewUserAPIService creates a default api service
func NewUserAPIService() UserAPIServicer { func NewUserAPIService() *UserAPIService {
return &UserAPIService{} return &UserAPIService{}
} }

View File

@ -34,9 +34,9 @@ func (e *ParsingError) Unwrap() error {
func (e *ParsingError) Error() string { func (e *ParsingError) Error() string {
if e.Param == "" { if e.Param == "" {
return e.Err.Error() return e.Err.Error()
} else {
return e.Param + ": " + e.Err.Error()
} }
return e.Param + ": " + e.Err.Error()
} }
// RequiredError indicates that an error has occurred when parsing request parameters // RequiredError indicates that an error has occurred when parsing request parameters
@ -54,15 +54,21 @@ type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error, result
// DefaultErrorHandler defines the default logic on how to handle errors from the controller. Any errors from parsing // DefaultErrorHandler defines the default logic on how to handle errors from the controller. Any errors from parsing
// request params will return a StatusBadRequest. Otherwise, the error code originating from the servicer will be used. // request params will return a StatusBadRequest. Otherwise, the error code originating from the servicer will be used.
func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error, result *ImplResponse) { func DefaultErrorHandler(w http.ResponseWriter, _ *http.Request, err error, result *ImplResponse) {
if _, ok := err.(*ParsingError); ok { var parsingErr *ParsingError
if ok := errors.As(err, &parsingErr); ok {
// Handle parsing errors // Handle parsing errors
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest), map[string][]string{}, w) _ = EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest), map[string][]string{}, w)
} else if _, ok := err.(*RequiredError); ok { return
}
var requiredErr *RequiredError
if ok := errors.As(err, &requiredErr); ok {
// Handle missing required errors // Handle missing required errors
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusUnprocessableEntity), map[string][]string{}, w) _ = EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusUnprocessableEntity), map[string][]string{}, w)
} else { return
}
// Handle all other errors // Handle all other errors
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w) _ = EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
}
} }

View File

@ -61,7 +61,7 @@ func AssertRecurseValueRequired[T any](value reflect.Value, callback func(T) err
// If it is a slice we continue recursion // If it is a slice we continue recursion
case reflect.Slice: case reflect.Slice:
for i := 0; i < value.Len(); i += 1 { for i := 0; i < value.Len(); i++ {
if err := AssertRecurseValueRequired(value.Index(i), callback); err != nil { if err := AssertRecurseValueRequired(value.Index(i), callback); err != nil {
return err return err
} }

View File

@ -50,8 +50,7 @@ func NewRouter(routers ...Router) chi.Router {
router.Use(middleware.Logger) router.Use(middleware.Logger)
for _, api := range routers { for _, api := range routers {
for _, route := range api.Routes() { for _, route := range api.Routes() {
var handler http.Handler var handler http.Handler = route.HandlerFunc
handler = route.HandlerFunc
router.Method(route.Method, route.Pattern, handler) router.Method(route.Method, route.Pattern, handler)
} }
} }
@ -341,7 +340,7 @@ func parseNumericArrayParameter[T Number](param, delim string, required bool, fn
} }
// parseQuery parses query paramaters and returns an error if any malformed value pairs are encountered. // parseQuery parses query parameters and returns an error if any malformed value pairs are encountered.
func parseQuery(rawQuery string) (url.Values, error) { func parseQuery(rawQuery string) (url.Values, error) {
return url.ParseQuery(rawQuery) return url.ParseQuery(rawQuery)
} }

View File

@ -33,7 +33,7 @@ func WithBodyAPIErrorHandler(h ErrorHandler) BodyAPIOption {
} }
// NewBodyAPIController creates a default api controller // NewBodyAPIController creates a default api controller
func NewBodyAPIController(s BodyAPIServicer, opts ...BodyAPIOption) Router { func NewBodyAPIController(s BodyAPIServicer, opts ...BodyAPIOption) *BodyAPIController {
controller := &BodyAPIController{ controller := &BodyAPIController{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -81,5 +81,5 @@ func (c *BodyAPIController) Body(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }

View File

@ -23,7 +23,7 @@ type BodyAPIService struct {
} }
// NewBodyAPIService creates a default api service // NewBodyAPIService creates a default api service
func NewBodyAPIService() BodyAPIServicer { func NewBodyAPIService() *BodyAPIService {
return &BodyAPIService{} return &BodyAPIService{}
} }

View File

@ -35,7 +35,7 @@ func WithBothAPIErrorHandler(h ErrorHandler) BothAPIOption {
} }
// NewBothAPIController creates a default api controller // NewBothAPIController creates a default api controller
func NewBothAPIController(s BothAPIServicer, opts ...BothAPIOption) Router { func NewBothAPIController(s BothAPIServicer, opts ...BothAPIOption) *BothAPIController {
controller := &BothAPIController{ controller := &BothAPIController{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -89,5 +89,5 @@ func (c *BothAPIController) Both(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }

View File

@ -23,7 +23,7 @@ type BothAPIService struct {
} }
// NewBothAPIService creates a default api service // NewBothAPIService creates a default api service
func NewBothAPIService() BothAPIServicer { func NewBothAPIService() *BothAPIService {
return &BothAPIService{} return &BothAPIService{}
} }

View File

@ -32,7 +32,7 @@ func WithNoneAPIErrorHandler(h ErrorHandler) NoneAPIOption {
} }
// NewNoneAPIController creates a default api controller // NewNoneAPIController creates a default api controller
func NewNoneAPIController(s NoneAPIServicer, opts ...NoneAPIOption) Router { func NewNoneAPIController(s NoneAPIServicer, opts ...NoneAPIOption) *NoneAPIController {
controller := &NoneAPIController{ controller := &NoneAPIController{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -65,5 +65,5 @@ func (c *NoneAPIController) One(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }

View File

@ -23,7 +23,7 @@ type NoneAPIService struct {
} }
// NewNoneAPIService creates a default api service // NewNoneAPIService creates a default api service
func NewNoneAPIService() NoneAPIServicer { func NewNoneAPIService() *NoneAPIService {
return &NoneAPIService{} return &NoneAPIService{}
} }

View File

@ -34,7 +34,7 @@ func WithPathAPIErrorHandler(h ErrorHandler) PathAPIOption {
} }
// NewPathAPIController creates a default api controller // NewPathAPIController creates a default api controller
func NewPathAPIController(s PathAPIServicer, opts ...PathAPIOption) Router { func NewPathAPIController(s PathAPIServicer, opts ...PathAPIOption) *PathAPIController {
controller := &PathAPIController{ controller := &PathAPIController{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -73,5 +73,5 @@ func (c *PathAPIController) Path(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }

View File

@ -23,7 +23,7 @@ type PathAPIService struct {
} }
// NewPathAPIService creates a default api service // NewPathAPIService creates a default api service
func NewPathAPIService() PathAPIServicer { func NewPathAPIService() *PathAPIService {
return &PathAPIService{} return &PathAPIService{}
} }

View File

@ -34,9 +34,9 @@ func (e *ParsingError) Unwrap() error {
func (e *ParsingError) Error() string { func (e *ParsingError) Error() string {
if e.Param == "" { if e.Param == "" {
return e.Err.Error() return e.Err.Error()
} else {
return e.Param + ": " + e.Err.Error()
} }
return e.Param + ": " + e.Err.Error()
} }
// RequiredError indicates that an error has occurred when parsing request parameters // RequiredError indicates that an error has occurred when parsing request parameters
@ -54,15 +54,21 @@ type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error, result
// DefaultErrorHandler defines the default logic on how to handle errors from the controller. Any errors from parsing // DefaultErrorHandler defines the default logic on how to handle errors from the controller. Any errors from parsing
// request params will return a StatusBadRequest. Otherwise, the error code originating from the servicer will be used. // request params will return a StatusBadRequest. Otherwise, the error code originating from the servicer will be used.
func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error, result *ImplResponse) { func DefaultErrorHandler(w http.ResponseWriter, _ *http.Request, err error, result *ImplResponse) {
if _, ok := err.(*ParsingError); ok { var parsingErr *ParsingError
if ok := errors.As(err, &parsingErr); ok {
// Handle parsing errors // Handle parsing errors
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest), map[string][]string{}, w) _ = EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest), map[string][]string{}, w)
} else if _, ok := err.(*RequiredError); ok { return
}
var requiredErr *RequiredError
if ok := errors.As(err, &requiredErr); ok {
// Handle missing required errors // Handle missing required errors
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusUnprocessableEntity), map[string][]string{}, w) _ = EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusUnprocessableEntity), map[string][]string{}, w)
} else { return
}
// Handle all other errors // Handle all other errors
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w) _ = EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
}
} }

View File

@ -61,7 +61,7 @@ func AssertRecurseValueRequired[T any](value reflect.Value, callback func(T) err
// If it is a slice we continue recursion // If it is a slice we continue recursion
case reflect.Slice: case reflect.Slice:
for i := 0; i < value.Len(); i += 1 { for i := 0; i < value.Len(); i++ {
if err := AssertRecurseValueRequired(value.Index(i), callback); err != nil { if err := AssertRecurseValueRequired(value.Index(i), callback); err != nil {
return err return err
} }

View File

@ -48,8 +48,7 @@ func NewRouter(routers ...Router) *mux.Router {
router := mux.NewRouter().StrictSlash(true) router := mux.NewRouter().StrictSlash(true)
for _, api := range routers { for _, api := range routers {
for name, route := range api.Routes() { for name, route := range api.Routes() {
var handler http.Handler var handler http.Handler = route.HandlerFunc
handler = route.HandlerFunc
handler = Logger(handler, name) handler = Logger(handler, name)
router. router.
@ -345,7 +344,7 @@ func parseNumericArrayParameter[T Number](param, delim string, required bool, fn
} }
// parseQuery parses query paramaters and returns an error if any malformed value pairs are encountered. // parseQuery parses query parameters and returns an error if any malformed value pairs are encountered.
func parseQuery(rawQuery string) (url.Values, error) { func parseQuery(rawQuery string) (url.Values, error) {
return url.ParseQuery(rawQuery) return url.ParseQuery(rawQuery)
} }

View File

@ -37,7 +37,7 @@ func WithPetAPIErrorHandler(h ErrorHandler) PetAPIOption {
} }
// NewPetAPIController creates a default api controller // NewPetAPIController creates a default api controller
func NewPetAPIController(s PetAPIServicer, opts ...PetAPIOption) Router { func NewPetAPIController(s PetAPIServicer, opts ...PetAPIOption) *PetAPIController {
controller := &PetAPIController{ controller := &PetAPIController{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -150,7 +150,7 @@ func (c *PetAPIController) AddPet(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// DeletePet - Deletes a pet // DeletePet - Deletes a pet
@ -172,7 +172,7 @@ func (c *PetAPIController) DeletePet(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// FilterPetsByCategory - Finds Pets // FilterPetsByCategory - Finds Pets
@ -217,7 +217,7 @@ func (c *PetAPIController) FilterPetsByCategory(w http.ResponseWriter, r *http.R
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// FindPetsByStatus - Finds Pets by status // FindPetsByStatus - Finds Pets by status
@ -292,7 +292,7 @@ func (c *PetAPIController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// FindPetsByTags - Finds Pets by tags // FindPetsByTags - Finds Pets by tags
@ -345,7 +345,7 @@ func (c *PetAPIController) FindPetsByTags(w http.ResponseWriter, r *http.Request
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetPetById - Find pet by ID // GetPetById - Find pet by ID
@ -366,7 +366,7 @@ func (c *PetAPIController) GetPetById(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetPetImageById - Returns the image for the Pet that has been previously uploaded // GetPetImageById - Returns the image for the Pet that has been previously uploaded
@ -387,7 +387,7 @@ func (c *PetAPIController) GetPetImageById(w http.ResponseWriter, r *http.Reques
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetPetsByTime - Get the pets by time // GetPetsByTime - Get the pets by time
@ -405,7 +405,7 @@ func (c *PetAPIController) GetPetsByTime(w http.ResponseWriter, r *http.Request)
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetPetsUsingBooleanQueryParameters - Get the pets by only using boolean query parameters // GetPetsUsingBooleanQueryParameters - Get the pets by only using boolean query parameters
@ -468,7 +468,7 @@ func (c *PetAPIController) GetPetsUsingBooleanQueryParameters(w http.ResponseWri
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// SearchPet - Search Pets by filters // SearchPet - Search Pets by filters
@ -538,7 +538,7 @@ func (c *PetAPIController) SearchPet(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UpdatePet - Update an existing pet // UpdatePet - Update an existing pet
@ -565,7 +565,7 @@ func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UpdatePetWithForm - Updates a pet in the store with form data // UpdatePetWithForm - Updates a pet in the store with form data
@ -596,7 +596,7 @@ func (c *PetAPIController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UploadFile - uploads an image // UploadFile - uploads an image
@ -639,7 +639,7 @@ func (c *PetAPIController) UploadFile(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UploadFileArrayOfFiles - uploads images (array of files) // UploadFileArrayOfFiles - uploads images (array of files)
@ -679,5 +679,5 @@ func (c *PetAPIController) UploadFileArrayOfFiles(w http.ResponseWriter, r *http
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }

View File

@ -25,7 +25,7 @@ type PetAPIService struct {
} }
// NewPetAPIService creates a default api service // NewPetAPIService creates a default api service
func NewPetAPIService() PetAPIServicer { func NewPetAPIService() *PetAPIService {
return &PetAPIService{} return &PetAPIService{}
} }

View File

@ -35,7 +35,7 @@ func WithStoreAPIErrorHandler(h ErrorHandler) StoreAPIOption {
} }
// NewStoreAPIController creates a default api controller // NewStoreAPIController creates a default api controller
func NewStoreAPIController(s StoreAPIServicer, opts ...StoreAPIOption) Router { func NewStoreAPIController(s StoreAPIServicer, opts ...StoreAPIOption) *StoreAPIController {
controller := &StoreAPIController{ controller := &StoreAPIController{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -89,7 +89,7 @@ func (c *StoreAPIController) DeleteOrder(w http.ResponseWriter, r *http.Request)
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetInventory - Returns pet inventories by status // GetInventory - Returns pet inventories by status
@ -101,7 +101,7 @@ func (c *StoreAPIController) GetInventory(w http.ResponseWriter, r *http.Request
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetOrderById - Find purchase order by ID // GetOrderById - Find purchase order by ID
@ -124,7 +124,7 @@ func (c *StoreAPIController) GetOrderById(w http.ResponseWriter, r *http.Request
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// PlaceOrder - Place an order for a pet // PlaceOrder - Place an order for a pet
@ -151,5 +151,5 @@ func (c *StoreAPIController) PlaceOrder(w http.ResponseWriter, r *http.Request)
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }

View File

@ -23,7 +23,7 @@ type StoreAPIService struct {
} }
// NewStoreAPIService creates a default api service // NewStoreAPIService creates a default api service
func NewStoreAPIService() StoreAPIServicer { func NewStoreAPIService() *StoreAPIService {
return &StoreAPIService{} return &StoreAPIService{}
} }

View File

@ -35,7 +35,7 @@ func WithUserAPIErrorHandler(h ErrorHandler) UserAPIOption {
} }
// NewUserAPIController creates a default api controller // NewUserAPIController creates a default api controller
func NewUserAPIController(s UserAPIServicer, opts ...UserAPIOption) Router { func NewUserAPIController(s UserAPIServicer, opts ...UserAPIOption) *UserAPIController {
controller := &UserAPIController{ controller := &UserAPIController{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -118,7 +118,7 @@ func (c *UserAPIController) CreateUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// CreateUsersWithArrayInput - Creates list of users with given input array // CreateUsersWithArrayInput - Creates list of users with given input array
@ -143,7 +143,7 @@ func (c *UserAPIController) CreateUsersWithArrayInput(w http.ResponseWriter, r *
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// CreateUsersWithListInput - Creates list of users with given input array // CreateUsersWithListInput - Creates list of users with given input array
@ -168,7 +168,7 @@ func (c *UserAPIController) CreateUsersWithListInput(w http.ResponseWriter, r *h
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// DeleteUser - Delete user // DeleteUser - Delete user
@ -205,7 +205,7 @@ func (c *UserAPIController) DeleteUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetUserByName - Get user by user name // GetUserByName - Get user by user name
@ -223,7 +223,7 @@ func (c *UserAPIController) GetUserByName(w http.ResponseWriter, r *http.Request
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// LoginUser - Logs user into the system // LoginUser - Logs user into the system
@ -272,7 +272,7 @@ func (c *UserAPIController) LoginUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// LogoutUser - Logs out current logged in user session // LogoutUser - Logs out current logged in user session
@ -284,7 +284,7 @@ func (c *UserAPIController) LogoutUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UpdateUser - Updated user // UpdateUser - Updated user
@ -317,5 +317,5 @@ func (c *UserAPIController) UpdateUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }

View File

@ -23,7 +23,7 @@ type UserAPIService struct {
} }
// NewUserAPIService creates a default api service // NewUserAPIService creates a default api service
func NewUserAPIService() UserAPIServicer { func NewUserAPIService() *UserAPIService {
return &UserAPIService{} return &UserAPIService{}
} }

View File

@ -34,9 +34,9 @@ func (e *ParsingError) Unwrap() error {
func (e *ParsingError) Error() string { func (e *ParsingError) Error() string {
if e.Param == "" { if e.Param == "" {
return e.Err.Error() return e.Err.Error()
} else {
return e.Param + ": " + e.Err.Error()
} }
return e.Param + ": " + e.Err.Error()
} }
// RequiredError indicates that an error has occurred when parsing request parameters // RequiredError indicates that an error has occurred when parsing request parameters
@ -54,15 +54,21 @@ type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error, result
// DefaultErrorHandler defines the default logic on how to handle errors from the controller. Any errors from parsing // DefaultErrorHandler defines the default logic on how to handle errors from the controller. Any errors from parsing
// request params will return a StatusBadRequest. Otherwise, the error code originating from the servicer will be used. // request params will return a StatusBadRequest. Otherwise, the error code originating from the servicer will be used.
func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error, result *ImplResponse) { func DefaultErrorHandler(w http.ResponseWriter, _ *http.Request, err error, result *ImplResponse) {
if _, ok := err.(*ParsingError); ok { var parsingErr *ParsingError
if ok := errors.As(err, &parsingErr); ok {
// Handle parsing errors // Handle parsing errors
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest), map[string][]string{}, w) _ = EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest), map[string][]string{}, w)
} else if _, ok := err.(*RequiredError); ok { return
}
var requiredErr *RequiredError
if ok := errors.As(err, &requiredErr); ok {
// Handle missing required errors // Handle missing required errors
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusUnprocessableEntity), map[string][]string{}, w) _ = EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusUnprocessableEntity), map[string][]string{}, w)
} else { return
}
// Handle all other errors // Handle all other errors
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w) _ = EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
}
} }

View File

@ -61,7 +61,7 @@ func AssertRecurseValueRequired[T any](value reflect.Value, callback func(T) err
// If it is a slice we continue recursion // If it is a slice we continue recursion
case reflect.Slice: case reflect.Slice:
for i := 0; i < value.Len(); i += 1 { for i := 0; i < value.Len(); i++ {
if err := AssertRecurseValueRequired(value.Index(i), callback); err != nil { if err := AssertRecurseValueRequired(value.Index(i), callback); err != nil {
return err return err
} }

View File

@ -49,9 +49,9 @@ func NewColourFromValue(v string) (Colour, error) {
ev := Colour(v) ev := Colour(v)
if ev.IsValid() { if ev.IsValid() {
return ev, nil return ev, nil
} else {
return "", fmt.Errorf("invalid value '%v' for Colour: valid values are %v", v, AllowedColourEnumValues)
} }
return "", fmt.Errorf("invalid value '%v' for Colour: valid values are %v", v, AllowedColourEnumValues)
} }

View File

@ -49,9 +49,9 @@ func NewGenderFromValue(v string) (Gender, error) {
ev := Gender(v) ev := Gender(v)
if ev.IsValid() { if ev.IsValid() {
return ev, nil return ev, nil
} else {
return "", fmt.Errorf("invalid value '%v' for Gender: valid values are %v", v, AllowedGenderEnumValues)
} }
return "", fmt.Errorf("invalid value '%v' for Gender: valid values are %v", v, AllowedGenderEnumValues)
} }

View File

@ -58,9 +58,9 @@ func NewSpeciesFromValue(v string) (Species, error) {
ev := Species(v) ev := Species(v)
if ev.IsValid() { if ev.IsValid() {
return ev, nil return ev, nil
} else {
return "", fmt.Errorf("invalid value '%v' for Species: valid values are %v", v, AllowedSpeciesEnumValues)
} }
return "", fmt.Errorf("invalid value '%v' for Species: valid values are %v", v, AllowedSpeciesEnumValues)
} }

View File

@ -48,8 +48,7 @@ func NewRouter(routers ...Router) *mux.Router {
router := mux.NewRouter().StrictSlash(true) router := mux.NewRouter().StrictSlash(true)
for _, api := range routers { for _, api := range routers {
for name, route := range api.Routes() { for name, route := range api.Routes() {
var handler http.Handler var handler http.Handler = route.HandlerFunc
handler = route.HandlerFunc
handler = Logger(handler, name) handler = Logger(handler, name)
router. router.
@ -345,7 +344,7 @@ func parseNumericArrayParameter[T Number](param, delim string, required bool, fn
} }
// parseQuery parses query paramaters and returns an error if any malformed value pairs are encountered. // parseQuery parses query parameters and returns an error if any malformed value pairs are encountered.
func parseQuery(rawQuery string) (url.Values, error) { func parseQuery(rawQuery string) (url.Values, error) {
return url.ParseQuery(rawQuery) return url.ParseQuery(rawQuery)
} }

View File

@ -37,7 +37,7 @@ func WithPetAPIErrorHandler(h ErrorHandler) PetAPIOption {
} }
// NewPetAPIController creates a default api controller // NewPetAPIController creates a default api controller
func NewPetAPIController(s PetAPIServicer, opts ...PetAPIOption) Router { func NewPetAPIController(s PetAPIServicer, opts ...PetAPIOption) *PetAPIController {
controller := &PetAPIController{ controller := &PetAPIController{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -150,7 +150,7 @@ func (c *PetAPIController) AddPet(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// DeletePet - Deletes a pet // DeletePet - Deletes a pet
@ -171,7 +171,7 @@ func (c *PetAPIController) DeletePet(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// FilterPetsByCategory - Finds Pets // FilterPetsByCategory - Finds Pets
@ -215,7 +215,7 @@ func (c *PetAPIController) FilterPetsByCategory(w http.ResponseWriter, r *http.R
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// FindPetsByStatus - Finds Pets by status // FindPetsByStatus - Finds Pets by status
@ -289,7 +289,7 @@ func (c *PetAPIController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// FindPetsByTags - Finds Pets by tags // FindPetsByTags - Finds Pets by tags
@ -342,7 +342,7 @@ func (c *PetAPIController) FindPetsByTags(w http.ResponseWriter, r *http.Request
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetPetById - Find pet by ID // GetPetById - Find pet by ID
@ -362,7 +362,7 @@ func (c *PetAPIController) GetPetById(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetPetImageById - Returns the image for the Pet that has been previously uploaded // GetPetImageById - Returns the image for the Pet that has been previously uploaded
@ -382,7 +382,7 @@ func (c *PetAPIController) GetPetImageById(w http.ResponseWriter, r *http.Reques
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetPetsByTime - Get the pets by time // GetPetsByTime - Get the pets by time
@ -399,7 +399,7 @@ func (c *PetAPIController) GetPetsByTime(w http.ResponseWriter, r *http.Request)
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetPetsUsingBooleanQueryParameters - Get the pets by only using boolean query parameters // GetPetsUsingBooleanQueryParameters - Get the pets by only using boolean query parameters
@ -462,7 +462,7 @@ func (c *PetAPIController) GetPetsUsingBooleanQueryParameters(w http.ResponseWri
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// SearchPet - Search Pets by filters // SearchPet - Search Pets by filters
@ -532,7 +532,7 @@ func (c *PetAPIController) SearchPet(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UpdatePet - Update an existing pet // UpdatePet - Update an existing pet
@ -559,7 +559,7 @@ func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UpdatePetWithForm - Updates a pet in the store with form data // UpdatePetWithForm - Updates a pet in the store with form data
@ -589,7 +589,7 @@ func (c *PetAPIController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UploadFile - uploads an image // UploadFile - uploads an image
@ -631,7 +631,7 @@ func (c *PetAPIController) UploadFile(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UploadFileArrayOfFiles - uploads images (array of files) // UploadFileArrayOfFiles - uploads images (array of files)
@ -670,5 +670,5 @@ func (c *PetAPIController) UploadFileArrayOfFiles(w http.ResponseWriter, r *http
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }

View File

@ -25,7 +25,7 @@ type PetAPIService struct {
} }
// NewPetAPIService creates a default api service // NewPetAPIService creates a default api service
func NewPetAPIService() PetAPIServicer { func NewPetAPIService() *PetAPIService {
return &PetAPIService{} return &PetAPIService{}
} }

View File

@ -35,7 +35,7 @@ func WithStoreAPIErrorHandler(h ErrorHandler) StoreAPIOption {
} }
// NewStoreAPIController creates a default api controller // NewStoreAPIController creates a default api controller
func NewStoreAPIController(s StoreAPIServicer, opts ...StoreAPIOption) Router { func NewStoreAPIController(s StoreAPIServicer, opts ...StoreAPIOption) *StoreAPIController {
controller := &StoreAPIController{ controller := &StoreAPIController{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -88,7 +88,7 @@ func (c *StoreAPIController) DeleteOrder(w http.ResponseWriter, r *http.Request)
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetInventory - Returns pet inventories by status // GetInventory - Returns pet inventories by status
@ -100,7 +100,7 @@ func (c *StoreAPIController) GetInventory(w http.ResponseWriter, r *http.Request
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetOrderById - Find purchase order by ID // GetOrderById - Find purchase order by ID
@ -122,7 +122,7 @@ func (c *StoreAPIController) GetOrderById(w http.ResponseWriter, r *http.Request
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// PlaceOrder - Place an order for a pet // PlaceOrder - Place an order for a pet
@ -149,5 +149,5 @@ func (c *StoreAPIController) PlaceOrder(w http.ResponseWriter, r *http.Request)
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }

View File

@ -23,7 +23,7 @@ type StoreAPIService struct {
} }
// NewStoreAPIService creates a default api service // NewStoreAPIService creates a default api service
func NewStoreAPIService() StoreAPIServicer { func NewStoreAPIService() *StoreAPIService {
return &StoreAPIService{} return &StoreAPIService{}
} }

View File

@ -35,7 +35,7 @@ func WithUserAPIErrorHandler(h ErrorHandler) UserAPIOption {
} }
// NewUserAPIController creates a default api controller // NewUserAPIController creates a default api controller
func NewUserAPIController(s UserAPIServicer, opts ...UserAPIOption) Router { func NewUserAPIController(s UserAPIServicer, opts ...UserAPIOption) *UserAPIController {
controller := &UserAPIController{ controller := &UserAPIController{
service: s, service: s,
errorHandler: DefaultErrorHandler, errorHandler: DefaultErrorHandler,
@ -118,7 +118,7 @@ func (c *UserAPIController) CreateUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// CreateUsersWithArrayInput - Creates list of users with given input array // CreateUsersWithArrayInput - Creates list of users with given input array
@ -143,7 +143,7 @@ func (c *UserAPIController) CreateUsersWithArrayInput(w http.ResponseWriter, r *
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// CreateUsersWithListInput - Creates list of users with given input array // CreateUsersWithListInput - Creates list of users with given input array
@ -168,7 +168,7 @@ func (c *UserAPIController) CreateUsersWithListInput(w http.ResponseWriter, r *h
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// DeleteUser - Delete user // DeleteUser - Delete user
@ -204,7 +204,7 @@ func (c *UserAPIController) DeleteUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// GetUserByName - Get user by user name // GetUserByName - Get user by user name
@ -221,7 +221,7 @@ func (c *UserAPIController) GetUserByName(w http.ResponseWriter, r *http.Request
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// LoginUser - Logs user into the system // LoginUser - Logs user into the system
@ -270,7 +270,7 @@ func (c *UserAPIController) LoginUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// LogoutUser - Logs out current logged in user session // LogoutUser - Logs out current logged in user session
@ -282,7 +282,7 @@ func (c *UserAPIController) LogoutUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }
// UpdateUser - Updated user // UpdateUser - Updated user
@ -314,5 +314,5 @@ func (c *UserAPIController) UpdateUser(w http.ResponseWriter, r *http.Request) {
return return
} }
// If no error, encode the body and the result code // If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w) _ = EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
} }

View File

@ -23,7 +23,7 @@ type UserAPIService struct {
} }
// NewUserAPIService creates a default api service // NewUserAPIService creates a default api service
func NewUserAPIService() UserAPIServicer { func NewUserAPIService() *UserAPIService {
return &UserAPIService{} return &UserAPIService{}
} }

View File

@ -34,9 +34,9 @@ func (e *ParsingError) Unwrap() error {
func (e *ParsingError) Error() string { func (e *ParsingError) Error() string {
if e.Param == "" { if e.Param == "" {
return e.Err.Error() return e.Err.Error()
} else {
return e.Param + ": " + e.Err.Error()
} }
return e.Param + ": " + e.Err.Error()
} }
// RequiredError indicates that an error has occurred when parsing request parameters // RequiredError indicates that an error has occurred when parsing request parameters
@ -54,15 +54,21 @@ type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error, result
// DefaultErrorHandler defines the default logic on how to handle errors from the controller. Any errors from parsing // DefaultErrorHandler defines the default logic on how to handle errors from the controller. Any errors from parsing
// request params will return a StatusBadRequest. Otherwise, the error code originating from the servicer will be used. // request params will return a StatusBadRequest. Otherwise, the error code originating from the servicer will be used.
func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error, result *ImplResponse) { func DefaultErrorHandler(w http.ResponseWriter, _ *http.Request, err error, result *ImplResponse) {
if _, ok := err.(*ParsingError); ok { var parsingErr *ParsingError
if ok := errors.As(err, &parsingErr); ok {
// Handle parsing errors // Handle parsing errors
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest), map[string][]string{}, w) _ = EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest), map[string][]string{}, w)
} else if _, ok := err.(*RequiredError); ok { return
}
var requiredErr *RequiredError
if ok := errors.As(err, &requiredErr); ok {
// Handle missing required errors // Handle missing required errors
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusUnprocessableEntity), map[string][]string{}, w) _ = EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusUnprocessableEntity), map[string][]string{}, w)
} else { return
}
// Handle all other errors // Handle all other errors
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w) _ = EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
}
} }

View File

@ -61,7 +61,7 @@ func AssertRecurseValueRequired[T any](value reflect.Value, callback func(T) err
// If it is a slice we continue recursion // If it is a slice we continue recursion
case reflect.Slice: case reflect.Slice:
for i := 0; i < value.Len(); i += 1 { for i := 0; i < value.Len(); i++ {
if err := AssertRecurseValueRequired(value.Index(i), callback); err != nil { if err := AssertRecurseValueRequired(value.Index(i), callback); err != nil {
return err return err
} }

View File

@ -49,9 +49,9 @@ func NewColourFromValue(v string) (Colour, error) {
ev := Colour(v) ev := Colour(v)
if ev.IsValid() { if ev.IsValid() {
return ev, nil return ev, nil
} else {
return "", fmt.Errorf("invalid value '%v' for Colour: valid values are %v", v, AllowedColourEnumValues)
} }
return "", fmt.Errorf("invalid value '%v' for Colour: valid values are %v", v, AllowedColourEnumValues)
} }

View File

@ -49,9 +49,9 @@ func NewGenderFromValue(v string) (Gender, error) {
ev := Gender(v) ev := Gender(v)
if ev.IsValid() { if ev.IsValid() {
return ev, nil return ev, nil
} else {
return "", fmt.Errorf("invalid value '%v' for Gender: valid values are %v", v, AllowedGenderEnumValues)
} }
return "", fmt.Errorf("invalid value '%v' for Gender: valid values are %v", v, AllowedGenderEnumValues)
} }

View File

@ -58,9 +58,9 @@ func NewSpeciesFromValue(v string) (Species, error) {
ev := Species(v) ev := Species(v)
if ev.IsValid() { if ev.IsValid() {
return ev, nil return ev, nil
} else {
return "", fmt.Errorf("invalid value '%v' for Species: valid values are %v", v, AllowedSpeciesEnumValues)
} }
return "", fmt.Errorf("invalid value '%v' for Species: valid values are %v", v, AllowedSpeciesEnumValues)
} }

View File

@ -50,8 +50,7 @@ func NewRouter(routers ...Router) chi.Router {
router.Use(middleware.Logger) router.Use(middleware.Logger)
for _, api := range routers { for _, api := range routers {
for _, route := range api.Routes() { for _, route := range api.Routes() {
var handler http.Handler var handler http.Handler = route.HandlerFunc
handler = route.HandlerFunc
router.Method(route.Method, route.Pattern, handler) router.Method(route.Method, route.Pattern, handler)
} }
} }
@ -341,7 +340,7 @@ func parseNumericArrayParameter[T Number](param, delim string, required bool, fn
} }
// parseQuery parses query paramaters and returns an error if any malformed value pairs are encountered. // parseQuery parses query parameters and returns an error if any malformed value pairs are encountered.
func parseQuery(rawQuery string) (url.Values, error) { func parseQuery(rawQuery string) (url.Values, error) {
return url.ParseQuery(rawQuery) return url.ParseQuery(rawQuery)
} }