Skip to content

Commit

Permalink
feat(python): specify variable type for query parameters
Browse files Browse the repository at this point in the history
Part of #53
  • Loading branch information
php-coder committed Apr 30, 2024
1 parent 940643f commit ccb5c76
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/python/fastapi/postgres/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def post_v1_categories(body: CreateCategoryDto, conn=Depends(db_connection)):


@router.get('/v1/categories/search')
def get_list_v1_categories_search(hidden, conn=Depends(db_connection)):
def get_list_v1_categories_search(hidden: bool, conn=Depends(db_connection)):
try:
with conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
Expand Down
15 changes: 14 additions & 1 deletion src/templates/routes.py.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ function findOutType(fieldsInfo, fieldName) {
return 'str'
}
// "q.title" => "q.title: str"
// "q.active" => "q.active: bool"
// "q.age" => "q.age: int"
// "p.id" => "p.id"
// "b.name" => "b.name"
function appendVariableTypeToQueryParam(paramsInfo, varName) {
if (varName.startsWith('q.')) {
return `${varName}: ${findOutType(paramsInfo, stipOurPrefixes(varName))}`
}
return varName
}
// LATER: reduce duplication with routes.go.ejs
function addTypes(props, fieldsInfo) {
return props.map(prop => {
Expand Down Expand Up @@ -187,7 +199,8 @@ endpoints.forEach(function(endpoint) {
const pythonMethodName = generate_method_name(method.name, path)
// LATER: add support for aggregated_queries (#17)
const argsFromQuery = method.query ? extractParamsFromQuery(method.query).map(stipOurPrefixes) : []
const queryParamsInfo = method.params && method.params.query ? method.params.query : {}
const argsFromQuery = method.query ? extractParamsFromQuery(method.query).map(param => appendVariableTypeToQueryParam(queryParamsInfo, param)).map(stipOurPrefixes) : []
// define before "if", to make them available later
let methodArgs
Expand Down

0 comments on commit ccb5c76

Please sign in to comment.