From bb3894b7901960c1074dab4b97a936e1520d7db5 Mon Sep 17 00:00:00 2001 From: Kyri Petrou Date: Tue, 30 Jan 2024 19:56:22 +1100 Subject: [PATCH 1/2] Optimize field parsing --- .../parsing/parsers/SelectionParsers.scala | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/core/src/main/scala/caliban/parsing/parsers/SelectionParsers.scala b/core/src/main/scala/caliban/parsing/parsers/SelectionParsers.scala index 2af9e859d..6fc582c28 100644 --- a/core/src/main/scala/caliban/parsing/parsers/SelectionParsers.scala +++ b/core/src/main/scala/caliban/parsing/parsers/SelectionParsers.scala @@ -10,7 +10,7 @@ import scala.annotation.nowarn @nowarn("msg=NoWhitespace") // False positive warning in Scala 2.x private[caliban] trait SelectionParsers extends ValueParsers { - def alias(implicit ev: P[Any]): P[String] = P(name ~ ":") + def aliasOrName(implicit ev: P[Any]): P[String] = P(":" ~ name) def argument(implicit ev: P[Any]): P[(String, InputValue)] = P(name ~ ":" ~ value) def arguments(implicit ev: P[Any]): P[Map[String, InputValue]] = P("(" ~/ argument.rep ~ ")").map(_.toMap) @@ -27,23 +27,18 @@ private[caliban] trait SelectionParsers extends ValueParsers { def namedType(implicit ev: P[Any]): P[NamedType] = P(name.filter(_ != "null")).map(NamedType(_, nonNull = false)) def listType(implicit ev: P[Any]): P[ListType] = P("[" ~ type_ ~ "]").map(t => ListType(t, nonNull = false)) - def nonNullType(implicit ev: P[Any]): P[Type] = P((namedType | listType) ~ "!").map { - case t: NamedType => t.copy(nonNull = true) - case t: ListType => t.copy(nonNull = true) + def type_(implicit ev: P[Any]): P[Type] = P((namedType | listType) ~ "!".!.?).map { + case (t: NamedType, nn) => if (nn.isDefined) t.copy(nonNull = true) else t + case (t: ListType, nn) => if (nn.isDefined) t.copy(nonNull = true) else t } - def type_(implicit ev: P[Any]): P[Type] = P(nonNullType | namedType | listType) def field(implicit ev: P[Any]): P[Field] = - P(Index ~ alias.? ~ name ~ arguments.? ~ directives.? ~ selectionSet.?).map { - case (index, alias, name, args, dirs, sels) => - Field( - alias, - name, - args.getOrElse(Map()), - dirs.getOrElse(Nil), - sels.getOrElse(Nil), - index - ) + P(Index ~ name ~ aliasOrName.? ~ arguments.? ~ directives.? ~ selectionSet.?).map { + case (index, alias, Some(name), args, dirs, sels) => + Field(Some(alias), name, args.getOrElse(Map()), dirs.getOrElse(Nil), sels.getOrElse(Nil), index) + case (index, name, _, args, dirs, sels) => + Field(None, name, args.getOrElse(Map()), dirs.getOrElse(Nil), sels.getOrElse(Nil), index) + } def fragmentName(implicit ev: P[Any]): P[String] = P(name).filter(_ != "on") From c0ec4a4357b3f3d62cd564532eadd8d26bd3ba8c Mon Sep 17 00:00:00 2001 From: Kyri Petrou Date: Tue, 30 Jan 2024 20:10:13 +1100 Subject: [PATCH 2/2] Make mima happy --- .../scala/caliban/parsing/parsers/SelectionParsers.scala | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/src/main/scala/caliban/parsing/parsers/SelectionParsers.scala b/core/src/main/scala/caliban/parsing/parsers/SelectionParsers.scala index 6fc582c28..dfef4b3c7 100644 --- a/core/src/main/scala/caliban/parsing/parsers/SelectionParsers.scala +++ b/core/src/main/scala/caliban/parsing/parsers/SelectionParsers.scala @@ -10,6 +10,9 @@ import scala.annotation.nowarn @nowarn("msg=NoWhitespace") // False positive warning in Scala 2.x private[caliban] trait SelectionParsers extends ValueParsers { + + @deprecated("Kept for bincompat only, scheduled to be removed") + def alias(implicit ev: P[Any]): P[String] = P(name ~ ":") def aliasOrName(implicit ev: P[Any]): P[String] = P(":" ~ name) def argument(implicit ev: P[Any]): P[(String, InputValue)] = P(name ~ ":" ~ value) @@ -27,6 +30,12 @@ private[caliban] trait SelectionParsers extends ValueParsers { def namedType(implicit ev: P[Any]): P[NamedType] = P(name.filter(_ != "null")).map(NamedType(_, nonNull = false)) def listType(implicit ev: P[Any]): P[ListType] = P("[" ~ type_ ~ "]").map(t => ListType(t, nonNull = false)) + @deprecated("Kept for bincompat only, scheduled to be removed") + def nonNullType(implicit ev: P[Any]): P[Type] = P((namedType | listType) ~ "!").map { + case t: NamedType => t.copy(nonNull = true) + case t: ListType => t.copy(nonNull = true) + } + def type_(implicit ev: P[Any]): P[Type] = P((namedType | listType) ~ "!".!.?).map { case (t: NamedType, nn) => if (nn.isDefined) t.copy(nonNull = true) else t case (t: ListType, nn) => if (nn.isDefined) t.copy(nonNull = true) else t