@@ -148,45 +148,59 @@ var userAgent = navigator.userAgent
148
148
purePath = route.purePath.replace('{', '_').replace('}', '_')
149
149
150
150
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)
153
158
else:
154
159
newLit(60)
155
160
156
- var routeKey = fmt"{purePath}:pp ("
161
+ var routeKey = fmt"{purePath}("
157
162
for i in route.pathParams:
158
163
routeKey &= i.name & "={" & i.name & "}"
164
+ for i in route.requestModels:
165
+ routeKey &= i.name & "={" & i.name & ".repr}"
159
166
routeKey &= ")"
160
- echo routeKey
161
167
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]
190
204
191
205
let cachedRoutesResult = newNimNode(nnkDotExpr).add(
192
206
newNimNode(nnkBracketExpr).add(ident"cachedRoutes", ident"routeKey"), ident"res"
@@ -198,7 +212,6 @@ var userAgent = navigator.userAgent
198
212
statementList.insert(0, newStmtList(
199
213
newVarStmt(ident"routeKey", newCall("fmt", newLit(fmt"{routeKey}"))),
200
214
queryStmt,
201
- queryArrStmt,
202
215
newConstStmt(ident"thisRouteCanBeCached", newLit(true)),
203
216
newNimNode(nnkIfStmt).add(newNimNode(nnkElifBranch).add(
204
217
newCall("hasKey", ident"cachedRoutes", ident"routeKey"),
0 commit comments