Skip to content

Commit ca5089a

Browse files
committed
refactor(golang): rename local variable "dto" to "body" for POST and PUT endpoints
1 parent 8323f34 commit ca5089a

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

examples/go/chi/mysql/routes.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
9292
})
9393

9494
r.Post("/v1/categories", func(w http.ResponseWriter, r *http.Request) {
95-
var dto CreateCategoryDto
96-
json.NewDecoder(r.Body).Decode(&dto)
95+
var body CreateCategoryDto
96+
json.NewDecoder(r.Body).Decode(&body)
9797

9898
args := map[string]interface{}{
99-
"name": dto.Name,
100-
"name_ru": dto.NameRu,
101-
"slug": dto.Slug,
102-
"user_id": dto.UserId,
99+
"name": body.Name,
100+
"name_ru": body.NameRu,
101+
"slug": body.Slug,
102+
"user_id": body.UserId,
103103
}
104104
_, err := db.NamedExec(
105105
"INSERT INTO categories ( name , name_ru , slug , created_at , created_by , updated_at , updated_by ) VALUES ( :name , :name_ru , :slug , NOW() , :user_id , NOW() , :user_id )",
@@ -140,14 +140,14 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
140140
})
141141

142142
r.Put("/v1/categories/{categoryId}", func(w http.ResponseWriter, r *http.Request) {
143-
var dto CreateCategoryDto
144-
json.NewDecoder(r.Body).Decode(&dto)
143+
var body CreateCategoryDto
144+
json.NewDecoder(r.Body).Decode(&body)
145145

146146
args := map[string]interface{}{
147-
"name": dto.Name,
148-
"name_ru": dto.NameRu,
149-
"slug": dto.Slug,
150-
"user_id": dto.UserId,
147+
"name": body.Name,
148+
"name_ru": body.NameRu,
149+
"slug": body.Slug,
150+
"user_id": body.UserId,
151151
"categoryId": chi.URLParam(r, "categoryId"),
152152
}
153153
_, err := db.NamedExec(

src/cli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ const createEndpoints = async (destDir, { lang }, config) => {
192192
return `chi.URLParam(r, "${param}")`
193193
},
194194
'b': function(param) {
195-
return 'dto.' + capitalize(snake2camelCase(param))
195+
return 'body.' + capitalize(snake2camelCase(param))
196196
},
197197
'q': function(param) {
198198
return `r.URL.Query().Get("${param}")`

src/templates/routes.go.ejs

+4-4
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ endpoints.forEach(function(endpoint) {
223223
const dataType = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
224224
%>
225225
r.Post("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
226-
var dto <%- dataType %>
227-
json.NewDecoder(r.Body).Decode(&dto)
226+
var body <%- dataType %>
227+
json.NewDecoder(r.Body).Decode(&body)
228228
229229
args := map[string]interface{}{
230230
<%- formatParamsAsGolangMap(params) %>
@@ -250,8 +250,8 @@ endpoints.forEach(function(endpoint) {
250250
const dataType = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
251251
%>
252252
r.Put("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
253-
var dto <%- dataType %>
254-
json.NewDecoder(r.Body).Decode(&dto)
253+
var body <%- dataType %>
254+
json.NewDecoder(r.Body).Decode(&body)
255255
256256
args := map[string]interface{}{
257257
<%- formatParamsAsGolangMap(params) %>

0 commit comments

Comments
 (0)