Skip to content

Commit

Permalink
Add handling for parsing optional bool params & regenerate samples (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
scevallos authored Jan 30, 2023
1 parent b538c2a commit 8540c82
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
}
{{/isInteger}}
{{#isBoolean}}
{{paramName}}Param, err := parseBoolParameter(query.Get("{{baseName}}"))
{{paramName}}Param, err := parseBoolParameter(query.Get("{{baseName}}"), {{required}})
if err != nil {
w.WriteHeader(500)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,15 @@ func parseInt32Parameter(param string, required bool) (int32, error) {
}

// parseBoolParameter parses a string parameter to a bool
func parseBoolParameter(param string) (bool, error) {
func parseBoolParameter(param string, required bool) (bool, error) {
if param == "" {
if required {
return false, errors.New(errMsgRequiredMissing)
}

return false, nil
}

val, err := strconv.ParseBool(param)
if err != nil {
return false, err
Expand Down
10 changes: 9 additions & 1 deletion samples/server/petstore/go-api-server/go/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,15 @@ func parseInt32Parameter(param string, required bool) (int32, error) {
}

// parseBoolParameter parses a string parameter to a bool
func parseBoolParameter(param string) (bool, error) {
func parseBoolParameter(param string, required bool) (bool, error) {
if param == "" {
if required {
return false, errors.New(errMsgRequiredMissing)
}

return false, nil
}

val, err := strconv.ParseBool(param)
if err != nil {
return false, err
Expand Down
10 changes: 9 additions & 1 deletion samples/server/petstore/go-chi-server/go/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,15 @@ func parseInt32Parameter(param string, required bool) (int32, error) {
}

// parseBoolParameter parses a string parameter to a bool
func parseBoolParameter(param string) (bool, error) {
func parseBoolParameter(param string, required bool) (bool, error) {
if param == "" {
if required {
return false, errors.New(errMsgRequiredMissing)
}

return false, nil
}

val, err := strconv.ParseBool(param)
if err != nil {
return false, err
Expand Down
10 changes: 9 additions & 1 deletion samples/server/petstore/go-server-required/go/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,15 @@ func parseInt32Parameter(param string, required bool) (int32, error) {
}

// parseBoolParameter parses a string parameter to a bool
func parseBoolParameter(param string) (bool, error) {
func parseBoolParameter(param string, required bool) (bool, error) {
if param == "" {
if required {
return false, errors.New(errMsgRequiredMissing)
}

return false, nil
}

val, err := strconv.ParseBool(param)
if err != nil {
return false, err
Expand Down

0 comments on commit 8540c82

Please sign in to comment.