Skip to content

Commit

Permalink
Merge pull request #160 from ubaldop/add-from-input-iterable-implicits
Browse files Browse the repository at this point in the history
Add Iterable subtype bound to FromInput
  • Loading branch information
yanns authored Sep 10, 2024
2 parents 4241851 + cde0579 commit 1f0cb13
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
13 changes: 13 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.typesafe.tools.mima.core._

val isScala3 = Def.setting(
CrossVersion.partialVersion(scalaVersion.value).exists(_._1 == 3)
)
Expand All @@ -10,6 +12,17 @@ mimaPreviousArtifacts := {
else
Set("org.sangria-graphql" %% "sangria-marshalling-api" % "1.0.5")
}
mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[IncompatibleMethTypeProblem](
"sangria.marshalling.CoercedScalaResultMarshaller.mapAndMarshal"),
ProblemFilters.exclude[IncompatibleResultTypeProblem]("sangria.marshalling.FromInput.seqInput"),
ProblemFilters.exclude[IncompatibleResultTypeProblem]("sangria.marshalling.FromInput.seqInput"),
ProblemFilters.exclude[MissingClassProblem]("sangria.marshalling.FromInput$SeqFromInput"),
ProblemFilters.exclude[DirectMissingMethodProblem](
"sangria.marshalling.ResultMarshaller.mapAndMarshal"),
ProblemFilters.exclude[IncompatibleMethTypeProblem](
"sangria.marshalling.ScalaResultMarshaller.mapAndMarshal")
)

description := "Sangria Marshalling API"
homepage := Some(url("https://sangria-graphql.github.io/"))
Expand Down
25 changes: 15 additions & 10 deletions src/main/scala/sangria/marshalling/FromInput.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ object FromInput {
def fromResult(node: marshaller.Node): marshaller.Node = node
}

class SeqFromInput[T](delegate: FromInput[T]) extends FromInput[Seq[T]] {
class IterableFromInput[T, I[_] <: Iterable[_]](delegate: FromInput[T]) extends FromInput[I[T]] {
val marshaller: ResultMarshaller = delegate.marshaller

def fromResult(node: marshaller.Node): Seq[T] =
def fromResult(node: marshaller.Node): I[T] =
node
.asInstanceOf[Seq[Any]]
.map {
case optElem: Option[_] =>
optElem.map(elem => delegate.fromResult(elem.asInstanceOf[delegate.marshaller.Node]))
case elem =>
delegate.fromResult(elem.asInstanceOf[delegate.marshaller.Node])
.asInstanceOf[I[Any]]
.map { (e: Any) =>
e match {
case optElem: Option[_] =>
optElem.map(elem => delegate.fromResult(elem.asInstanceOf[delegate.marshaller.Node]))
case elem =>
delegate.fromResult(elem.asInstanceOf[delegate.marshaller.Node])
}
}
.asInstanceOf[Seq[T]]
.asInstanceOf[I[T]]
}

import sangria.util.tag._
Expand All @@ -43,7 +45,10 @@ object FromInput {

implicit def optionInput[T](implicit ev: FromInput[T]): FromInput[Option[T]] =
ev.asInstanceOf[FromInput[Option[T]]]
implicit def seqInput[T](implicit ev: FromInput[T]): SeqFromInput[T] = new SeqFromInput[T](ev)
implicit def seqInput[T](implicit ev: FromInput[T]): IterableFromInput[T, Seq] =
new IterableFromInput[T, Seq](ev)
implicit def iterableInput[T](implicit ev: FromInput[T]): IterableFromInput[T, Iterable] =
new IterableFromInput[T, Iterable](ev)

trait CoercedScalaResult
trait InputObjectResult
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/sangria/marshalling/ResultMarshaller.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ trait ResultMarshaller {
def renderCompact(node: Node): String
def renderPretty(node: Node): String

def mapAndMarshal[T](seq: Seq[T], fn: T => Node): Node = {
def mapAndMarshal[T](seq: Iterable[T], fn: T => Node): Node = {
val res = new VectorBuilder[Node]

for (elem <- seq)
Expand Down

0 comments on commit 1f0cb13

Please sign in to comment.