Skip to content

Instantly share code, notes, and snippets.

View ghostdogpr's full-sized avatar

Pierre Ricadat ghostdogpr

View GitHub Profile
import java.nio.file.{ Files, Paths }
import zio.IO
def copyFile(path: String, destination: String): IO[Throwable, Unit] = IO.effect {
Files.copy(Paths.get(path), Paths.get(destination))
}
import scala.io.{ Codec, Source }
import zio.{ App, console, ZIO }
import zio.blocking._
object SampleApp extends App {
def getResource(path: String): ZIO[Blocking, Throwable, String] = effectBlocking {
Source.fromResource(path)(Codec.UTF8).getLines.mkString
}
def send(client: SqsAsyncClient, queueUrl: String, msg: String): Task[Unit] =
IO.effectAsync[Any, Throwable, Unit] { cb =>
client
.sendMessage(SendMessageRequest.builder.queueUrl(queueUrl).messageBody(msg).build)
.handle[Unit]((_, err) => {
err match {
case null => cb(IO.unit)
case ex => cb(IO.fail(ex))
}
})
def sendMsg(actor: ActorRef, msg: String): Task[String] =
IO.fromFuture { implicit ctx =>
(actor ? msg).mapTo[String]
}
Stream.fromEffect {
IO.effectAsync[Any, Throwable, List[Message]] { cb =>
client
.receiveMessage(ReceiveMessageRequest.builder.queueUrl(queueUrl).maxNumberOfMessages(10).build)
.handle[Unit]((result, err) => {
err match {
case null => cb(IO.succeed(result.messages.asScala.toList))
case ex => cb(IO.fail(ex))
}
})
def listen(actorSystem: ActorSystem, topic: String): Task[Queue[String]] =
for {
queue <- Queue.bounded[String](1000)
rts <- Task.runtime[Any]
_ <- Task(actorSystem.actorOf(Props(new SubscriberActor(topic, rts, queue))))
} yield queue
case class MessageEnvelope(msg: String)
class SubscriberActor(topic: String, rts: Runtime[Any], queue: Queue[String]) extends Actor {
import language.experimental.macros
import language.implicitConversions
import magnolia._
trait MyTypeclass[T] {
def f(t: T): String
}
object MyTypeclass {
type Typeclass[T] = MyTypeclass[T]
import magnolia.{ CaseClass, Magnolia, SealedTrait }
import mercator.Monadic
import zio.random.Random
import zio.test.{ Gen, Sized }
object Generators {
implicit val genUnit: Typeclass[Unit] = Gen.unit
implicit val genBool: Typeclass[Boolean] = Gen.boolean
implicit val genString: Typeclass[String] = Gen.small(Gen.stringN(_)(Gen.alphaNumericChar))
@ghostdogpr
ghostdogpr / caliban-client.scala
Last active February 8, 2020 00:06
Early draft for caliban-client. Feedback welcome!
// Step 1:
// Code generation creates helpers from a GraphQL schema
// Character.* and Queries.* here are auto-generated
// Step 2:
// Build your own queries combining those helpers
val character =
(Character.name ~
Character.nicknames ~
Character.origin).mapN(CharacterView)
import scala.deriving._
import scala.quoted._
import scala.quoted.matching._
import scala.compiletime._
trait Show[T] {
def show(x: T): String
}
object Show {