Skip to content

Commit

Permalink
Merge pull request #37 from davenverse/prettyResp
Browse files Browse the repository at this point in the history
Resp Printers
  • Loading branch information
ChristopherDavenport authored Dec 24, 2021
2 parents 04fce65 + 246fe86 commit 05f2077
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ object RedisConnection{
runRequest(connection)(input, key).flatMap{
case Right(a) => a.pure[F]
case Left(e@Resp.Error(_)) => ApplicativeError[F, Throwable].raiseError[A](e)
case Left(other) => ApplicativeError[F, Throwable].raiseError[A](RedisError.Generic(s"Rediculous: Incompatible Return Type for Operation: ${input.head}, got: $other"))
case Left(other) => ApplicativeError[F, Throwable].raiseError[A](RedisError.Generic(s"Rediculous: Incompatible Return Type for Operation: ${input.head}, got:\n${Resp.toStringRedisCLI(other)}"))
}
})

private[rediculous] def closeReturn[F[_]: MonadThrow, A](fE: Either[Resp, A]): F[A] =
fE match {
case Right(a) => a.pure[F]
case Left(e@Resp.Error(_)) => ApplicativeError[F, Throwable].raiseError[A](e)
case Left(other) => ApplicativeError[F, Throwable].raiseError[A](RedisError.Generic(s"Rediculous: Incompatible Return Type: Got $other"))
case Left(other) => ApplicativeError[F, Throwable].raiseError[A](RedisError.Generic(s"Rediculous: Incompatible Return Type, got\n${Resp.toStringRedisCLI(other)}"))
}

object Defaults {
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/scala/io/chrisdavenport/rediculous/Resp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cats.data.NonEmptyList
import cats.implicits._
import scala.util.control.NonFatal
import java.nio.charset.StandardCharsets
import java.nio.charset.Charset

sealed trait Resp

Expand Down Expand Up @@ -51,6 +52,25 @@ object Resp {
}
}

def toStringProtocol(resp: Resp)(implicit C: Charset = StandardCharsets.UTF_8) =
new String(encode(resp), C)

def toStringRedisCLI(resp: Resp, depth: Int = 0): String = resp match {
case BulkString(Some(value)) => s""""$value""""
case BulkString(None) => "(empty bulk string)"
case SimpleString(value) => s""""$value""""
case Integer(long) => s"(integer) $long"
case Error(value) => s"(error) $value"
case Array(None) => "(empty array)"
case Array(Some(a)) =>
a.zipWithIndex.map{ case (a, i) => (a, i + 1)}
.map{ case (resp, i) =>
val whitespace = if (i > 1) List.fill(depth * 3)(" ").mkString else ""
whitespace ++ s"$i) ${toStringRedisCLI(resp, depth + 1)}"
}.mkString("\n")

}

def parseAll(arr: SArray[Byte]): RespParserResult[List[Resp]] = { // TODO Investigate Performance Benchmarks with Chain
val listBuffer = new mutable.ListBuffer[Resp]
def loop(arr: SArray[Byte]): RespParserResult[List[Resp]] = {
Expand Down
63 changes: 43 additions & 20 deletions examples/src/main/scala/internal/ATestApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,50 @@ import scala.concurrent.duration._
* Test App For Development Purposes
**/
object ATestApp extends IOApp {
val all = "__keyspace*__:*"
val foo = "foo"

import io.chrisdavenport.rediculous.Resp._
def run(args: List[String]): IO[ExitCode] = {
RedisConnection.queued[IO].withHost(host"localhost").withPort(port"6379").withMaxQueued(10000).withWorkers(workers = 1).build.flatMap{
connection =>
RedisPubSub.fromConnection(connection, 4096).map(alg => (connection, alg))
}.use{ case(conn, alg) =>
alg.nonMessages({r => IO.println(s"other: $r")}) >>
alg.unhandledMessages({r => IO.println(s"unhandled: $r")}) >>
alg.psubscribe(all, {r => IO.println("p: " + r.toString())}) >>
alg.subscribe(foo, {r => IO.println("s: " + r.toString())}) >> {
(
alg.runMessages,
alg.publish(foo, "Baz"),
Temporal[IO].sleep(10.seconds) >>
alg.subscriptions.flatTap(IO.println(_)) >>
alg.psubscriptions.flatTap(IO.println(_))
).parMapN{ case (_, _, _) => ()}
}
// RedisConnection.queued[IO].withHost(host"localhost").withPort(port"6379").withMaxQueued(10000).withWorkers(workers = 1).build.flatMap{
// connection =>
// RedisPubSub.fromConnection(connection, 4096).map(alg => (connection, alg))
// }.use{ case(conn, alg) =>
// alg.nonMessages({r => IO.println(s"other: $r")}) >>
// alg.unhandledMessages({r => IO.println(s"unhandled: $r")}) >>
// alg.psubscribe(all, {r => IO.println("p: " + r.toString())}) >>
// alg.subscribe(foo, {r => IO.println("s: " + r.toString())}) >> {
// (
// alg.runMessages,
// alg.publish(foo, "Baz"),
// Temporal[IO].sleep(10.seconds) >>
// alg.subscriptions.flatTap(IO.println(_)) >>
// alg.psubscriptions.flatTap(IO.println(_))
// ).parMapN{ case (_, _, _) => ()}
// }
val resp = Array(Some(
List(
Array(Some(
List(
BulkString(Some("mystream")),
Array(Some(
List(
Array(Some(
List(
BulkString(Some("1639792169819-0")),
Array(Some(
List(
BulkString(Some("sensor-id")), BulkString(Some("1234")),
BulkString(Some("temperature")), BulkString(Some("12")),
BulkString(Some("ba")), BulkString(Some("123"))
)
))
)
))
)
))
)
))
)
))
IO.println(Resp.toStringRedisCLI(resp))
}.as(ExitCode.Success)
// val r = for {
// connection <- RedisConnection.pool[IO].withHost(host"localhost").withPort(port"30001").build
Expand All @@ -42,6 +66,5 @@ object ATestApp extends IOApp {
// } >>
// IO.pure(ExitCode.Success)

}

}

0 comments on commit 05f2077

Please sign in to comment.