Skip to content

Commit 00ef4cd

Browse files
committed
fix(golang): fix get_list method to return [] instead of null for empty result set
Relate to #9 Relate to #13
1 parent 2482db2 commit 00ef4cd

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

examples/go/chi/mysql/routes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
8484
return
8585
}
8686

87-
var result []CategoryDto
87+
result := []CategoryDto{}
8888
args := map[string]interface{}{
8989
"limit": r.URL.Query().Get("limit"),
9090
}

src/templates/routes.go.ejs

+5-3
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ endpoints.forEach(function(endpoint) {
175175
// LATER: do we really need signature and cache?
176176
const cacheKey = dto ? dto.signature : null;
177177
const dtoName = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
178-
const dataType = hasGetMany ? '[]' + dtoName : dtoName;
178+
const resultVariableDeclaration = hasGetMany
179+
? `result := []${dtoName}\{\}`
180+
: `var result ${dtoName}`;
179181
180182
const queryFunction = hasGetOne ? 'Get' : 'Select';
181183
// LATER: handle only particular method (get/post/put)
@@ -192,13 +194,13 @@ endpoints.forEach(function(endpoint) {
192194
return
193195
}
194196
195-
var result <%- dataType %>
197+
<%- resultVariableDeclaration %>
196198
args := map[string]interface{}{
197199
<%- /* LATER: extract */ params.map(p => `"${p.substring(2)}": ${placeholdersMap['go'][p.substring(0, 1)](p.substring(2))},`).join('\n\t\t\t') %>
198200
}
199201
err = stmt.<%- queryFunction %>(&result, args)
200202
<% } else { -%>
201-
var result <%- dataType %>
203+
<%- resultVariableDeclaration %>
202204
err := db.<%- queryFunction %>(&result, "<%- formatQuery(method.query) %>")
203205
<% } -%>
204206
switch err {

0 commit comments

Comments
 (0)