Skip to content

Commit

Permalink
fix(golang): fix get_list method to return [] instead of null for emp…
Browse files Browse the repository at this point in the history
…ty result set

Relate to #9
Relate to #13
  • Loading branch information
php-coder committed Mar 28, 2024
1 parent 2482db2 commit 00ef4cd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/go/chi/mysql/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
return
}

var result []CategoryDto
result := []CategoryDto{}
args := map[string]interface{}{
"limit": r.URL.Query().Get("limit"),
}
Expand Down
8 changes: 5 additions & 3 deletions src/templates/routes.go.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ endpoints.forEach(function(endpoint) {
// LATER: do we really need signature and cache?
const cacheKey = dto ? dto.signature : null;
const dtoName = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
const dataType = hasGetMany ? '[]' + dtoName : dtoName;
const resultVariableDeclaration = hasGetMany
? `result := []${dtoName}\{\}`
: `var result ${dtoName}`;
const queryFunction = hasGetOne ? 'Get' : 'Select';
// LATER: handle only particular method (get/post/put)
Expand All @@ -192,13 +194,13 @@ endpoints.forEach(function(endpoint) {
return
}
var result <%- dataType %>
<%- resultVariableDeclaration %>
args := map[string]interface{}{
<%- /* LATER: extract */ params.map(p => `"${p.substring(2)}": ${placeholdersMap['go'][p.substring(0, 1)](p.substring(2))},`).join('\n\t\t\t') %>
}
err = stmt.<%- queryFunction %>(&result, args)
<% } else { -%>
var result <%- dataType %>
<%- resultVariableDeclaration %>
err := db.<%- queryFunction %>(&result, "<%- formatQuery(method.query) %>")
<% } -%>
switch err {
Expand Down

0 comments on commit 00ef4cd

Please sign in to comment.