Skip to content

Commit 8719f56

Browse files
committed
feat: add request models support into @cached decorator
1 parent 8ff5944 commit 8719f56

File tree

2 files changed

+52
-34
lines changed

2 files changed

+52
-34
lines changed

src/happyx/routing/decorators.nim

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -148,45 +148,59 @@ var userAgent = navigator.userAgent
148148
purePath = route.purePath.replace('{', '_').replace('}', '_')
149149
150150
let expiresIn =
151-
if arguments.len == 1:
152-
arguments[0]
151+
if arguments.len == 1 and arguments[0].kind in {nnkIntLit, nnkInt16Lit, nnkInt32Lit, nnkInt64Lit, nnkInt8Lit}:
152+
newLit(arguments[0].intVal.int)
153+
elif arguments.len == 1 and arguments[0].kind == nnkExprEqExpr and arguments[0][0] == ident"expires":
154+
if arguments[0][1].kind in {nnkIntLit, nnkInt16Lit, nnkInt32Lit, nnkInt64Lit, nnkInt8Lit}:
155+
newLit(arguments[0][1].intVal.int)
156+
else:
157+
newLit(60)
153158
else:
154159
newLit(60)
155160
156-
var routeKey = fmt"{purePath}:pp("
161+
var routeKey = fmt"{purePath}("
157162
for i in route.pathParams:
158163
routeKey &= i.name & "={" & i.name & "}"
164+
for i in route.requestModels:
165+
routeKey &= i.name & "={" & i.name & ".repr}"
159166
routeKey &= ")"
160-
echo routeKey
161167
162-
let
163-
queryStmt = newStmtList()
164-
queryArrStmt = newStmtList()
165-
166-
if statementList.isIdentUsed(ident"query"):
167-
var usages = statementList.getIdentUses(ident"query")
168-
for i in usages:
169-
if i.kind == nnkInfix and i[0] == ident"?" and i[1] == ident"query" and i[2].kind == nnkIdent:
170-
queryStmt.add parseStmt(fmt"""routeKey &= "{i[2]}" & "=" & query.getOrDefault("{i[2]}", "")""")
171-
elif i.kind == nnkBracketExpr and i[0] == ident"query" and i[1].kind == nnkStrLit:
172-
queryStmt.add parseStmt(fmt"""routeKey &= "{i[1].strVal}" & "=" & query.getOrDefault("{i[1].strVal}", "")""")
173-
elif i.kind == nnkBracketExpr and i[0] == ident"query":
174-
queryStmt.add parseStmt(fmt"""routeKey &= {i[1].toStrLit} & "=" & query.getOrDefault({i[1].toStrLit}, "")""")
175-
else:
176-
discard
177-
# echo i.treeRepr
178-
if statementList.isIdentUsed(ident"queryArr"):
179-
var usages = statementList.getIdentUses(ident"queryArr")
180-
for i in usages:
181-
if i.kind == nnkInfix and i[0] == ident"?" and i[1] == ident"queryArr" and i[2].kind == nnkIdent:
182-
queryStmt.add parseStmt(fmt"""routeKey &= "{i[2]}" & "=" & $queryArr.getOrDefault("{i[2]}", "")""")
183-
elif i.kind == nnkBracketExpr and i[0] == ident"queryArr" and i[1].kind == nnkStrLit:
184-
queryStmt.add parseStmt(fmt"""routeKey &= "{i[1].strVal}" & "=" & $queryArr.getOrDefault("{i[1].strVal}", "")""")
185-
elif i.kind == nnkBracketExpr and i[0] == ident"queryArr":
186-
queryStmt.add parseStmt(fmt"""routeKey &= {i[1].toStrLit} & "=" & $queryArr.getOrDefault({i[1].toStrLit}, "")""")
187-
else:
188-
discard
189-
# echo i.treeRepr
168+
let queryStmt = newStmtList()
169+
var usedVariables: seq[NimNode] = @[]
170+
171+
for identName in ["query", "queryArr"]:
172+
let idnt = ident(identName)
173+
if statementList.isIdentUsed(idnt):
174+
var usages = statementList.getIdentUses(idnt)
175+
for i in usages:
176+
# query?KEY
177+
if i.kind == nnkInfix and i[0] == ident"?" and i[1] == idnt and i[2].kind == nnkIdent:
178+
if i[2] notin usedVariables:
179+
queryStmt.add parseStmt(
180+
fmt"""routeKey &= "{i[2]}" & "=" & {identName}.getOrDefault("{i[2]}", "")"""
181+
)
182+
usedVariables.add i[2]
183+
# query["KEY"]
184+
elif i.kind == nnkBracketExpr and i[0] == idnt and i[1].kind == nnkStrLit:
185+
if i[1] notin usedVariables:
186+
queryStmt.add parseStmt(
187+
fmt"""routeKey &= "{i[1].strVal}" & "=" & {identName}.getOrDefault("{i[1].strVal}", "")"""
188+
)
189+
usedVariables.add i[1]
190+
# query[KEY]
191+
elif i.kind == nnkBracketExpr and i[0] == idnt:
192+
if i[1] notin usedVariables:
193+
queryStmt.add parseStmt(
194+
fmt"""routeKey &= {i[1].toStrLit} & "=" & {identName}.getOrDefault({i[1].toStrLit}, "")"""
195+
)
196+
usedVariables.add i[1]
197+
# hasKey(query, KEY)
198+
elif i.kind == nnkCall and i[0] == ident"hasKey" and i[1] == idnt and i.len == 3:
199+
if i[2] notin usedVariables:
200+
queryStmt.add parseStmt(
201+
fmt"""routeKey &= {i[2].toStrLit} & "=" & {identName}.getOrDefault({i[2].toStrLit}, "")"""
202+
)
203+
usedVariables.add i[2]
190204
191205
let cachedRoutesResult = newNimNode(nnkDotExpr).add(
192206
newNimNode(nnkBracketExpr).add(ident"cachedRoutes", ident"routeKey"), ident"res"
@@ -198,7 +212,6 @@ var userAgent = navigator.userAgent
198212
statementList.insert(0, newStmtList(
199213
newVarStmt(ident"routeKey", newCall("fmt", newLit(fmt"{routeKey}"))),
200214
queryStmt,
201-
queryArrStmt,
202215
newConstStmt(ident"thisRouteCanBeCached", newLit(true)),
203216
newNimNode(nnkIfStmt).add(newNimNode(nnkElifBranch).add(
204217
newCall("hasKey", ident"cachedRoutes", ident"routeKey"),

tests/testc16.nim

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,15 @@ serve "127.0.0.1", 5000:
116116
@Cached(120) # Expires in 60 seconds by default
117117
get "/cached/{x}":
118118
await sleepAsync(1000)
119-
if query.hasKey("key"):
119+
if hasKey(query, "key"):
120120
return query["key"]
121121
await sleepAsync(1000)
122122
return x
123+
124+
@Cached(expires = 120) # Expires in 60 seconds by default
125+
post "/cached/[m:TestModel]":
126+
await sleepAsync(1000)
127+
return m.username
123128

124129
@AuthBasic
125130
post "/test/basic-auth":

0 commit comments

Comments
 (0)