Lua String equality check in eval

837 views
Skip to first unread message

Raghava Mutharaju

unread,
Aug 28, 2014, 10:42:17 AM8/28/14
to redi...@googlegroups.com
Hello All,

I am trying to do a simple string equality comparison in the lua script, but it doesn't work.

local b = redis.call('TYPE', value) 
if(b == 'zset') then ...... else ....... end

This doesn't work. I checked the value of the comparison (==) and it always returns null.

I also tried string.find(b, 'zset') and I got an exception that find expects a string but found table. Redis command 'TYPE' returns a string representation.

How can I do an equality comparison of strings in lua?

Thank you.

Regards,
Raghava.

Jan-Erik Rediger

unread,
Aug 28, 2014, 11:11:40 AM8/28/14
to redi...@googlegroups.com
Hey Raghava,

in fact b is not the string "zset", that's why you're if check fails.

If you look into that type the return value actually is you should get
the idea how to access the value correctly.

127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> eval 'local t = redis.call("type", "foo"); return type(t)' 0
"table"
127.0.0.1:6379> eval 'local t = redis.call("type", "foo"); return
cjson.encode(t)' 0
"{\"ok\":\"string\"}"


As you can see by encoding it as json, it is actually a hashmap, so you
need to acces the "ok" field.

127.0.0.1:6379> eval 'local t = redis.call("type", "foo"); return t.ok
== "string"' 0
(integer) 1

If you do your check on "b.ok" it works as expected.
> --
> You received this message because you are subscribed to the Google Groups "Redis DB" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
> To post to this group, send email to redi...@googlegroups.com.
> Visit this group at http://groups.google.com/group/redis-db.
> For more options, visit https://groups.google.com/d/optout.

Raghava Mutharaju

unread,
Aug 28, 2014, 11:22:48 AM8/28/14
to redi...@googlegroups.com, Jan-Erik Rediger
Yes, that works. Thank you.

I thought type command would return a string as mentioned at http://redis.io/commands/type.

Thanks again.

Regards,
Raghava.

Matt Stancliff

unread,
Aug 28, 2014, 11:45:58 AM8/28/14
to redi...@googlegroups.com

On Aug 28, 2014, at 11:22 AM, Raghava Mutharaju <m.vijay...@gmail.com> wrote:

> I thought type command would return a string as mentioned at http://redis.io/commands/type.

Yeah, it is slightly unclear. Somewhere along the way, “Status Reply” got renamed to “Simple String Reply.” The Redis Lua integration still treats them as status replies, so Redis wants to tell you the status is okay by returning {“ok”: value}. Though, a status reply can only ever be “ok” making the entire wrapper slightly redundant. Error replies get returned as {“err”: value}. It would be nice to change Simple String Replies to just return a bare string, but it would break existing scripts, so it should be done with a notable backwards compatability breaking version change.


-Matt

Raghava Mutharaju

unread,
Aug 28, 2014, 12:00:15 PM8/28/14
to redi...@googlegroups.com
Ok. Thank you. I understand now.

Regards,
Raghava.


Reply all
Reply to author
Forward
0 new messages