Skip to content

Commit 2756738

Browse files
committed
style(python): remove trailing semicolons
1 parent 0c73d2b commit 2756738

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

src/templates/routes.py.ejs

+44-44
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ router = APIRouter()
1616
<%
1717
// { "get", "/v1/categories/:categoryId" } => "get_v1_categories_category_id"
1818
function generate_method_name(method, path) {
19-
const name = camel2snakeCase(path).replace(/\//g, '_').replace(/[^_a-z0-9]/g, '');
19+
const name = camel2snakeCase(path).replace(/\//g, '_').replace(/[^_a-z0-9]/g, '')
2020
return `${method}${name}`
2121
}
2222
@@ -39,7 +39,7 @@ function formatQueryForPython(query, indentLevel) {
3939
if (isMultilineSql) {
4040
const indent = ' '.repeat(indentLevel)
4141
const indentedSql = sql.replace(/\n/g, '\n' + indent)
42-
return `\n${indent}"""\n${indent}${indentedSql}\n${indent}"""`;
42+
return `\n${indent}"""\n${indent}${indentedSql}\n${indent}"""`
4343
}
4444
return `"${sql}"`
4545
}
@@ -56,8 +56,8 @@ function formatQueryForPython(query, indentLevel) {
5656
function extractInsertValues(queryAst) {
5757
const values = queryAst.values.flatMap(elem => elem.value)
5858
.map(elem => elem.type === 'param' ? elem.value : null)
59-
.filter(elem => elem); // filter out nulls
60-
return Array.from(new Set(values));
59+
.filter(elem => elem) // filter out nulls
60+
return Array.from(new Set(values))
6161
}
6262
6363
// LATER: reduce duplication with routes.go.ejs
@@ -79,22 +79,22 @@ function extractUpdateValues(queryAst) {
7979
// LATER: reduce duplication with routes.go.ejs
8080
function extractProperties(queryAst) {
8181
if (queryAst.type === 'insert') {
82-
return extractInsertValues(queryAst);
82+
return extractInsertValues(queryAst)
8383
}
8484
if (queryAst.type === 'update') {
85-
return extractUpdateValues(queryAst);
85+
return extractUpdateValues(queryAst)
8686
}
87-
return [];
87+
return []
8888
}
8989
9090
// LATER: try to reduce duplication with routes.go.ejs
9191
function findOutType(fieldsInfo, fieldName) {
92-
const defaultType = 'str';
93-
const hasTypeInfo = fieldsInfo.hasOwnProperty(fieldName) && fieldsInfo[fieldName].hasOwnProperty('type');
92+
const defaultType = 'str'
93+
const hasTypeInfo = fieldsInfo.hasOwnProperty(fieldName) && fieldsInfo[fieldName].hasOwnProperty('type')
9494
if (hasTypeInfo && fieldsInfo[fieldName].type === 'integer') {
95-
return 'int';
95+
return 'int'
9696
}
97-
return defaultType;
97+
return defaultType
9898
}
9999
100100
// LATER: reduce duplication with routes.go.ejs
@@ -104,24 +104,24 @@ function addTypes(props, fieldsInfo) {
104104
"name": prop,
105105
"type": findOutType(fieldsInfo, prop),
106106
}
107-
});
107+
})
108108
}
109109
110110
// LATER: reduce duplication with routes.go.ejs
111111
function query2dto(parser, method) {
112-
const query = removePlaceholders(method.query);
113-
const queryAst = parser.astify(query);
114-
const props = extractProperties(queryAst);
112+
const query = removePlaceholders(method.query)
113+
const queryAst = parser.astify(query)
114+
const props = extractProperties(queryAst)
115115
if (props.length === 0) {
116-
console.warn('Could not create DTO for query:', formatQuery(query));
117-
console.debug('Query AST:');
118-
console.debug(queryAst);
119-
return null;
116+
console.warn('Could not create DTO for query:', formatQuery(query))
117+
console.debug('Query AST:')
118+
console.debug(queryAst)
119+
return null
120120
}
121-
const fieldsInfo = method.dto && method.dto.fields ? method.dto.fields : {};
122-
const propsWithTypes = addTypes(props, fieldsInfo);
123-
const hasName = method.dto && method.dto.name && method.dto.name.length > 0;
124-
const name = hasName ? method.dto.name : "Dto" + ++globalDtoCounter;
121+
const fieldsInfo = method.dto && method.dto.fields ? method.dto.fields : {}
122+
const propsWithTypes = addTypes(props, fieldsInfo)
123+
const hasName = method.dto && method.dto.name && method.dto.name.length > 0
124+
const name = hasName ? method.dto.name : "Dto" + ++globalDtoCounter
125125
return {
126126
"name": name,
127127
"hasUserProvidedName": hasName,
@@ -130,34 +130,34 @@ function query2dto(parser, method) {
130130
// [ {name:foo, type:int}, {name:bar, type:string} ] => "foo=int bar=string"
131131
// LATER: sort before join
132132
"signature": propsWithTypes.map(field => `${field.name}=${field.type}`).join(' ')
133-
};
133+
}
134134
}
135135
136136
// https://fastapi.tiangolo.com/tutorial/body/
137137
function dto2model(dto) {
138-
let result = `class ${dto.name}(BaseModel):\n`;
138+
let result = `class ${dto.name}(BaseModel):\n`
139139
dto.props.forEach(prop => {
140140
result += ` ${prop.name}: Optional[${prop.type}] = None\n`
141-
});
142-
return result;
141+
})
142+
return result
143143
}
144144
145-
let globalDtoCounter = 0;
146-
const dtoCache = {};
145+
let globalDtoCounter = 0
146+
const dtoCache = {}
147147
148148
// LATER: reduce duplication with routes.go.ejs
149149
function cacheDto(dto) {
150-
dtoCache[dto.signature] = dto.name;
151-
return dto;
150+
dtoCache[dto.signature] = dto.name
151+
return dto
152152
}
153153
154154
// LATER: reduce duplication with routes.go.ejs
155155
function dtoInCache(dto) {
156156
// always prefer user specified name even when we have a similar DTO in cache
157157
if (dto.hasUserProvidedName) {
158-
return false;
158+
return false
159159
}
160-
return dtoCache.hasOwnProperty(dto.signature);
160+
return dtoCache.hasOwnProperty(dto.signature)
161161
}
162162
163163
// Generate models
@@ -173,8 +173,8 @@ endpoints.forEach(function(endpoint) {
173173
%>
174174
<%- model -%>
175175
<%
176-
});
177-
});
176+
})
177+
})
178178
179179
// Generate endpoints
180180
endpoints.forEach(function(endpoint) {
@@ -205,7 +205,7 @@ endpoints.forEach(function(endpoint) {
205205
queriesWithNames.forEach(queryWithName => {
206206
for (const [name, query] of Object.entries(queryWithName)) {
207207
const sql = convertToPsycopgNamedArguments(formatQueryForPython(query, 20))
208-
const params = extractParamsFromQuery(query);
208+
const params = extractParamsFromQuery(query)
209209
const formattedParams = formatParamsAsPythonDict(params)
210210
queries.push({ [name]: { sql : sql, formattedParams: formattedParams }})
211211
}
@@ -255,13 +255,13 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
255255
<%
256256
}
257257
if (method.name === 'post') {
258-
const dto = query2dto(sqlParser, method);
258+
const dto = query2dto(sqlParser, method)
259259
// LATER: do we really need signature and cache?
260-
const cacheKey = dto ? dto.signature : null;
261-
const model = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
260+
const cacheKey = dto ? dto.signature : null
261+
const model = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name
262262
263263
const sql = convertToPsycopgNamedArguments(formatQueryForPython(method.query, 20))
264-
const params = extractParamsFromQuery(method.query);
264+
const params = extractParamsFromQuery(method.query)
265265
const formattedParams = formatParamsAsPythonDict(params)
266266
267267
%>
@@ -280,15 +280,15 @@ def <%- pythonMethodName %>(body: <%- model %>, conn=Depends(db_connection)):
280280
}
281281
if (method.name === 'put') {
282282
// TODO: reduce duplication with POST
283-
const dto = query2dto(sqlParser, method);
283+
const dto = query2dto(sqlParser, method)
284284
// LATER: do we really need signature and cache?
285-
const cacheKey = dto ? dto.signature : null;
286-
const model = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
285+
const cacheKey = dto ? dto.signature : null
286+
const model = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name
287287
288288
const methodArgs = [ `body: ${model}`, ...argsFromPath, 'conn=Depends(db_connection)' ]
289289
290290
const sql = convertToPsycopgNamedArguments(formatQueryForPython(method.query, 20))
291-
const params = extractParamsFromQuery(method.query);
291+
const params = extractParamsFromQuery(method.query)
292292
const formattedParams = formatParamsAsPythonDict(params)
293293
%>
294294

0 commit comments

Comments
 (0)