[Go-server] - Support for Enums in the Path and Query (#16781)

* Add support for Enums to be part of the Path and Query
Supports optional Query Enums and Lists of Enums as well
Add an example endpoint that covers the added scenarios
Added an import mapping for the GoServerCodegen for "fmt"
    when a model is an enum.
Expanded the Enum Model for the Go Server to have validation.
    Copied this logic from the Go Client Enum Model.

* Fix identation of examples

* Pre-allocate the capacity of the slice of Enums to be
the correct size.

* Formatting and updated examples

* Empty-Commit

* Switch to using a map to store the valid enum values

* Fixed pointer derefs missed from previous change in PR

* More fixing of pointer to not pointer

* Create a map for validation and a list of enums for messaging
This commit is contained in:
Ian Cubbon
2023-10-16 20:35:39 -07:00
committed by GitHub
parent d1fa38e286
commit 1dd9590064
19 changed files with 718 additions and 9 deletions

View File

@@ -60,6 +60,11 @@ func (c *PetAPIController) Routes() Routes {
"/v2/pet/{petId}",
c.DeletePet,
},
"FilterPetsByCategory": Route{
strings.ToUpper("Get"),
"/v2/pet/filterPets/{gender}",
c.FilterPetsByCategory,
},
"FindPetsByStatus": Route{
strings.ToUpper("Get"),
"/v2/pet/findByStatus",
@@ -147,10 +152,54 @@ func (c *PetAPIController) DeletePet(w http.ResponseWriter, r *http.Request) {
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
}
// FilterPetsByCategory - Finds Pets
func (c *PetAPIController) FilterPetsByCategory(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
query := r.URL.Query()
genderParam, err := NewGenderFromValue(params["gender"])
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
if !query.Has("species"){
c.errorHandler(w, r, &RequiredError{"species"}, nil)
return
}
speciesParam, err := NewSpeciesFromValue(query.Get("species"))
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
var notSpeciesParam []Species
if query.Has("notSpecies") {
paramSplits := strings.Split(query.Get("notSpecies"), ",")
notSpeciesParam = make([]Species, 0, len(paramSplits))
for _, param := range paramSplits {
paramEnum, err := NewSpeciesFromValue(param)
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
notSpeciesParam = append(notSpeciesParam, paramEnum)
}
}
result, err := c.service.FilterPetsByCategory(r.Context(), genderParam, speciesParam, notSpeciesParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
}
// FindPetsByStatus - Finds Pets by status
func (c *PetAPIController) FindPetsByStatus(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
statusParam := strings.Split(query.Get("status"), ",")
var statusParam []string
if query.Has("status") {
statusParam = strings.Split(query.Get("status"), ",")
}
result, err := c.service.FindPetsByStatus(r.Context(), statusParam)
// If an error occurred, encode the error with the status code
if err != nil {
@@ -165,7 +214,10 @@ func (c *PetAPIController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque
// Deprecated
func (c *PetAPIController) FindPetsByTags(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
tagsParam := strings.Split(query.Get("tags"), ",")
var tagsParam []string
if query.Has("tags") {
tagsParam = strings.Split(query.Get("tags"), ",")
}
result, err := c.service.FindPetsByTags(r.Context(), tagsParam)
// If an error occurred, encode the error with the status code
if err != nil {