Skip to content

Commit

Permalink
feat(golang): add support for boolean type for query arguments
Browse files Browse the repository at this point in the history
Part of #49
  • Loading branch information
php-coder committed Apr 14, 2024
1 parent e6cd468 commit bb0fe9d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
11 changes: 10 additions & 1 deletion examples/go/chi/mysql/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "fmt"
import "io"
import "net/http"
import "os"
import "strconv"
import "github.com/go-chi/chi"
import "github.com/jmoiron/sqlx"

Expand All @@ -29,6 +30,14 @@ type CreateCategoryDto struct {
UserId *int `json:"user_id" db:"user_id"`
}

func parseBoolean(value string) bool {
boolValue, err := strconv.ParseBool(value)
if err != nil {
boolValue = false
}
return boolValue
}

func registerRoutes(r chi.Router, db *sqlx.DB) {

r.Get("/v1/categories/count", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -120,7 +129,7 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {

result := []CategoryDto{}
args := map[string]interface{}{
"hidden": r.URL.Query().Get("hidden"),
"hidden": parseBoolean(r.URL.Query().Get("hidden")),
}
err = stmt.Select(&result, args)
switch err {
Expand Down
9 changes: 7 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ const createEndpoints = async (destDir, { lang }, config) => {

// [ "p.page", "b.num" ] => '"page": chi.URLParam(r, "page"),\n\t\t\t"num": dto.Num),'
// (used only with Golang's go-chi)
"formatParamsAsGolangMap": (params) => {
"formatParamsAsGolangMap": (params, method) => {
if (params.length === 0) {
return params
}
Expand All @@ -284,9 +284,14 @@ const createEndpoints = async (destDir, { lang }, config) => {
const paramName = p.substring(2)
const formatFunc = placeholdersMap['go'][bindTarget]
const quotedParam = '"' + paramName + '":'
let extractParamExpr = formatFunc(paramName)
// LATER: add support for path (method.params.path) and body (method.dto.fields) parameters
if (bindTarget === 'q' && retrieveType(method.params.query, paramName) === 'boolean') {
extractParamExpr = `parseBoolean(${extractParamExpr})`
}
// We don't count quotes and colon because they are compensated by "p." prefix.
// We do +1 because the longest parameter will also have an extra space as a delimiter.
return `${quotedParam.padEnd(maxParamNameLength+1)} ${formatFunc(paramName)},`
return `${quotedParam.padEnd(maxParamNameLength+1)} ${extractParamExpr},`
}
).join('\n\t\t\t')
},
Expand Down
19 changes: 15 additions & 4 deletions src/templates/routes.go.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import "fmt"
import "io"
import "net/http"
import "os"
<%# LATER: add it only when there is at least one parameter of boolean type -%>
import "strconv"
import "github.com/go-chi/chi"
import "github.com/jmoiron/sqlx"

Expand Down Expand Up @@ -175,6 +177,15 @@ endpoints.forEach(function(endpoint) {
})
})
-%>
<%# LATER: add it only when there is at least one parameter of boolean type -%>
func parseBoolean(value string) bool {
boolValue, err := strconv.ParseBool(value)
if err != nil {
boolValue = false
}
return boolValue
}

func registerRoutes(r chi.Router, db *sqlx.DB) {
<%
endpoints.forEach(function(endpoint) {
Expand Down Expand Up @@ -219,7 +230,7 @@ endpoints.forEach(function(endpoint) {
<%- resultVariableDeclaration %>
args := map[string]interface{}{
<%- formatParamsAsGolangMap(params) %>
<%- formatParamsAsGolangMap(params, method) %>
}
err = stmt.<%- queryFunction %>(&result, args)
<% } else { -%>
Expand All @@ -246,7 +257,7 @@ endpoints.forEach(function(endpoint) {
json.NewDecoder(r.Body).Decode(&body)
args := map[string]interface{}{
<%- formatParamsAsGolangMap(params) %>
<%- formatParamsAsGolangMap(params, method) %>
}
_, err := db.NamedExec(
"<%- formatQuery(method.query) %>",
Expand All @@ -269,7 +280,7 @@ endpoints.forEach(function(endpoint) {
json.NewDecoder(r.Body).Decode(&body)
args := map[string]interface{}{
<%- formatParamsAsGolangMap(params) %>
<%- formatParamsAsGolangMap(params, method) %>
}
_, err := db.NamedExec(
"<%- formatQuery(method.query) %>",
Expand All @@ -289,7 +300,7 @@ endpoints.forEach(function(endpoint) {
%>
r.Delete("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
args := map[string]interface{}{
<%- formatParamsAsGolangMap(params) %>
<%- formatParamsAsGolangMap(params, method) %>
}
_, err := db.NamedExec(
"<%- formatQuery(method.query) %>",
Expand Down

0 comments on commit bb0fe9d

Please sign in to comment.