[go-server] Add ability to handle parameters of number type (#15079)

* Add ability to handle parameters of number type

* Generate samples

* Add handling for number without format

* Regenerate samples

* Fix indentation
This commit is contained in:
Ween Jiann
2023-04-11 15:38:58 +08:00
committed by GitHub
parent 2b796d5c61
commit 8ce990d3d7
14 changed files with 415 additions and 96 deletions

View File

@@ -133,7 +133,6 @@ func (c *PetApiController) DeletePet(w http.ResponseWriter, r *http.Request) {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
apiKeyParam := r.Header.Get("api_key")
result, err := c.service.DeletePet(r.Context(), petIdParam, apiKeyParam)
// If an error occurred, encode the error with the status code
@@ -185,7 +184,6 @@ func (c *PetApiController) GetPetById(w http.ResponseWriter, r *http.Request) {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
result, err := c.service.GetPetById(r.Context(), petIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
@@ -233,7 +231,6 @@ func (c *PetApiController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
nameParam := r.FormValue("name")
statusParam := r.FormValue("status")
result, err := c.service.UpdatePetWithForm(r.Context(), petIdParam, nameParam, statusParam)
@@ -259,7 +256,6 @@ func (c *PetApiController) UploadFile(w http.ResponseWriter, r *http.Request) {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
additionalMetadataParam := r.FormValue("additionalMetadata")
fileParam, err := ReadFormFileToTempFile(r, "file")

View File

@@ -81,7 +81,6 @@ func (c *StoreApiController) Routes() Routes {
func (c *StoreApiController) DeleteOrder(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
orderIdParam := params["orderId"]
result, err := c.service.DeleteOrder(r.Context(), orderIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
@@ -114,7 +113,6 @@ func (c *StoreApiController) GetOrderById(w http.ResponseWriter, r *http.Request
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
result, err := c.service.GetOrderById(r.Context(), orderIdParam)
// If an error occurred, encode the error with the status code
if err != nil {

View File

@@ -181,7 +181,6 @@ func (c *UserApiController) CreateUsersWithListInput(w http.ResponseWriter, r *h
func (c *UserApiController) DeleteUser(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
usernameParam := params["username"]
result, err := c.service.DeleteUser(r.Context(), usernameParam)
// If an error occurred, encode the error with the status code
if err != nil {
@@ -197,7 +196,6 @@ func (c *UserApiController) DeleteUser(w http.ResponseWriter, r *http.Request) {
func (c *UserApiController) GetUserByName(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
usernameParam := params["username"]
result, err := c.service.GetUserByName(r.Context(), usernameParam)
// If an error occurred, encode the error with the status code
if err != nil {
@@ -242,7 +240,6 @@ func (c *UserApiController) LogoutUser(w http.ResponseWriter, r *http.Request) {
func (c *UserApiController) UpdateUser(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
usernameParam := params["username"]
userParam := User{}
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()

View File

@@ -139,8 +139,8 @@ func readFileHeaderToTempFile(fileHeader *multipart.FileHeader) (*os.File, error
return file, nil
}
// parseInt64Parameter parses a string parameter to an int64.
func parseInt64Parameter(param string, required bool) (int64, error) {
// parseFloatParameter parses a string parameter to an int64.
func parseFloatParameter(param string, bitSize int, required bool) (float64, error) {
if param == "" {
if required {
return 0, errors.New(errMsgRequiredMissing)
@@ -149,25 +149,42 @@ func parseInt64Parameter(param string, required bool) (int64, error) {
return 0, nil
}
return strconv.ParseInt(param, 10, 64)
return strconv.ParseFloat(param, bitSize)
}
// parseFloat64Parameter parses a string parameter to an float64.
func parseFloat64Parameter(param string, required bool) (float64, error) {
return parseFloatParameter(param, 64, required)
}
// parseFloat32Parameter parses a string parameter to an float32.
func parseFloat32Parameter(param string, required bool) (float32, error) {
val, err := parseFloatParameter(param, 32, required)
return float32(val), err
}
// parseIntParameter parses a string parameter to an int64.
func parseIntParameter(param string, bitSize int, required bool) (int64, error) {
if param == "" {
if required {
return 0, errors.New(errMsgRequiredMissing)
}
return 0, nil
}
return strconv.ParseInt(param, 10, bitSize)
}
// parseInt64Parameter parses a string parameter to an int64.
func parseInt64Parameter(param string, required bool) (int64, error) {
return parseIntParameter(param, 64, required)
}
// parseInt32Parameter parses a string parameter to an int32.
func parseInt32Parameter(param string, required bool) (int32, error) {
if param == "" {
if required {
return 0, errors.New(errMsgRequiredMissing)
}
return 0, nil
}
val, err := strconv.ParseInt(param, 10, 32)
if err != nil {
return -1, err
}
return int32(val), nil
val, err := parseIntParameter(param, 32, required)
return int32(val), err
}
// parseBoolParameter parses a string parameter to a bool
@@ -188,6 +205,55 @@ func parseBoolParameter(param string, required bool) (bool, error) {
return bool(val), nil
}
// parseFloat64ArrayParameter parses a string parameter containing array of values to []Float64.
func parseFloat64ArrayParameter(param, delim string, required bool) ([]float64, error) {
if param == "" {
if required {
return nil, errors.New(errMsgRequiredMissing)
}
return nil, nil
}
str := strings.Split(param, delim)
floats := make([]float64, len(str))
for i, s := range str {
if v, err := strconv.ParseFloat(s, 64); err != nil {
return nil, err
} else {
floats[i] = v
}
}
return floats, nil
}
// parseFloat32ArrayParameter parses a string parameter containing array of values to []float32.
func parseFloat32ArrayParameter(param, delim string, required bool) ([]float32, error) {
if param == "" {
if required {
return nil, errors.New(errMsgRequiredMissing)
}
return nil, nil
}
str := strings.Split(param, delim)
floats := make([]float32, len(str))
for i, s := range str {
if v, err := strconv.ParseFloat(s, 32); err != nil {
return nil, err
} else {
floats[i] = float32(v)
}
}
return floats, nil
}
// parseInt64ArrayParameter parses a string parameter containing array of values to []int64.
func parseInt64ArrayParameter(param, delim string, required bool) ([]int64, error) {
if param == "" {