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

code review: use fold instead of map follow by getOrElse #1157

Merged
merged 1 commit into from
Nov 8, 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
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ private[execution] class FutureResolver[Ctx](
val sfield = tpe.getField(schema, origField.name).head
val fieldPath = path.add(fields.head, tpe)

def resolveUc(v: Any) = newUc.map(_.ctxFn(v)).getOrElse(uc)
def resolveUc(v: Any) = newUc.fold(uc)(_.ctxFn(v))

def resolveError(e: Throwable) = {
try newUc.foreach(_.onError(e))
Expand Down Expand Up @@ -875,7 +875,7 @@ private[execution] class FutureResolver[Ctx](
}

private def resolveUc(newUc: Option[MappedCtxUpdate[Ctx, Any, Any]], v: Any, userCtx: Ctx) =
newUc.map(_.ctxFn(v)).getOrElse(userCtx)
newUc.fold(userCtx)(_.ctxFn(v))

private def resolveVal(newUc: Option[MappedCtxUpdate[Ctx, Any, Any]], v: Any) = newUc match {
case Some(MappedCtxUpdate(_, mapFn, _)) => mapFn(v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,11 @@ object QueryReducerExecutor {
case abst: AbstractType =>
schema.possibleTypes
.get(abst.name)
.map(types =>
.fold(initialValues)(types =>
types.map(loop(path, _, astFields)).transpose.zipWithIndex.map { case (values, idx) =>
val reducer = reducers(idx)
reducer.reduceAlternatives(values.asInstanceOf[Seq[reducer.Acc]])
})
.getOrElse(initialValues)
case s: ScalarType[_] => reducers.map(_.reduceScalar(path, userContext, s))
case ScalarAlias(aliasFor, _, _) =>
reducers.map(_.reduceScalar(path, userContext, aliasFor))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ object IntrospectionParser {

private def parseNamedTypeRef[In: InputUnmarshaller](in: In, path: Vector[String]) =
IntrospectionNamedTypeRef(
mapStringFieldOpt(in, "kind", path).map(TypeKind.fromString).getOrElse(TypeKind.Object),
mapStringFieldOpt(in, "kind", path).fold(TypeKind.Object)(TypeKind.fromString),
mapStringField(in, "name", path))

private def parseTypeRef[In: InputUnmarshaller](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,8 @@ object QueryRenderer {
renderComment(vd, prev, indent, config) +
indent.str + "$" + name + ":" + config.separator +
renderNode(tpe, config, indent.zero) +
defaultValue
.map(v =>
config.separator + "=" + config.separator + renderNode(v, config, indent.zero))
.getOrElse("") +
defaultValue.fold("")(v =>
config.separator + "=" + config.separator + renderNode(v, config, indent.zero)) +
renderDirs(dirs, config, indent, frontSep = true)

case NotNullType(ofType, _) =>
Expand All @@ -565,7 +563,7 @@ object QueryRenderer {

case f @ Field(alias, name, args, dirs, sels, _, _, _) =>
renderComment(f, prev, indent, config) +
indent.str + alias.map(_ + ":" + config.separator).getOrElse("") + name +
indent.str + alias.fold("")(_ + ":" + config.separator) + name +
renderArgs(args, indent, config, withSep = false) +
(if (dirs.nonEmpty || sels.nonEmpty) config.separator else "") +
renderDirs(dirs, config, indent, withSep = sels.nonEmpty) +
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/sangria/schema/Action.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ case class PartialFutureValue[Ctx, Val](value: Future[PartialValue[Ctx, Val]])
PartialFutureValue(value.map(_.map(fn) match {
case v: PartialValue[Ctx, NewVal] => v
case TryValue(Failure(e)) => throw e
case v => throw new IllegalStateException("Unexpected result from `PartialValue.map`: " + v)
case v => throw new IllegalStateException(s"Unexpected result from `PartialValue.map`: $v")
}))
}

Expand Down
10 changes: 4 additions & 6 deletions modules/core/src/main/scala/sangria/schema/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1487,12 +1487,10 @@ case class Schema[Ctx, Val](
val queryTypesWithAdditions = additionalTypes.foldLeft(queryTypes) { case (acc, tpe) =>
collectTypes("additional type", 10, tpe, acc)
}
val queryAndSubTypes = mutation
.map(collectTypes("a mutation type", 10, _, queryTypesWithAdditions))
.getOrElse(queryTypesWithAdditions)
val queryAndSubAndMutTypes = subscription
.map(collectTypes("a subscription type", 10, _, queryAndSubTypes))
.getOrElse(queryAndSubTypes)
val queryAndSubTypes = mutation.fold(queryTypesWithAdditions)(
collectTypes("a mutation type", 10, _, queryTypesWithAdditions))
val queryAndSubAndMutTypes = subscription.fold(queryAndSubTypes)(
collectTypes("a subscription type", 10, _, queryAndSubTypes))

val queryAndSubAndMutAndDirArgTypes = directives.foldLeft(queryAndSubAndMutTypes) {
case (acc, dir) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ case class VarTypeMismatchViolation(
}

lazy val simpleErrorMessage =
s"Variable '$$$definitionName' expected value of type '$expectedType' but ${input.map("got: " + _).getOrElse("value is undefined")}. Reason: $violationMessage"
s"Variable '$$$definitionName' expected value of type '$expectedType' but ${input.fold("value is undefined")("got: " + _)}. Reason: $violationMessage"
}

case class UnknownVariableTypeViolation(
Expand Down Expand Up @@ -343,9 +343,7 @@ case class MisplacedDirectiveViolation(
val code = "misplacedDirective"
val args = Map(
"directiveName" -> name,
"location" -> correctPlacement
.map(loc => DirectiveLocation.toSpecString(loc._1))
.getOrElse("here"))
"location" -> correctPlacement.fold("here")(loc => DirectiveLocation.toSpecString(loc._1)))

lazy val simpleErrorMessage =
s"Directive '$name' may not be used ${correctPlacement.fold("here")("on " + _._2)}."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class DeriveEnumTypeMacro(context: blackbox.Context)
val upperCase = config.exists(_.isInstanceOf[MacroUppercaseValues])

val transformed =
transformFnOpt.map(fn => q"$fn($nonTransformedName)").getOrElse(nonTransformedName)
transformFnOpt.fold(nonTransformedName)(fn => q"$fn($nonTransformedName)")
if (upperCase)
q"sangria.util.StringUtil.camelCaseToUnderscore($transformed).toUpperCase"
else
Expand Down