Ian Cubbon 1dd9590064
[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
2023-10-17 11:35:39 +08:00

76 lines
1.6 KiB
Go

/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package petstoreserver
import (
"fmt"
)
type Species string
// List of Species
const (
CAT Species = "cat"
DOG Species = "dog"
FISH Species = "fish"
GOAT Species = "goat"
PIG Species = "pig"
)
// AllowedSpeciesEnumValues is all the allowed values of Species enum
var AllowedSpeciesEnumValues = []Species{
"cat",
"dog",
"fish",
"goat",
"pig",
}
// validSpeciesEnumValue provides a map of Speciess for fast verification of use input
var validSpeciesEnumValues = map[Species]struct{}{
"cat": {},
"dog": {},
"fish": {},
"goat": {},
"pig": {},
}
// IsValid return true if the value is valid for the enum, false otherwise
func (v Species) IsValid() bool {
_, ok := validSpeciesEnumValues[v]
return ok
}
// NewSpeciesFromValue returns a pointer to a valid Species
// for the value passed as argument, or an error if the value passed is not allowed by the enum
func NewSpeciesFromValue(v string) (Species, error) {
ev := Species(v)
if ev.IsValid() {
return ev, nil
} else {
return "", fmt.Errorf("invalid value '%v' for Species: valid values are %v", v, AllowedSpeciesEnumValues)
}
}
// AssertSpeciesRequired checks if the required fields are not zero-ed
func AssertSpeciesRequired(obj Species) error {
return nil
}
// AssertSpeciesConstraints checks if the values respects the defined constraints
func AssertSpeciesConstraints(obj Species) error {
return nil
}