@@ -20,7 +20,7 @@ import "github.com/jmoiron/sqlx"
20
20
// } => [ 'nameRu' ]
21
21
function extractSelectParameters (queryAst ) {
22
22
return queryAst .columns
23
- .map (column => column .as !== null ? column .as : column .expr .column );
23
+ .map (column => column .as !== null ? column .as : column .expr .column )
24
24
}
25
25
26
26
// {'values':
@@ -34,8 +34,8 @@ function extractSelectParameters(queryAst) {
34
34
function extractInsertValues (queryAst ) {
35
35
const values = queryAst .values .flatMap (elem => elem .value )
36
36
.map (elem => elem .type === ' param' ? elem .value : null )
37
- .filter (elem => elem); // filter out nulls
38
- return Array .from (new Set (values));
37
+ .filter (elem => elem) // filter out nulls
38
+ return Array .from (new Set (values))
39
39
}
40
40
41
41
// {'set':
@@ -56,27 +56,27 @@ function extractUpdateValues(queryAst) {
56
56
// LATER: consider taking into account b.params from WHERE clause
57
57
function extractProperties (queryAst ) {
58
58
if (queryAst .type === ' select' ) {
59
- return extractSelectParameters (queryAst);
59
+ return extractSelectParameters (queryAst)
60
60
}
61
61
62
62
if (queryAst .type === ' insert' ) {
63
- return extractInsertValues (queryAst);
63
+ return extractInsertValues (queryAst)
64
64
}
65
65
66
66
if (queryAst .type === ' update' ) {
67
- return extractUpdateValues (queryAst);
67
+ return extractUpdateValues (queryAst)
68
68
}
69
69
70
- return [];
70
+ return []
71
71
}
72
72
73
73
function findOutType (fieldsInfo , fieldName ) {
74
- const defaultType = ' *string' ;
75
- const hasTypeInfo = fieldsInfo .hasOwnProperty (fieldName) && fieldsInfo[fieldName].hasOwnProperty (' type' );
74
+ const defaultType = ' *string'
75
+ const hasTypeInfo = fieldsInfo .hasOwnProperty (fieldName) && fieldsInfo[fieldName].hasOwnProperty (' type' )
76
76
if (hasTypeInfo && fieldsInfo[fieldName].type === ' integer' ) {
77
- return ' *int' ;
77
+ return ' *int'
78
78
}
79
- return defaultType;
79
+ return defaultType
80
80
}
81
81
82
82
function addTypes (props , fieldsInfo ) {
@@ -85,23 +85,23 @@ function addTypes(props, fieldsInfo) {
85
85
" name" : prop,
86
86
" type" : findOutType (fieldsInfo, prop),
87
87
}
88
- });
88
+ })
89
89
}
90
90
91
91
function query2dto (parser , method ) {
92
- const query = removePlaceholders (method .query );
93
- const queryAst = parser .astify (query);
94
- const props = extractProperties (queryAst);
92
+ const query = removePlaceholders (method .query )
93
+ const queryAst = parser .astify (query)
94
+ const props = extractProperties (queryAst)
95
95
if (props .length === 0 ) {
96
- console .warn (' Could not create DTO for query:' , formatQuery (query));
97
- console .debug (' Query AST:' );
98
- console .debug (queryAst);
99
- return null ;
96
+ console .warn (' Could not create DTO for query:' , formatQuery (query))
97
+ console .debug (' Query AST:' )
98
+ console .debug (queryAst)
99
+ return null
100
100
}
101
- const fieldsInfo = method .dto && method .dto .fields ? method .dto .fields : {};
102
- const propsWithTypes = addTypes (props, fieldsInfo);
103
- const hasName = method .dto && method .dto .name && method .dto .name .length > 0 ;
104
- const name = hasName ? method .dto .name : " Dto" + ++ globalDtoCounter;
101
+ const fieldsInfo = method .dto && method .dto .fields ? method .dto .fields : {}
102
+ const propsWithTypes = addTypes (props, fieldsInfo)
103
+ const hasName = method .dto && method .dto .name && method .dto .name .length > 0
104
+ const name = hasName ? method .dto .name : " Dto" + ++ globalDtoCounter
105
105
return {
106
106
" name" : name,
107
107
" hasUserProvidedName" : hasName,
@@ -112,33 +112,33 @@ function query2dto(parser, method) {
112
112
// [ {name:foo, type:int}, {name:bar, type:string} ] => "foo=int bar=string"
113
113
// LATER: sort before join
114
114
" signature" : propsWithTypes .map (field => ` ${ field .name } =${ field .type } ` ).join (' ' )
115
- };
115
+ }
116
116
}
117
117
118
118
function dto2struct (dto ) {
119
- let result = ` type ${ dto .name } struct {\n ` ;
119
+ let result = ` type ${ dto .name } struct {\n `
120
120
dto .props .forEach (prop => {
121
- const fieldName = capitalize (snake2camelCase (prop .name )).padEnd (dto .maxFieldNameLength );
121
+ const fieldName = capitalize (snake2camelCase (prop .name )).padEnd (dto .maxFieldNameLength )
122
122
result += ` \t ${ fieldName} ${ prop .type } \` json:"${ prop .name } " db:"${ prop .name } "\`\n `
123
- });
124
- result += ' }\n ' ;
123
+ })
124
+ result += ' }\n '
125
125
126
- return result;
126
+ return result
127
127
}
128
128
129
- let globalDtoCounter = 0 ;
129
+ let globalDtoCounter = 0
130
130
131
- const dtoCache = {};
131
+ const dtoCache = {}
132
132
function cacheDto (dto ) {
133
- dtoCache[dto .signature ] = dto .name ;
134
- return dto;
133
+ dtoCache[dto .signature ] = dto .name
134
+ return dto
135
135
}
136
136
function dtoInCache (dto ) {
137
137
// always prefer user specified name even when we have a similar DTO in cache
138
138
if (dto .hasUserProvidedName ) {
139
- return false ;
139
+ return false
140
140
}
141
- return dtoCache .hasOwnProperty (dto .signature );
141
+ return dtoCache .hasOwnProperty (dto .signature )
142
142
}
143
143
144
144
const verbs_with_dto = [ ' get' , ' post' , ' put' ]
@@ -154,32 +154,32 @@ endpoints.forEach(function(endpoint) {
154
154
-% >
155
155
< %- struct % >
156
156
< %
157
- });
158
- });
157
+ })
158
+ })
159
159
- %>
160
160
func registerRoutes(r chi.Router, db *sqlx.DB) {
161
161
<%
162
162
endpoints .forEach (function (endpoint ) {
163
- const path = convertPathPlaceholders (endpoint .path );
163
+ const path = convertPathPlaceholders (endpoint .path )
164
164
165
165
endpoint .methods .forEach (function (method ) {
166
166
if (! method .query ) {
167
167
// filter out aggregated_queries for a while (see #17)
168
168
return
169
169
}
170
- const params = extractParamsFromQuery (method .query );
171
- const hasGetOne = method .name === ' get' ;
172
- const hasGetMany = method .name === ' get_list' ;
170
+ const params = extractParamsFromQuery (method .query )
171
+ const hasGetOne = method .name === ' get'
172
+ const hasGetMany = method .name === ' get_list'
173
173
if (hasGetOne || hasGetMany) {
174
- const dto = query2dto (sqlParser, method);
174
+ const dto = query2dto (sqlParser, method)
175
175
// LATER: do we really need signature and cache?
176
- const cacheKey = dto ? dto .signature : null ;
177
- const dtoName = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name ;
176
+ const cacheKey = dto ? dto .signature : null
177
+ const dtoName = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name
178
178
const resultVariableDeclaration = hasGetMany
179
179
? ` result := []${ dtoName} \{\} `
180
- : ` var result ${ dtoName} ` ;
180
+ : ` var result ${ dtoName} `
181
181
182
- const queryFunction = hasGetOne ? ' Get' : ' Select' ;
182
+ const queryFunction = hasGetOne ? ' Get' : ' Select'
183
183
// LATER: handle only particular method (get/post/put)
184
184
// LATER: include method/path into an error message
185
185
% >
@@ -217,10 +217,10 @@ endpoints.forEach(function(endpoint) {
217
217
< %
218
218
}
219
219
if (method .name === ' post' ) {
220
- const dto = query2dto (sqlParser, method);
220
+ const dto = query2dto (sqlParser, method)
221
221
// LATER: do we really need signature and cache?
222
- const cacheKey = dto ? dto .signature : null ;
223
- const dataType = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name ;
222
+ const cacheKey = dto ? dto .signature : null
223
+ const dataType = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name
224
224
% >
225
225
r .Post (" <%- path %>" , func (w http .ResponseWriter , r * http .Request ) {
226
226
var body < %- dataType % >
@@ -244,10 +244,10 @@ endpoints.forEach(function(endpoint) {
244
244
< %
245
245
}
246
246
if (method .name === ' put' ) {
247
- const dto = query2dto (sqlParser, method);
247
+ const dto = query2dto (sqlParser, method)
248
248
// LATER: do we really need signature and cache?
249
- const cacheKey = dto ? dto .signature : null ;
250
- const dataType = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name ;
249
+ const cacheKey = dto ? dto .signature : null
250
+ const dataType = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name
251
251
% >
252
252
r .Put (" <%- path %>" , func (w http .ResponseWriter , r * http .Request ) {
253
253
var body < %- dataType % >
@@ -290,7 +290,7 @@ endpoints.forEach(function(endpoint) {
290
290
})
291
291
< %
292
292
}
293
- });
293
+ })
294
294
})
295
295
%>
296
296
}
0 commit comments