Skip to content

Commit

Permalink
nice cats examples
Browse files Browse the repository at this point in the history
  • Loading branch information
szymon-rd committed Jan 17, 2024
1 parent 2d79d21 commit df1be54
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
7 changes: 6 additions & 1 deletion cats-interop/src/main/scala/lynx/catsinter/CatsMonadic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ type CatsResource[M[_], R] = CanReflect[Resource[M, Any]] ?=> R
extension [M[+_] : Monad, R](resource: Resource[M, R]^)
def useR(using M: MonadCancel[M, Throwable]): (R requires M)^{resource} = resource.use[R](a => M.pure(a))(M).reflect

type LIO[A] = A requires IO
type LIO[A] = A requires IO
type LEither[L, +R] = R requires ([A] =>> Either[L, A])
object LEither {
def left[L, R](l: L): LEither[L, R] = Lynx[[A] =>> Either[L, A]].reflect(Left(l))
def right[L, R](r: R): LEither[L, R] = Lynx[[A] =>> Either[L, A]].reflect(Right(r))
}
29 changes: 19 additions & 10 deletions cats-interop/src/test/scala/lynx/catsinter/CatsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.Path
import java.nio.file.NoSuchFileException
import java.rmi.ServerError

class CatsTest extends CatsEffectSuite {

Expand All @@ -28,26 +29,29 @@ class CatsTest extends CatsEffectSuite {
def toJson: String = s"""{"status": ${status}, "response": "${body}"}"""
}

trait HttpError
case object ServerError extends HttpError
case object ClientError extends HttpError

class Routes(handers: Map[String, Request => LIO[Response]]) {
def run(req: Request): LIO[Response] = {
class Routes(handers: Map[String, Request => LIO[LEither[HttpError, Response]]]) {
def run(req: Request): LIO[LEither[HttpError, Response]] = {
handers.get(req.path) match {
case Some(handler) => handler(req)
case None => IO.delayR(Response(404, "Not found"))
}
}
def handleGet(path: String)(handler: Request => LIO[Response]): Routes = {
def handleGet(path: String)(handler: Request => LIO[LEither[HttpError, Response]]): Routes = {
new Routes(handers + (path -> handler))
}
}
object Routes {
def apply(handers: (String, Request => LIO[Response])*): Routes = new Routes(handers.toMap)
def apply(handers: (String, Request => LIO[LEither[HttpError, Response]])*): Routes = new Routes(handers.toMap)
}


test("Async server") {

def makeRequest(url: String): LIO[Response] = IO.delayR(Response(200, "Hello from " + url))
def makeRequest(url: String): LIO[LEither[HttpError, Response]] = IO.delayR(Response(200, "Hello from " + url))

val routes = Routes()
.handleGet("/hello") { req =>
Expand All @@ -65,11 +69,16 @@ class CatsTest extends CatsEffectSuite {
Response(200, s"""{"responses": [${responses.map(_.toJson).mkString(",")}]}""")
}

Lynx[IO].reify(routes.run(Request("/hello", ""))).assertEquals(Response(200, "Hello"))
Lynx[IO].reify(routes.run(Request("/proxy", "http://google.com")))
.assertEquals(Response(200, """{"status": 200, "response": "Hello from http://google.com}""""))
Lynx[IO].reify(routes.run(Request("/proxyBatch", "http://google.com\nhttp://yandex.ru")))
.assertEquals(Response(200, """{"responses": [{"status": 200, "response": "Hello from http://google.com"},{"status": 200, "response": "Hello from http://yandex.ru"}]}"""))

def reify[A](program: LIO[LEither[HttpError, A]]): IO[Either[HttpError, A]] = {
type EitherHttp[A] = Either[HttpError, A]
Lynx[IO].reify(Lynx[EitherHttp].reify(program))
}
reify(routes.run(Request("/hello", ""))).assertEquals(Right(Response(200, "Hello")))
reify(routes.run(Request("/proxy", "http://google.com")))
.assertEquals(Right(Response(200, """{"status": 200, "response": "Hello from http://google.com}"""")))
reify(routes.run(Request("/proxyBatch", "http://google.com\nhttp://yandex.ru")))
.assertEquals(Right(Response(200, """{"responses": [{"status": 200, "response": "Hello from http://google.com"},{"status": 200, "response": "Hello from http://yandex.ru"}]}""")))
}


Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/lynx/syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ infix type requiresN[A, xs] = xs match {
extension [M[+_], R](mr: M[R])
inline def reflect(using r: CanReflect[M[Any]]): R = r.reflect(mr)
inline def r(using r: CanReflect[M[Any]]): R = r.reflect(mr)
inline def ?(using r: CanReflect[M[Any]]): R = r.reflect(mr)


inline def Lynx[M[+_]: Monadic]: LynxSyntax[M] = LynxSyntax()
Expand Down

0 comments on commit df1be54

Please sign in to comment.