redis/go-redis

Returning false from a lua script causes an error

eparker-tulip opened this issue · 1 comments

Expected Behavior

script.Run().Bool() should be able to return true or false without an error.

Current Behavior

Returning false from a lua script causes a redis: nil error also to be returned. It is not possible to return false without an error. An error occurs even if .Result() is used instead of .Bool().

Steps to Reproduce

package main

import (
	"context"
	"fmt"

	"github.com/redis/go-redis/v9"
)

func main() {
	ctx := context.Background()

	rdb := redis.NewClient(&redis.Options{
		Addr: ":6379",
	})
	_ = rdb.FlushDB(ctx).Err()

	res, err := boolTest.Run(ctx, rdb, []string{"true"}).Bool()
	fmt.Printf("Result (true): %v\n Error: %v\n", res, err)
	res, err = boolTest.Run(ctx, rdb, []string{"false"}).Bool()
	fmt.Printf("Result (false): %v\n Error: %v\n", res, err)
}

var boolTest = redis.NewScript(`
	local arg = KEYS[1]
	if arg == "false" then
		return false
	else
		return true
	end 
`)

OUTPUT

% go run .
   Result (true): true
   Error: <nil>
   Result (false): false
   Error: redis: nil

Context (Environment)

  • go-redis version v9.7.0
  • go version 1.22.2

Ok, I was just pointed to this documentation which I hadn't found earlier: https://redis.uptrace.dev/guide/lua-scripting.html#lua-and-go-types

This confirms that returning false is converted to an error. This is an unexpected and odd design -- but at least it's documented.