8000 declutter `Plan.scala` by arainko · Pull Request #108 · arainko/ducktape · GitHub
[go: up one dir, main page]

Skip to content
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 @@ -14,6 +14,7 @@ private[ducktape] enum Configuration derives Debug {
}

private[ducktape] object Configuration {

enum At derives Debug {
def path: Path
def target: Target
Expand Down
147 changes: 2 additions & 145 deletions ducktape/src/main/scala/io/github/arainko/ducktape/internal/Plan.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.github.arainko.ducktape.internal
import io.github.arainko.ducktape.*
import io.github.arainko.ducktape.internal.*

import scala.annotation.tailrec
import scala.collection.immutable.ListMap
import scala.quoted.*

Expand All @@ -20,9 +19,9 @@ private[ducktape] enum Plan[+E <: PlanError] {

def destContext: Path

final def configureAll(configs: List[Configuration.At])(using Quotes): Plan.Reconfigured = Plan.configureAll(this, configs)
final def configureAll(configs: List[Configuration.At])(using Quotes): Plan.Reconfigured = PlanConfigurer.run(this, configs)

final def refine: Either[NonEmptyList[Plan.Error], Plan[Nothing]] = Plan.refine(this)
final def refine: Either[NonEmptyList[Plan.Error], Plan[Nothing]] = PlanRefiner.run(this)

case Upcast(
sourceTpe: Type[?],
Expand Down Expand Up @@ -161,146 +160,4 @@ private[ducktape] object Plan {
successes: List[Configuration.At.Successful],
result: Plan[Plan.Error]
) derives Debug

private def configureAll(plan: Plan[Plan.Error], configs: List[Configuration.At])(using Quotes): Plan.Reconfigured = {
// Buffer of all errors that originate from a config
val errors = List.newBuilder[Plan.Error]
val successful = List.newBuilder[Configuration.At.Successful]

def configureSingle(plan: Plan[Plan.Error], config: Configuration.At)(using Quotes): Plan[Plan.Error] = {
extension (currentPlan: Plan[?]) {
def isReplaceableBy(update: Configuration.At.Successful)(using Quotes) =
update.config.tpe.repr <:< currentPlan.destContext.currentTpe.repr
}

def recurse(
current: Plan[Plan.Error],
segments: List[Path.Segment]
)(using Quotes): Plan[Plan.Error] = {
segments match {
case (segment @ Path.Segment.Field(_, fieldName)) :: tail =>
current match {
case plan @ BetweenProducts(sourceTpe, destTpe, sourceContext, destContext, fieldPlans) =>
val fieldPlan =
fieldPlans
.get(fieldName)
.map(fieldPlan => recurse(fieldPlan, tail))
.getOrElse(Plan.Error.from(plan, ErrorMessage.InvalidFieldAccessor(fieldName, config.span), None))

plan.copy(fieldPlans = fieldPlans.updated(fieldName, fieldPlan))
case plan @ BetweenProductFunction(sourceTpe, destTpe, sourceContext, destContext, argPlans, function) =>
val argPlan =
argPlans
.get(fieldName)
.map(argPlan => recurse(argPlan, tail))
.getOrElse(Plan.Error.from(plan, ErrorMessage.InvalidArgAccessor(fieldName, config.span), None))

plan.copy(argPlans = argPlans.updated(fieldName, argPlan))

case suppressed: Plan.Error =>
Plan.Error.from(plan, ErrorMessage.InvalidPathSegment(segment, config.target, config.span), Some(suppressed))

case plan =>
Plan.Error.from(plan, ErrorMessage.InvalidPathSegment(segment, config.target, config.span), None)
}

case (segment @ Path.Segment.Case(tpe)) :: tail =>
current match {
// BetweenNonOptionOption keeps the same type as its source so we passthrough it when traversing source nodes
case plan: BetweenNonOptionOption[Plan.Error] if config.target.isSource =>
plan.copy(plan = recurse(plan.plan, segments))

case plan @ BetweenCoproducts(sourceTpe, destTpe, sourceContext, destContext, casePlans) =>
def targetTpe(plan: Plan[Plan.Error]) = if (config.target.isSource) plan.sourceTpe.repr else plan.destTpe.repr

casePlans.zipWithIndex
.find((plan, _) => tpe.repr =:= targetTpe(plan))
.map((casePlan, idx) => plan.copy(casePlans = casePlans.updated(idx, recurse(casePlan, tail))))
.getOrElse(Plan.Error.from(plan, ErrorMessage.InvalidCaseAccessor(tpe, config.span), None))

case suppressed: Plan.Error =>
Plan.Error.from(plan, ErrorMessage.InvalidPathSegment(segment, config.target, config.span), Some(suppressed))
case plan =>
Plan.Error.from(plan, ErrorMessage.InvalidPathSegment(segment, config.target, config.span), None)
}

case (segment @ Path.Segment.Element(tpe)) :: tail =>
current match {
case p @ BetweenCollections(_, _, _, _, _, plan) =>
p.copy(plan = recurse(plan, tail))

case p @ BetweenOptions(_, _, _, _, plan) =>
p.copy(plan = recurse(plan, tail))

case p @ BetweenNonOptionOption(_, _, _, _, plan) =>
p.copy(plan = recurse(plan, tail))

case suppressed: Plan.Error =>
Plan.Error.from(plan, ErrorMessage.InvalidPathSegment(segment, config.target, config.span), Some(suppressed))
case plan =>
Plan.Error.from(plan, ErrorMessage.InvalidPathSegment(segment, config.target, config.span), None)
}

case Nil =>
import scala.util.chaining.*

config match {
case cfg @ Configuration.At.Successful(path, target, config, span) =>
if (current.isReplaceableBy(cfg)) {
successful.addOne(cfg)
Plan
.Configured(current.sourceTpe, current.destTpe, current.sourceContext, current.destContext, config)
} else
Plan.Error
.from(
current,
ErrorMessage.InvalidConfiguration(config.tpe, current.destContext.currentTpe, target, span),
None
)
.tap(errors.addOne)

case cfg @ Configuration.At.Failed(path, target, message, span) =>
Plan.Error.from(current, ErrorMessage.ConfigurationFailed(cfg), None).tap(errors.addOne)
}
}
}

recurse(plan, config.path.segments.toList)
}

val reconfiguredPlan = configs.foldLeft(plan)(configureSingle)
Plan.Reconfigured(errors.result(), successful.result(), reconfiguredPlan)
}

private def refine(plan: Plan[Plan.Error]): Either[NonEmptyList[Plan.Error], Plan[Nothing]] = {

@tailrec
def recurse(stack: List[Plan[Plan.Error]], errors: List[Plan.Error]): List[Plan.Error] =
stack match {
case head :: next =>
head match {
case plan: Plan.Upcast => recurse(next, errors)
case Plan.BetweenProducts(_, _, _, _, fieldPlans) =>
recurse(fieldPlans.values.toList ::: next, errors)
case Plan.BetweenCoproducts(_, _, _, _, casePlans) =>
recurse(casePlans.toList ::: next, errors)
case Plan.BetweenProductFunction(_, _, _, _, argPlans, _) =>
recurse(argPlans.values.toList ::: next, errors)
case Plan.BetweenOptions(_, _, _, _, plan) => recurse(plan :: next, errors)
case Plan.BetweenNonOptionOption(_, _, _, _, plan) => recurse(plan :: next, errors)
case Plan.BetweenCollections(_, _, _, _, _, plan) => recurse(plan :: next, errors)
case plan: Plan.BetweenSingletons => recurse(next, errors)
case plan: Plan.UserDefined => recurse(next, errors)
case plan: Plan.Derived => recurse(next, errors)
case plan: Plan.Configured => recurse(next, errors)
case plan: Plan.BetweenWrappedUnwrapped => recurse(next, errors)
case plan: Plan.BetweenUnwrappedWrapped => recurse(next, errors)
case error: Plan.Error => recurse(next, error :: errors)
}
case Nil => errors
}
val errors = recurse(plan :: Nil, Nil)
// if no errors were accumulated that means there are no Plan.Error nodes which means we operate on a Plan[Nothing]
NonEmptyList.fromList(errors).toLeft(plan.asInstanceOf[Plan[Nothing]])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package io.github.arainko.ducktape.internal

import io.github.arainko.ducktape.internal.Configuration.At

import scala.collection.mutable.Builder
import scala.quoted.*

private[ducktape] object PlanConfigurer {
import Plan.*

def run(plan: Plan[Plan.Error], configs: List[Configuration.At])(using Quotes): Plan.Reconfigured = {
// Buffer of all errors that originate from a config
val errors = List.newBuilder[Plan.Error]
val successful = List.newBuilder[Configuration.At.Successful]

def configureSingle(plan: Plan[Plan.Error], config: Configuration.At)(using Quotes): Plan[Plan.Error] = {

def recurse(
current: Plan[Plan.Error],
segments: List[Path.Segment]
)(using Quotes): Plan[Plan.Error] = {
segments match {
case (segment @ Path.Segment.Field(_, fieldName)) :: tail =>
current match {
case plan @ BetweenProducts(sourceTpe, destTpe, sourceContext, destContext, fieldPlans) =>
val fieldPlan =
fieldPlans
.get(fieldName)
.map(fieldPlan => recurse(fieldPlan, tail))
.getOrElse(Plan.Error.from(plan, ErrorMessage.InvalidFieldAccessor(fieldName, config.span), None))

plan.copy(fieldPlans = fieldPlans.updated(fieldName, fieldPlan))

case plan @ BetweenProductFunction(sourceTpe, destTpe, sourceContext, destContext, argPlans, function) =>
val argPlan =
argPlans
.get(fieldName)
.map(argPlan => recurse(argPlan, tail))
.getOrElse(Plan.Error.from(plan, ErrorMessage.InvalidArgAccessor(fieldName, config.span), None))

plan.copy(argPlans = argPlans.updated(fieldName, argPlan))

case other => invalidPathSegment(config, other, segment)
}

case (segment @ Path.Segment.Case(tpe)) :: tail =>
current match {
// BetweenNonOptionOption keeps the same type as its source so we passthrough it when traversing source nodes
case plan: BetweenNonOptionOption[Plan.Error] if config.target.isSource =>
plan.copy(plan = recurse(plan.plan, segments))

case plan @ BetweenCoproducts(sourceTpe, destTpe, sourceContext, destContext, casePlans) =>
def targetTpe(plan: Plan[Plan.Error]) = if (config.target.isSource) plan.sourceTpe.repr else plan.destTpe.repr

casePlans.zipWithIndex
.find((plan, _) => tpe.repr =:= targetTpe(plan))
.map((casePlan, idx) => plan.copy(casePlans = casePlans.updated(idx, recurse(casePlan, tail))))
.getOrElse(Plan.Error.from(plan, ErrorMessage.InvalidCaseAccessor(tpe, config.span), None))

case other => invalidPathSegment(config, other, segment)
}

case (segment @ Path.Segment.Element(tpe)) :: tail =>
current match {
case p @ BetweenCollections(_, _, _, _, _, plan) =>
p.copy(plan = recurse(plan, tail))

case p @ BetweenOptions(_, _, _, _, plan) =>
p.copy(plan = recurse(plan, tail))

case p @ BetweenNonOptionOption(_, _, _, _, plan) =>
p.copy(plan = recurse(plan, tail))

case other => invalidPathSegment(config, other, segment)
}

case Nil => configurePlan(config, current, errors, successful)
}
}

recurse(plan, config.path.segments.toList)
}

val reconfiguredPlan = configs.foldLeft(plan)(configureSingle)
Plan.Reconfigured(errors.result(), successful.result(), reconfiguredPlan)
}

private def configurePlan(
config: Configuration.At,
current: Plan[Error],
errors: Builder[Plan.Error, List[Plan.Error]],
successful: Builder[Configuration.At.Successful, List[Configuration.At.Successful]]
)(using Quotes) = {
import scala.util.chaining.*

extension (currentPlan: Plan[?]) {
def isReplaceableBy(update: Configuration.At.Successful)(using Quotes) =
update.config.tpe.repr <:< currentPlan.destContext.currentTpe.repr
}

config match {
case cfg @ Configuration.At.Successful(path, target, config, span) =>
if (current.isReplaceableBy(cfg)) {
successful.addOne(cfg)
Plan
.Configured(current.sourceTpe, current.destTpe, current.sourceContext, current.destContext, config)
} else
Plan.Error
.from(
current,
ErrorMessage.InvalidConfiguration(config.tpe, current.destContext.currentTpe, target, span),
None
)
.tap(errors.addOne)

case cfg @ Configuration.At.Failed(path, target, message, span) =>
Plan.Error.from(current, ErrorMessage.ConfigurationFailed(cfg), None).tap(errors.addOne)
}
}

private def invalidPathSegment(config: Configuration.At, plan: Plan[Error], segment: Path.Segment): Plan.Error =
plan match {
case suppressed: Plan.Error =>
Plan.Error.from(plan, ErrorMessage.InvalidPathSegment(segment, config.target, config.span), Some(suppressed))
case other =>
Plan.Error.from(plan, ErrorMessage.InvalidPathSegment(segment, config.target, config.span), None)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.github.arainko.ducktape.internal

import scala.annotation.tailrec

private[ducktape] object PlanRefiner {
def run(plan: Plan[Plan.Error]): Either[NonEmptyList[Plan.Error], Plan[Nothing]] = {

@tailrec
def recurse(stack: List[Plan[Plan.Error]], errors: List[Plan.Error]): List[Plan.Error] =
stack match {
case head :: next =>
head match {
case plan: Plan.Upcast => recurse(next, errors)
case Plan.BetweenProducts(_, _, _, _, fieldPlans) =>
recurse(fieldPlans.values.toList ::: next, errors)
case Plan.BetweenCoproducts(_, _, _, _, casePlans) =>
recurse(casePlans.toList ::: next, errors)
case Plan.BetweenProductFunction(_, _, _, _, argPlans, _) =>
recurse(argPlans.values.toList ::: next, errors)
case Plan.BetweenOptions(_, _, _, _, plan) => recurse(plan :: next, errors)
case Plan.BetweenNonOptionOption(_, _, _, _, plan) => recurse(plan :: next, errors)
case Plan.BetweenCollections(_, _, _, _, _, plan) => recurse(plan :: next, errors)
case plan: Plan.BetweenSingletons => recurse(next, errors)
case plan: Plan.UserDefined => recurse(next, errors)
case plan: Plan.Derived => recurse(next, errors)
case plan: Plan.Configured => recurse(next, errors)
case plan: Plan.BetweenWrappedUnwrapped => recurse(next, errors)
case plan: Plan.BetweenUnwrappedWrapped => recurse(next, errors)
case error: Plan.Error => recurse(next, error :: errors)
}
case Nil => errors
}
val errors = recurse(plan :: Nil, Nil)
// if no errors were accumulated that means there are no Plan.Error nodes which means we operate on a Plan[Nothing]
NonEmptyList.fromList(errors).toLeft(plan.asInstanceOf[Plan[Nothing]])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class ErrorReportingSuite extends DucktapeSuite {
)
"""
}(
"""The path segment 'toByte' is not valid as it is not a field of a case class or an argument of a function @ DestToplevel1
"""The path segment 'toByte' is not valid as it is not a field of a case class or an argument of a function @ DestToplevel1.level2Extra
| SUPPRESSES: The field 'level2Extra' doesn't have a default value @ DestToplevel1.level2Extra""".stripMargin,
"The field 'level2Extra' doesn't have a default value @ DestToplevel1.level2Extra",
"No field 'level2Extra' found in SourceToplevel1 @ DestToplevel1.level2Extra"
Expand Down
8 changes: 6 additions & 2 deletions tooling/src/main/scala/io/github/arainko/tooling/Debug.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ private[ducktape] object Debug {
}
}

644F given deferred[A]: Debug[() => A] with {
extension (value: () => A) def show(using Quotes): String =
given deferred: Debug[() => Any] with {
extension (value: () => Any) def show(using Quotes): String =
s"Deferred(...)"
}

given function: Debug[Nothing => Any] with {
extension (self: Nothing => Any) def show(using Quotes): String = s"Function(...)"
}

given expr[A]: Debug[Expr[A]] with {
extension (value: Expr[A]) def show(using Quotes): String = {
import quotes.reflect.*
Expand Down
0