Skip to content

Commit bb0fe9d

Browse files
committed
feat(golang): add support for boolean type for query arguments
Part of #49
1 parent e6cd468 commit bb0fe9d

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

examples/go/chi/mysql/routes.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "fmt"
66
import "io"
77
import "net/http"
88
import "os"
9+
import "strconv"
910
import "github.com/go-chi/chi"
1011
import "github.com/jmoiron/sqlx"
1112

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

33+
func parseBoolean(value string) bool {
34+
boolValue, err := strconv.ParseBool(value)
35+
if err != nil {
36+
boolValue = false
37+
}
38+
return boolValue
39+
}
40+
3241
func registerRoutes(r chi.Router, db *sqlx.DB) {
3342

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

121130
result := []CategoryDto{}
122131
args := map[string]interface{}{
123-
"hidden": r.URL.Query().Get("hidden"),
132+
"hidden": parseBoolean(r.URL.Query().Get("hidden")),
124133
}
125134
err = stmt.Select(&result, args)
126135
switch err {

src/cli.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ const createEndpoints = async (destDir, { lang }, config) => {
272272

273273
// [ "p.page", "b.num" ] => '"page": chi.URLParam(r, "page"),\n\t\t\t"num": dto.Num),'
274274
// (used only with Golang's go-chi)
275-
"formatParamsAsGolangMap": (params) => {
275+
"formatParamsAsGolangMap": (params, method) => {
276276
if (params.length === 0) {
277277
return params
278278
}
@@ -284,9 +284,14 @@ const createEndpoints = async (destDir, { lang }, config) => {
284284
const paramName = p.substring(2)
285285
const formatFunc = placeholdersMap['go'][bindTarget]
286286
const quotedParam = '"' + paramName + '":'
287+
let extractParamExpr = formatFunc(paramName)
288+
// LATER: add support for path (method.params.path) and body (method.dto.fields) parameters
289+
if (bindTarget === 'q' && retrieveType(method.params.query, paramName) === 'boolean') {
290+
extractParamExpr = `parseBoolean(${extractParamExpr})`
291+
}
287292
// We don't count quotes and colon because they are compensated by "p." prefix.
288293
// We do +1 because the longest parameter will also have an extra space as a delimiter.
289-
return `${quotedParam.padEnd(maxParamNameLength+1)} ${formatFunc(paramName)},`
294+
return `${quotedParam.padEnd(maxParamNameLength+1)} ${extractParamExpr},`
290295
}
291296
).join('\n\t\t\t')
292297
},

src/templates/routes.go.ejs

+15-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import "fmt"
66
import "io"
77
import "net/http"
88
import "os"
9+
<%# LATER: add it only when there is at least one parameter of boolean type -%>
10+
import "strconv"
911
import "github.com/go-chi/chi"
1012
import "github.com/jmoiron/sqlx"
1113

@@ -175,6 +177,15 @@ endpoints.forEach(function(endpoint) {
175177
})
176178
})
177179
-%>
180+
<%# LATER: add it only when there is at least one parameter of boolean type -%>
181+
func parseBoolean(value string) bool {
182+
boolValue, err := strconv.ParseBool(value)
183+
if err != nil {
184+
boolValue = false
185+
}
186+
return boolValue
187+
}
188+
178189
func registerRoutes(r chi.Router, db *sqlx.DB) {
179190
<%
180191
endpoints.forEach(function(endpoint) {
@@ -219,7 +230,7 @@ endpoints.forEach(function(endpoint) {
219230
220231
<%- resultVariableDeclaration %>
221232
args := map[string]interface{}{
222-
<%- formatParamsAsGolangMap(params) %>
233+
<%- formatParamsAsGolangMap(params, method) %>
223234
}
224235
err = stmt.<%- queryFunction %>(&result, args)
225236
<% } else { -%>
@@ -246,7 +257,7 @@ endpoints.forEach(function(endpoint) {
246257
json.NewDecoder(r.Body).Decode(&body)
247258
248259
args := map[string]interface{}{
249-
<%- formatParamsAsGolangMap(params) %>
260+
<%- formatParamsAsGolangMap(params, method) %>
250261
}
251262
_, err := db.NamedExec(
252263
"<%- formatQuery(method.query) %>",
@@ -269,7 +280,7 @@ endpoints.forEach(function(endpoint) {
269280
json.NewDecoder(r.Body).Decode(&body)
270281
271282
args := map[string]interface{}{
272-
<%- formatParamsAsGolangMap(params) %>
283+
<%- formatParamsAsGolangMap(params, method) %>
273284
}
274285
_, err := db.NamedExec(
275286
"<%- formatQuery(method.query) %>",
@@ -289,7 +300,7 @@ endpoints.forEach(function(endpoint) {
289300
%>
290301
r.Delete("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
291302
args := map[string]interface{}{
292-
<%- formatParamsAsGolangMap(params) %>
303+
<%- formatParamsAsGolangMap(params, method) %>
293304
}
294305
_, err := db.NamedExec(
295306
"<%- formatQuery(method.query) %>",

0 commit comments

Comments
 (0)