Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Iterable subtype bound to FromInput #160

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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