Skip to content

Commit

Permalink
Merge pull request #215 from debasishg/fix-eval
Browse files Browse the repository at this point in the history
Script evaluation fix
  • Loading branch information
debasishg authored Sep 15, 2018
2 parents ec76dca + 44df75b commit 94e503a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ lazy val redisClient = (project in file(".")).settings(coreSettings : _*)
lazy val commonSettings: Seq[Setting[_]] = Seq(
organization := "net.debasishg",
version := "3.7",
scalaVersion := "2.11.12",
scalaVersion := "2.12.6",
crossScalaVersions := Seq("2.12.6", "2.11.12", "2.10.7", "2.13.0-M4"),

scalacOptions in Compile ++= Seq( "-unchecked", "-feature", "-language:postfixOps", "-deprecation" ),
Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/com/redis/EvalOperations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ trait EvalOperations { self: Redis =>

// EVAL
// evaluates lua code on the server.
def evalMultiBulk[A](luaCode: String, keys: List[Any], args: List[Any])(implicit format: Format, parse: Parse[A]): Option[List[Option[A]]] =
def evalMultiBulk[A](luaCode: String, keys: List[Any], args: List[Any])(implicit format: Format, parse: Parse[A]): Option[List[Option[A]]] =
send("EVAL", argsForEval(luaCode, keys, args))(asList[A])

def evalBulk[A](luaCode: String, keys: List[Any], args: List[Any])(implicit format: Format, parse: Parse[A]): Option[A] =
send("EVAL", argsForEval(luaCode, keys, args))(asBulk)

def evalInt(luaCode: String, keys: List[Any], args: List[Any]): Option[Int] =
send("EVAL", argsForEval(luaCode, keys, args))(asInt)

def evalMultiSHA[A](shahash: String, keys: List[Any], args: List[Any])(implicit format: Format, parse: Parse[A]): Option[List[Option[A]]] =
send("EVALSHA", argsForEval(shahash, keys, args))(asList[A])

Expand Down
21 changes: 21 additions & 0 deletions src/test/scala/com/redis/EvalOperationsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,26 @@ class EvalOperationsSpec extends FunSpec

r.scriptExists(shahash.get) should equal (Some(0))
}

it("should do stuff") {
r.lpush("content", "{\"source\": \"output1.txt\", \"col1\": \"water_pressure\", \"col2\": \"sunday\", \"col3\": \"december\"}")
r.lpush("content", "{\"source\": \"output1.txt\", \"col1\": \"air_pressure\", \"col2\": \"saturday\", \"col3\": \"november\"}")
r.lpush("content", "{\"source\": \"output2.txt\", \"col1\": \"air_pressure\", \"col2\": \"saturday\", \"col3\": \"november\"}")

val luaCode = """
if redis.call("EXISTS", KEYS[1]) == 1 then
local res = {}
local payload = redis.call("LRANGE", KEYS[1], 0, -1)
local row = cjson.decode(payload[1])
res[1] = row["source"]
return #res
else
return -1
end
"""

val res = r.evalInt(luaCode, List("content"), List())
res should equal (Some(1))
}
}
}

0 comments on commit 94e503a

Please sign in to comment.