Skip to content

Commit

Permalink
Remove dependency on zio-prelude (#2314)
Browse files Browse the repository at this point in the history
* Remove dependency on zio-prelude

* Fix for Scala 2.12
  • Loading branch information
kyri-petrou authored Jun 27, 2024
1 parent f1b85ac commit 806a8d0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 54 deletions.
2 changes: 0 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ val zqueryVersion = "0.7.2"
val zioJsonVersion = "0.7.0"
val zioHttpVersion = "3.0.0-RC9"
val zioOpenTelemetryVersion = "3.0.0-RC21"
val zioPreludeVersion = "1.0.0-RC27"

Global / onChangedBuildSource := ReloadOnSourceChanges

Expand Down Expand Up @@ -176,7 +175,6 @@ lazy val core = project
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
"dev.zio" %% "zio-query" % zqueryVersion,
"dev.zio" %% "zio-prelude" % zioPreludeVersion,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test,
"dev.zio" %% "zio-json" % zioJsonVersion % Optional,
Expand Down
82 changes: 43 additions & 39 deletions core/src/main/scala/caliban/relay/PaginationArgs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package caliban.relay

import caliban.CalibanError
import zio.Exit
import zio.prelude._

object Pagination {
import PaginationCount._
Expand All @@ -12,75 +11,80 @@ object Pagination {
apply(args.first, args.last, args.before, args.after)

def apply[C: Cursor](args: ForwardPaginationArgs[C]): Exit[CalibanError, Pagination[C]] =
(args.first match {
case None => Validation.fail("first cannot be empty")
case Some(a) => validatePositive("first", a).map(First.apply)
})
.zipWith(args.after match {
case None => Validation.succeed(NoCursor)
case Some(x) => Validation.fromEither(Cursor[C].decode(x)).map(After.apply)
})(new Pagination[C](_, _))(
// Explicit implicits cause thanks Scala 2.12
ZValidation.ZValidationIdentityBoth,
implicitly
)
.toExit
toPaginationExit(
args.first match {
case None => Left("first cannot be empty")
case Some(a) => validatePositive("first", a).map(First.apply)
},
args.after match {
case None => Right(NoCursor)
case Some(x) => Cursor[C].decode(x).map(After.apply)
}
)

def apply[C: Cursor](args: BackwardPaginationArgs[C]): Exit[CalibanError, Pagination[C]] =
(args.last match {
case None => Validation.fail("last cannot be empty")
case Some(a) => validatePositive("last", a).map(Last.apply)
})
.zipWith(args.before match {
case None => Validation.succeed(NoCursor)
case Some(x) => Validation.fromEither(Cursor[C].decode(x)).map(Before.apply)
})(new Pagination[C](_, _))(ZValidation.ZValidationIdentityBoth, implicitly)
.toExit
toPaginationExit(
args.last match {
case None => Left("last cannot be empty")
case Some(a) => validatePositive("last", a).map(Last.apply)
},
args.before match {
case None => Right(NoCursor)
case Some(x) => Cursor[C].decode(x).map(Before.apply)
}
)

def apply[C: Cursor](
first: Option[Int],
last: Option[Int],
before: Option[String],
after: Option[String]
): Exit[CalibanError, Pagination[C]] =
validateFirstLast(first, last)
.zipWith(validateCursors(before, after))(new Pagination[C](_, _))(ZValidation.ZValidationIdentityBoth, implicitly)
.toExit
toPaginationExit(
validateFirstLast(first, last),
validateCursors(before, after)
)

private def validateCursors[C: Cursor](
before: Option[String],
after: Option[String]
): Validation[String, PaginationCursor[C]] =
): Either[String, PaginationCursor[C]] =
(before, after) match {
case (Some(_), Some(_)) =>
Validation.fail("before and after cannot both be set")
Left("before and after cannot both be set")
case (Some(x), _) =>
Validation.fromEither(Cursor[C].decode(x)).map(Before.apply)
Cursor[C].decode(x).map(Before.apply)
case (_, Some(x)) =>
Validation.fromEither(Cursor[C].decode(x)).map(After.apply)
case (None, None) => Validation.succeed(NoCursor)
Cursor[C].decode(x).map(After.apply)
case (None, None) => Right(NoCursor)
}

private def validateFirstLast(first: Option[Int], last: Option[Int]) =
(first, last) match {
case (None, None) =>
Validation.fail("first and last cannot both be empty")
Left("first and last cannot both be empty")
case (Some(_), Some(_)) =>
Validation.fail("first and last cannot both be set")
Left("first and last cannot both be set")
case (Some(a), _) =>
validatePositive("first", a).map(First.apply)
case (_, Some(b)) =>
validatePositive("last", b).map(Last.apply)
}

private def validatePositive(which: String, i: Int) =
if (i > -1) Validation.succeed(i) else Validation.fail(s"$which cannot be negative")

private implicit class ValidationOps[E, A](private val v: Validation[String, A]) extends AnyVal {
def toExit: Exit[CalibanError, A] = {
val either = v.toEitherWith(errors => CalibanError.ExecutionError(msg = errors.mkString(", ")))
Exit.fromEither(either)
if (i > -1) Right(i) else Left(s"$which cannot be negative")

private def toPaginationExit[C](
count: Either[String, PaginationCount],
cursor: Either[String, PaginationCursor[C]]
): Exit[CalibanError, Pagination[C]] = {
val v = (count, cursor) match {
case (Right(c), Right(r)) => Right(new Pagination(c, r))
case (Left(c), Left(r)) => Left(List(c, r))
case (Left(c), _) => Left(List(c))
case (_, Left(r)) => Left(List(r))
}
Exit.fromEither(v.left.map(errors => CalibanError.ExecutionError(msg = errors.mkString(", "))))
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import caliban.introspection.adt._
import caliban.parsing.adt.Selection
import caliban.validation.Utils._
import zio.Chunk
import zio.prelude._
import zio.prelude.fx.ZPure

import scala.collection.mutable
import scala.util.hashing.MurmurHash3
Expand Down Expand Up @@ -64,12 +62,12 @@ object FragmentValidator {

def doTypesConflict(t1: __Type, t2: __Type): Boolean =
if (isNonNull(t1))
if (isNonNull(t2)) (t1.ofType, t2.ofType).mapN((p1, p2) => doTypesConflict(p1, p2)).getOrElse(true)
if (isNonNull(t2)) t1.ofType.flatMap(p1 => t2.ofType.map(p2 => doTypesConflict(p1, p2))).getOrElse(true)
else true
else if (isNonNull(t2))
true
else if (isListType(t1))
if (isListType(t2)) (t1.ofType, t2.ofType).mapN((p1, p2) => doTypesConflict(p1, p2)).getOrElse(true)
if (isListType(t2)) t1.ofType.flatMap(p1 => t2.ofType.map(p2 => doTypesConflict(p1, p2))).getOrElse(true)
else true
else if (isListType(t2))
true
Expand Down
16 changes: 7 additions & 9 deletions core/src/main/scala/caliban/wrappers/Caching.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import caliban.parsing.adt.{ Directive, Document }
import caliban.schema.Annotations.{ GQLDirective, GQLName }
import caliban.schema.Types
import caliban.wrappers.Wrapper.{ EffectfulWrapper, FieldWrapper, OverallWrapper, ValidationWrapper }
import zio.prelude._
import zio.query.ZQuery
import zio.{ durationInt, Duration, Exit, FiberRef, Ref, UIO, Unsafe, ZIO }
import zio.{ duration2DurationOps, durationInt, Duration, Exit, FiberRef, Ref, UIO, Unsafe, ZIO }

import java.util.concurrent.{ ConcurrentHashMap, TimeUnit }
import scala.collection.compat._

object Caching {
val DirectiveName = "cacheControl"
Expand Down Expand Up @@ -167,15 +167,13 @@ object Caching {
override def toString: String = "PUBLIC"
}

implicit val ord: Ord[CacheScope] = Ord.make {
case (Private, Private) => Ordering.Equals
case (Public, Public) => Ordering.Equals
case (Private, Public) => Ordering.LessThan
case (Public, Private) => Ordering.GreaterThan
implicit val ordering: Ordering[CacheScope] = {
case (Private, Private) => 0
case (Public, Public) => 0
case (Private, Public) => -1
case (Public, Private) => 1
}

implicit val ordering: scala.math.Ordering[CacheScope] = ord.toScala

val _type = __Type(
__TypeKind.ENUM,
Some("CacheScope"),
Expand Down

0 comments on commit 806a8d0

Please sign in to comment.