diff --git a/modules/redis-it/src/test/scala/zio/redis/SortedSetsSpec.scala b/modules/redis-it/src/test/scala/zio/redis/SortedSetsSpec.scala index 118e0a123..1331b3412 100644 --- a/modules/redis-it/src/test/scala/zio/redis/SortedSetsSpec.scala +++ b/modules/redis-it/src/test/scala/zio/redis/SortedSetsSpec.scala @@ -183,7 +183,10 @@ trait SortedSetsSpec extends IntegrationSpec { key <- uuid _ <- redis.zAdd(key)(MemberScore("v1", 3d)) _ <- redis.zAdd(key)(MemberScore("v2", 4d)) - added <- redis.zAdd(key, update = Some(Update.SetLessThan))(MemberScore("v3", 1d), MemberScore("v1", 2d)) + added <- redis.zAdd(key, updateByScore = Some(UpdateByScore.SetLessThan))( + MemberScore("v3", 1d), + MemberScore("v1", 2d) + ) result <- redis.zRange(key, 0 to -1).returning[String] } yield assert(added)(equalTo(1L)) && assert(result.toList)(equalTo(List("v3", "v1", "v2"))) }, @@ -193,7 +196,10 @@ trait SortedSetsSpec extends IntegrationSpec { key <- uuid _ <- redis.zAdd(key)(MemberScore("v1", 1d)) _ <- redis.zAdd(key)(MemberScore("v2", 2d)) - added <- redis.zAdd(key, update = Some(Update.SetGreaterThan))(MemberScore("v3", 1d), MemberScore("v1", 3d)) + added <- redis.zAdd(key, updateByScore = Some(UpdateByScore.SetGreaterThan))( + MemberScore("v3", 1d), + MemberScore("v1", 3d) + ) result <- redis.zRange(key, 0 to -1).returning[String] } yield assert(added)(equalTo(1L)) && assert(result.toList)(equalTo(List("v3", "v2", "v1"))) }, @@ -203,7 +209,7 @@ trait SortedSetsSpec extends IntegrationSpec { key <- uuid _ <- redis.zAdd(key)(MemberScore("v1", 1d)) _ <- redis.zAdd(key)(MemberScore("v2", 2d)) - added <- redis.zAdd(key, update = Some(Update.SetGreaterThan), change = Some(Changed))( + added <- redis.zAdd(key, updateByScore = Some(UpdateByScore.SetGreaterThan), change = Some(Changed))( MemberScore("v3", 1d), MemberScore("v1", 3d) ) diff --git a/modules/redis/src/main/scala/zio/redis/Input.scala b/modules/redis/src/main/scala/zio/redis/Input.scala index 94aae8f7f..a16dffeae 100644 --- a/modules/redis/src/main/scala/zio/redis/Input.scala +++ b/modules/redis/src/main/scala/zio/redis/Input.scala @@ -584,6 +584,11 @@ object Input { RespCommand(RespCommandArgument.Value(data.asString)) } + case object UpdateByScoreInput extends Input[UpdateByScore] { + def encode(data: UpdateByScore): RespCommand = + RespCommand(RespCommandArgument.Value(data.asString)) + } + case object ValueInput extends Input[Chunk[Byte]] { def encode(data: Chunk[Byte]): RespCommand = RespCommand(RespCommandArgument.Value(data)) diff --git a/modules/redis/src/main/scala/zio/redis/api/SortedSets.scala b/modules/redis/src/main/scala/zio/redis/api/SortedSets.scala index 5c108a0be..bb61be8c7 100644 --- a/modules/redis/src/main/scala/zio/redis/api/SortedSets.scala +++ b/modules/redis/src/main/scala/zio/redis/api/SortedSets.scala @@ -122,21 +122,27 @@ trait SortedSets[G[+_]] extends RedisEnvironment[G] { * The number of elements added to the sorted set, not including elements already existing for which the score was * updated. */ - final def zAdd[K: Schema, M: Schema](key: K, update: Option[Update] = None, change: Option[Changed] = None)( + final def zAdd[K: Schema, M: Schema]( + key: K, + update: Option[Update] = None, + updateByScore: Option[UpdateByScore] = None, + change: Option[Changed] = None + )( memberScore: MemberScore[M], memberScores: MemberScore[M]* ): G[Long] = { val command = RedisCommand( ZAdd, - Tuple4( + Tuple5( ArbitraryKeyInput[K](), OptionalInput(UpdateInput), + OptionalInput(UpdateByScoreInput), OptionalInput(ChangedInput), NonEmptyList(MemberScoreInput[M]()) ), LongOutput ) - command.run((key, update, change, (memberScore, memberScores.toList))) + command.run((key, update, updateByScore, change, (memberScore, memberScores.toList))) } /** @@ -160,23 +166,29 @@ trait SortedSets[G[+_]] extends RedisEnvironment[G] { * The new score of member (a double precision floating point number), or None if the operation was aborted (when * called with either the XX or the NX option). */ - final def zAddWithIncr[K: Schema, M: Schema](key: K, update: Option[Update] = None, change: Option[Changed] = None)( + final def zAddWithIncr[K: Schema, M: Schema]( + key: K, + update: Option[Update] = None, + updateByScore: Option[UpdateByScore] = None, + change: Option[Changed] = None + )( increment: Increment, memberScore: MemberScore[M], memberScores: MemberScore[M]* ): G[Option[Double]] = { val command = RedisCommand( ZAdd, - Tuple5( + Tuple6( ArbitraryKeyInput[K](), OptionalInput(UpdateInput), + OptionalInput(UpdateByScoreInput), OptionalInput(ChangedInput), IncrementInput, NonEmptyList(MemberScoreInput[M]()) ), OptionalOutput(DoubleOutput) ) - command.run((key, update, change, increment, (memberScore, memberScores.toList))) + command.run((key, update, updateByScore, change, increment, (memberScore, memberScores.toList))) } /** diff --git a/modules/redis/src/main/scala/zio/redis/options/Shared.scala b/modules/redis/src/main/scala/zio/redis/options/Shared.scala index ed65b1a0e..c7aaef268 100644 --- a/modules/redis/src/main/scala/zio/redis/options/Shared.scala +++ b/modules/redis/src/main/scala/zio/redis/options/Shared.scala @@ -41,18 +41,27 @@ trait Shared { sealed trait Update { self => private[redis] final def asString: String = self match { - case Update.SetExisting => "XX" - case Update.SetNew => "NX" - case Update.SetLessThan => "LT" - case Update.SetGreaterThan => "GT" + case Update.SetExisting => "XX" + case Update.SetNew => "NX" } } object Update { - case object SetExisting extends Update - case object SetNew extends Update - case object SetLessThan extends Update - case object SetGreaterThan extends Update + case object SetExisting extends Update + case object SetNew extends Update + } + + sealed trait UpdateByScore { self => + private[redis] final def asString: String = + self match { + case UpdateByScore.SetLessThan => "LT" + case UpdateByScore.SetGreaterThan => "GT" + } + } + + object UpdateByScore { + case object SetLessThan extends UpdateByScore + case object SetGreaterThan extends UpdateByScore } } diff --git a/modules/redis/src/test/scala/zio/redis/InputSpec.scala b/modules/redis/src/test/scala/zio/redis/InputSpec.scala index 1f34fcde8..0e892e456 100644 --- a/modules/redis/src/test/scala/zio/redis/InputSpec.scala +++ b/modules/redis/src/test/scala/zio/redis/InputSpec.scala @@ -1022,6 +1022,18 @@ object InputSpec extends BaseSpec { } yield assert(result)(equalTo(RespCommand(Value("NX")))) } ), + suite("UpdateByScore")( + test("set greater than") { + for { + result <- ZIO.attempt(UpdateByScoreInput.encode(UpdateByScore.SetGreaterThan)) + } yield assert(result)(equalTo(RespCommand(Value("XX")))) + }, + test("set less than") { + for { + result <- ZIO.attempt(UpdateByScoreInput.encode(UpdateByScore.SetLessThan)) + } yield assert(result)(equalTo(RespCommand(Value("NX")))) + } + ), suite("Id")( test("valid value") { for {