Skip to content

Instantly share code, notes, and snippets.

@Daenyth
Daenyth / AkkaHttp4s.scala
Created May 18, 2020 18:29
POC akka-http to http4s layer
package akkahttp4s
import akka.http.scaladsl.model.{
ContentType,
ContentTypes,
HttpEntity,
HttpHeader,
HttpMethod,
HttpMethods,
HttpProtocol,
@Daenyth
Daenyth / ModDate.scala
Created March 5, 2020 17:54
Shard `Long` values across N days
import cats._
import cats.implicits._
import cats.effect._
import scala.concurrent.duration._
object ModDate {
def epochDay[F[_]: Clock: Functor]: F[Long] =
Clock[F]
.realTime(SECONDS)
.map(FiniteDuration(_, SECONDS).toDays)
import cats.implicits._
import cats.data.NonEmptyChain
import scala.xml.{Elem, Node}
object ScalaXmlUtils {
type XmlResult[A] = Either[NonEmptyChain[String], A]
/** Get the `<tagLabel>` child of `node` or fail */
def get(node: Node, tagLabel: String): XmlResult[Node] =
getOpt(node, tagLabel)
@Daenyth
Daenyth / IOAssertions.scala
Last active December 17, 2023 18:16
Cats-effect IOSpec for scalatest / TestContext usage
import cats.Eq
import cats.effect.{ContextShift, IO, Timer}
import org.scalactic.Prettifier
import org.scalactic.source.Position
import org.scalatest.exceptions.TestFailedException
import org.scalatest.{Assertion, AsyncTestSuite}
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.reflect.ClassTag
@Daenyth
Daenyth / KeyedEnqueue.scala
Created October 9, 2019 14:22
fs2 groupBy / KeyedEnqueue
import cats.Monad
import cats.effect.concurrent.{Ref, Semaphore}
import cats.effect.{Concurrent, Resource}
import cats.implicits._
import fs2.{Pipe, Stream}
import fs2.concurrent.{NoneTerminatedQueue, Queue}
/** Represents the ability to enqueue keyed items into a stream of queues that emits homogenous keyed streams.
*
@Daenyth
Daenyth / PauseButton.scala
Created September 27, 2019 18:40
PauseButton for cats-effect / fs2
import java.time.Instant
import cats.effect.concurrent.Deferred
import cats.effect.implicits._
import cats.effect.{Concurrent, Timer, Clock}
import cats.implicits._
import fs2.concurrent.{Signal, SignallingRef}
import scala.concurrent.duration._
@Daenyth
Daenyth / S3Api.scala
Last active May 12, 2023 12:51
fs2 S3 Multipart Upload and download [OBSOLETE, use fs2-aws instead]
// Copyright 2019-2021 github.com/daenyth
// Under the MIT license
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNE
@Daenyth
Daenyth / TableCopy.scala
Created June 5, 2019 14:22
Doobie postgres table copy
import cats.implicits._
import cats._
import cats.effect.implicits._
import cats.effect._
import doobie._
import doobie.implicits._
import doobie.postgres._
import java.io.{InputStream, OutputStream, PipedInputStream, PipedOutputStream}
import fs2.Stream
@Daenyth
Daenyth / Fs2Timeout.scala
Last active June 4, 2019 19:56
Resettable fs2 stream timeout
import cats.effect._
import cats.effect.implicits._
import cats.implicits._
import cats.effect.concurrent._
import fs2._
object Fs2Timeout {
def stopWhenIdle[F[_]: Timer, A](
duration: FiniteDuration
)(
@Daenyth
Daenyth / AlpakkaS3Put.scala
Created May 23, 2019 17:31
AlpakkaS3Put for fs2
import akka.http.scaladsl.model.{ContentType, ContentTypes}
import akka.stream.Materializer
import akka.stream.alpakka.s3.scaladsl.{S3 => AlpakkaS3}
import akka.stream.alpakka.s3.{MetaHeaders, MultipartUploadResult}
import akka.util.ByteString
import cats.effect.{ConcurrentEffect, ContextShift}
import fs2.{Pipe, Stream}
import org.http4s.Uri
import org.joda.time.DateTime