Skip to content

Incorrect Lua function signature in set_then_get examples #301

Description

@Aryex

Page

src/content/docs/how-to/load-and-execute-functions.mdx (Load and Execute Functions)

Problem

The set_then_get Lua example registers the function with named parameters:

#!lua name=example_module
server.register_function('set_then_get', function(key, value)
   server.call('SET', key, value)
   return server.call('GET', key)
end)

Valkey Functions always invoke a registered function with exactly two
arguments — keys (array) and args (array). So key binds to the whole
keys array and value to the whole args array (both Lua tables), not
strings. When called as fcall("set_then_get", keys: ["page:home:visits"], args: ["1"]), the function runs server.call('SET', <table>, <table>) and
the server rejects it:

ResponseError: Command arguments must be strings or integers

The client-side fcall/function_load calls are correct — the bug is only
in the Lua function body.

Scope

This same broken signature appears in all language tabs on the page
(Python, Java, Node, Go, PHP, C#, Ruby). The other example on the page,
update_visits, uses the correct array-indexing style and works.

Suggested fix

Use the (keys, args) signature and index into the arrays, across every
language tab:

#!lua name=example_module
server.register_function('set_then_get', function(keys, args)
   server.call('SET', keys[1], args[1])
   return server.call('GET', keys[1])
end)

How it was found

Caught while verifying Ruby doc examples against the published
valkey-glide-rb 1.0.0.pre.rc2 gem with a live Valkey server (PRs #299/#300).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdocumentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions