Skip to content

Commit 056ecce

Browse files
committed
refactor(golang): extract method obtainDtoName()
1 parent 584d9f0 commit 056ecce

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/templates/routes.go.ejs

+8-6
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ function dtoInCache(dto) {
149149
return dtoCache.hasOwnProperty(dto.signature)
150150
}
151151
152+
function obtainDtoName(dto) {
153+
const cacheKey = dto ? dto.signature : null
154+
return dtoInCache(dto) ? dtoCache[cacheKey] : dto.name
155+
}
156+
152157
const verbs_with_dto = [ 'get', 'post', 'put' ]
153158
endpoints.forEach(function(endpoint) {
154159
const dtos = endpoint.methods
@@ -181,8 +186,7 @@ endpoints.forEach(function(endpoint) {
181186
if (hasGetOne || hasGetMany) {
182187
const dto = query2dto(sqlParser, method)
183188
// LATER: do we really need signature and cache?
184-
const cacheKey = dto ? dto.signature : null
185-
const dtoName = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name
189+
const dataType = obtainDtoName(dto)
186190
const resultVariableDeclaration = hasGetMany
187191
? `result := []${dtoName}\{\}`
188192
: `var result ${dtoName}`
@@ -227,8 +231,7 @@ endpoints.forEach(function(endpoint) {
227231
if (method.name === 'post') {
228232
const dto = query2dto(sqlParser, method)
229233
// LATER: do we really need signature and cache?
230-
const cacheKey = dto ? dto.signature : null
231-
const dataType = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name
234+
const dataType = obtainDtoName(dto)
232235
%>
233236
r.Post("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
234237
var body <%- dataType %>
@@ -254,8 +257,7 @@ endpoints.forEach(function(endpoint) {
254257
if (method.name === 'put') {
255258
const dto = query2dto(sqlParser, method)
256259
// LATER: do we really need signature and cache?
257-
const cacheKey = dto ? dto.signature : null
258-
const dataType = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name
260+
const dataType = obtainDtoName(dto)
259261
%>
260262
r.Put("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
261263
var body <%- dataType %>

0 commit comments

Comments
 (0)