nim-lang / redis

Official redis wrapper for Nim.
Other
128 stars 35 forks source link

How to use hGetAll #31

Closed mitjafelicijan closed 2 years ago

mitjafelicijan commented 2 years ago

If I populate with setk and then do let info = rds.get("somekey") this works ok.

But if these key is populated with hMSet (which works) and then I try to get data from with let info = rds.hGetAll("somekey") the whole thing fails with the following error.

Error: type mismatch: got <Prologue, string, proc (ctx: Context): Future[system.void]{.locks: <unknown>.}, seq[HttpMethod]>
but expected one of:
proc addRoute(app: Prologue; patterns: openArray[UrlPattern]; baseRoute = "";
              middlewares: Option[seq[HandlerAsync]] = none(seq[HandlerAsync]))
  first type mismatch at position: 2
  required type for patterns: openArray[UrlPattern]
  but expression '"/db/{databaseId}"' is of type: string
proc addRoute(app: Prologue; route: Regex; handler: HandlerAsync;
              httpMethod = HttpGet; middlewares: openArray[HandlerAsync] = @[])
  first type mismatch at position: 2
  required type for route: Regex
  but expression '"/db/{databaseId}"' is of type: string
proc addRoute(app: Prologue; route: Regex; handler: HandlerAsync;
              httpMethod: openArray[HttpMethod];
              middlewares: openArray[HandlerAsync] = @[])
  first type mismatch at position: 2
  required type for route: Regex
  but expression '"/db/{databaseId}"' is of type: string
proc addRoute(app: Prologue; route: string; handler: HandlerAsync;
              httpMethod = HttpGet; name = "";
              middlewares: openArray[HandlerAsync] = @[])
  first type mismatch at position: 3
  required type for handler: HandlerAsync
  but expression 'databaseInfo' is of type: proc (ctx: Context): Future[system.void]{.locks: <unknown>.}
  Pragma mismatch: got '{..}', but expected '{.gcsafe.}'.
  This expression is not GC-safe. Annotate the proc with {.gcsafe.} to get extended error information.
proc addRoute(app: Prologue; route: string; handler: HandlerAsync;
              httpMethod: openArray[HttpMethod]; name = "";
              middlewares: openArray[HandlerAsync] = @[])
  first type mismatch at position: 3
  required type for handler: HandlerAsync
  but expression 'databaseInfo' is of type: proc (ctx: Context): Future[system.void]{.locks: <unknown>.}
  Pragma mismatch: got '{..}', but expected '{.gcsafe.}'.
  This expression is not GC-safe. Annotate the proc with {.gcsafe.} to get extended error information.
1 other mismatching symbols have been suppressed; compile with --showAllMismatches:on to see them

expression: addRoute(app, "/db/{databaseId}", databaseInfo, @[HttpGet])
ThomasTJdev commented 2 years ago

What happens with:

{.gcsafe.}:
  let info = rds.hGetAll("somekey")
mitjafelicijan commented 2 years ago

If I do it like you suggested, it works. Is there a way to use this globally, or is this not recommended?

image

ThomasTJdev commented 2 years ago

Some procedures in this library is not per se marked as gcsafe. See the PR #25.

When using {.gcsafe.} you manually tell the compiler, that the code is safe - so you gotta be sure, that it is safe!

To override the compiler's gcsafety analysis a {.gcsafe.} pragma block can be used

https://nim-lang.org/docs/manual.html#effect-system-gc-safety-effect

You can mark a whole procedure with:

proc redisproc() {.gcsafe.} =
  xxxx
mitjafelicijan commented 2 years ago

Can I use {.gcsafe} with {.async}?

If I try this proc databaseInfo*(ctx: context.Context) {.gcsafe.} {.async.} = I get indentation error.

mitjafelicijan commented 2 years ago

It works like this

image

Is this the only way of using it in my case?

ThomasTJdev commented 2 years ago

You can combine multiple pragmas. Just separate them with ,.

proc databaseInfo*(ctx: context.Context) {.async,gcsafe.} =
  xxx