Conversion between Lua and Redis data types

Can someone explain with examples the conversion between Lua and Redis data types?

Regards

You should review the EVAL docs https://redis.io/commands/eval

i will suggest you go through the documentation from redis
https://redis.io/topics/data-types-intro

@alberto2020
Here is how documentation explain Conversion between Lua and Redis data types

  1. EVAL and EVALSHA are used to evaluate scripts using the Lua interpreter built into Redis starting from version 2.6.0.
  2. Redis return values are converted into Lua data types when Lua calls a Redis command using call() or pcall() . Similarly, Lua data types are converted into the Redis protocol when calling a Redis command and when a Lua script returns a value, so that scripts can control what EVAL will return to the client.
  3. This conversion between data types is designed in a way that if a Redis type is converted into a Lua type, and then the result is converted back into a Redis type, the result is the same as the initial value.
  4. Redis to Lua conversion table.
  • Redis integer reply -> Lua number
  • Redis bulk reply -> Lua string
  • Redis multi bulk reply -> Lua table (may have other Redis data types nested)
  • Redis status reply -> Lua table with a single ok field containing the status
  • Redis error reply -> Lua table with a single err field containing the error
  • Redis Nil bulk reply and Nil multi bulk reply -> Lua false boolean type
  1. Lua to Redis conversion table.
  • Lua number -> Redis integer reply (the number is converted into an integer)
  • Lua string -> Redis bulk reply
  • Lua table (array) -> Redis multi bulk reply (truncated to the first nil inside the Lua array if any)
  • Lua table with a single ok field -> Redis status reply
  • Lua table with a single err field -> Redis error reply
  • Lua boolean false -> Redis Nil bulk reply.
  1. There is an additional Lua-to-Redis conversion rule that has no corresponding Redis to Lua conversion rule:
  • Lua boolean true -> Redis integer reply with value of 1
  1. Lastly, there are three important rules to note:
  • Lua has a single numerical type, Lua numbers. There is no distinction between integers and floats. So we always convert Lua numbers into integer replies, removing the decimal part of the number if any. If you want to return a float from Lua you should return it as a string , exactly like Redis itself does (see for instance the ZSCORE command).
  • There is no simple way to have nils inside Lua arrays, this is a result of Lua table semantics, so when Redis converts a Lua array into Redis protocol the conversion is stopped if a nil is encountered.
  • When a Lua table contains keys (and their values), the converted Redis reply will not include them.

Hope this helps

1 Like