Skip to content

Commit

Permalink
privatize stuff inside the internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
arainko committed Nov 18, 2023
1 parent a6027dd commit df2491a
Show file tree
Hide file tree
Showing 32 changed files with 93 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package io.github.arainko.ducktape
import io.github.arainko.ducktape.internal.Transformations

final class AppliedBuilder[Source, Dest](value: Source) {
inline def transform(inline config: Field[Source, Dest] | Case[Source, Dest]*): Dest =
inline def transform(inline config: Field[Source, Dest] | Case[Source, Dest]*): Dest =
Transformations.between[Source, Dest](value, config*)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.github.arainko.ducktape
import io.github.arainko.ducktape.internal.Transformations

final class AppliedViaBuilder[Source, Dest, Func, Args <: FunctionArguments] private (value: Source, function: Func) {
inline def transform(inline config: Field[Source, Args] | Case[Source, Args]*): Dest =
inline def transform(inline config: Field[Source, Args] | Case[Source, Args]*): Dest =
Transformations.via[Source, Dest, Func, Args](value, function, config*)
}

Expand All @@ -13,6 +13,7 @@ object AppliedViaBuilder {

transparent inline def create[A, Func](source: A, inline function: Func): Any = {
val inst = instance(source, function)
internal.Function.encodeAsType[[args <: FunctionArguments, retTpe] =>> AppliedViaBuilder[A, retTpe, Func, args]](function, inst)
internal.Function
.encodeAsType[[args <: FunctionArguments, retTpe] =>> AppliedViaBuilder[A, retTpe, Func, args]](function, inst)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package io.github.arainko.ducktape
import io.github.arainko.ducktape.internal.Transformations

class DefinitionBuilder[Source, Dest] {
inline def build(inline config: Field[Source, Dest] | Case[Source, Dest]*): Transformer[Source, Dest] = source => Transformations.between[Source, Dest](source, config*)
inline def build(inline config: Field[Source, Dest] | Case[Source, Dest]*): Transformer[Source, Dest] = source =>
Transformations.between[Source, Dest](source, config*)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ object DefinitionViaBuilder {
extension [Source](partial: PartiallyApplied[Source]) {
transparent inline def apply[Func](inline function: Func): Any = {
val builder = instance[Source, Func](function)
internal.Function.encodeAsType[[args <: FunctionArguments, retTpe] =>> DefinitionViaBuilder[Source, retTpe, Func, args]](function, builder)
internal.Function.encodeAsType[[args <: FunctionArguments, retTpe] =>> DefinitionViaBuilder[Source, retTpe, Func, args]](
function,
builder
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.github.arainko.ducktape

import scala.collection.SortedMap
import scala.deriving.Mirror
import scala.reflect.TypeTest
import scala.collection.SortedMap
import io.github.arainko.ducktape.internal.Transformations

final case class Value(int: Int) extends AnyVal
final case class ValueGen[A](int: A) extends AnyVal
Expand Down Expand Up @@ -76,10 +75,9 @@ final case class NotRec[A](value: A = "string")

println(cos.unapply(test))


val rec: Rec[Int] = Rec(1, None)

// // given Transformer[NotRec[Int], NotRec[Int | String]] =
// // given Transformer[NotRec[Int], NotRec[Int | String]] =
// internal.CodePrinter.code:
// Transformer.defineVia[NotRec[Int]](NotRec[Int | String]).build(Field.const(_.value, ""))

Expand All @@ -92,21 +90,19 @@ final case class NotRec[A](value: A = "string")
// internal.CodePrinter.code:
// given Transformer[Rec[Int], Rec[Int | String]] = DefinitionBuilder[Rec[Int], Rec[Int | String]].build()


def test1[A, B](a: A)(using t: Transformer[A, B]): B =
def test1[A, B](a: A)(using t: Transformer[A, B]): B =
internal.CodePrinter.code:
a.to[B]

// rec.to[Rec[Int | String]]

// rec.to[Rec[Int | String]]

// // internal.CodePrinter.code:
// (??? : DeffTest1).into[DeffTest2].transform(
// (??? : DeffTest1).into[DeffTest2].transform(

// Field.default(a => a._1),
// Field.default(a => a.int),
// // Field.default(a => a.int.toByte)
// )
// Field.default(a => a._1),
// Field.default(a => a.int),
// // Field.default(a => a.int.toByte)
// )

// PositionTest.positions(
// "asdasdasdas",
Expand All @@ -127,34 +123,29 @@ final case class NotRec[A](value: A = "string")
*/
// DebugMacros.code {
// PlanInterpreter.transformBetween[ProdTest1, ProdTest2](
// Field2.const(_.test.at[Test2.Cos].int.additional, 1), // missing field
// Field.computed(_.test.at[Test2.Cos].int.additional, _.test.ordinal + 123),
// Field2.const(_.test.at[Test2.Cos].int.additional, 1), // missing field
// Field.computed(_.test.at[Test2.Cos].int.additional, _.test.ordinal + 123),

ProdTest1(Test1.Cos(Nested1(1)))
.into[ProdTest2]
.transform(
Field.const(_.test.at[Test2.Cos].int.int, 123), // overridden fieldn,
Case.const(_.test.at[Test1.Empty.type], ???),
Field.const(_.test.at[Test2.Cos].int.additional, 1),
Field.const(_.test.at[Test2.Cos].int.additional, 1)
// Field.default(_.test),

// Field.default(_.test),
// Field.default(_.test),
// Field.default(_.test),


// Field.default(_.test),


// Field.default(_.test.ordinal),
// Field2.const(_.add, 1), // missing field
// Case.const(_.test.at[Test1.Empty.type], Test2.Cos(Nested2(1, 1))) // missing case
// Case2.const(_.test.at[Test1.Cos], Test2.Cos(Nested2(1, 1))) // overriden case

)



)

// }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
package io.github.arainko.ducktape

import scala.quoted.*
import io.github.arainko.ducktape.internal.Transformations
import io.github.arainko.ducktape.DefinitionViaBuilder.PartiallyApplied
import io.github.arainko.ducktape.internal.Transformations

trait Transformer[Source, Dest] extends Transformer.Derived[Source, Dest]

object Transformer {
inline given derive[Source, Dest]: Transformer.Derived[Source, Dest] = new {
def transform(value: Source): Dest = Transformations.between[Source, Dest](value)
}
}

def define[Source, Dest]: DefinitionBuilder[Source, Dest] =
def define[Source, Dest]: DefinitionBuilder[Source, Dest] =
DefinitionBuilder[Source, Dest]

def defineVia[Source]: DefinitionViaBuilder.PartiallyApplied[Source] =
def defineVia[Source]: DefinitionViaBuilder.PartiallyApplied[Source] =
DefinitionViaBuilder.create[Source]

sealed trait Derived[Source, Dest] {
def transform(value: Source): Dest
}
}

object Test extends App {
// given t: Transformer[Int, Int] = ???

internal.CodePrinter.code:
summon[Transformer.Derived[Int, Int]]

// Transformer.derive[Int, Int].transform(1)
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private[ducktape] object Configuration {
Span.fromExpr(cfg)
) :: Nil

case cfg @ '{ Field.allMatching[a, b, source]($fieldSource) } =>
case cfg @ '{ Field.allMatching[a, b, source]($fieldSource) } =>
parseAllMatching(fieldSource, Path.empty(Type.of[b]), TypeRepr.of[b], TypeRepr.of[source], Span.fromExpr(cfg))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package io.github.arainko.ducktape.internal
import scala.quoted.*
import scala.util.chaining.*

object Defaults {
private[ducktape] object Defaults {
def of(struct: Structure.Product)(using Quotes): Map[String, Expr[Any]] = {
import quotes.reflect.*

Logger.debug(s"Searching for defaults for a type of", struct.tpe)

val tpe = struct.tpe.repr.widen
val sym = tpe.typeSymbol
val fieldNamesWithDefaults =
val fieldNamesWithDefaults =
Logger.loggedDebug("Fields that have a default"):
sym.caseFields.filter(_.flags.is(Flags.HasDefault)).map(_.name)

val companionBody = sym.companionClass.tree.asInstanceOf[ClassDef].body // sus but it works?
val companion = Ref(sym.companionModule)
val defaultValues = companionBody.collect {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.arainko.ducktape.internal

opaque type Depth <: Int = Int
private[ducktape] opaque type Depth <: Int = Int

object Depth {
private[ducktape] object Depth {
val zero: Depth = 0
def current(using depth: Depth): Depth = depth
def incremented(using depth: Depth): Depth = depth + 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package io.github.arainko.ducktape.internal

import scala.quoted.Quotes
import scala.quoted.Type
import io.github.arainko.ducktape.internal.Path.Segment

sealed trait ErrorMessage derives Debug {
import scala.quoted.{ Quotes, Type }

private[ducktape] sealed trait ErrorMessage derives Debug {
def render(using Quotes): String
def span: Span | None.type
def target: Target
}

object ErrorMessage {
private[ducktape] object ErrorMessage {
final case class NoFieldFound(fieldName: String, fieldTpe: Type[?], sourceTpe: Type[?]) extends ErrorMessage {
def render(using Quotes): String = s"No field '$fieldName' found in ${sourceTpe.repr.show}"
def span = None
Expand Down Expand Up @@ -76,7 +76,8 @@ object ErrorMessage {
}

case object RecursionSuspected extends ErrorMessage {
def render(using Quotes): String = "Recursive type suspected, consider using Transformer.define or Transformer.defineVia instead"
def render(using Quotes): String =
"Recursive type suspected, consider using Transformer.define or Transformer.defineVia instead"
val span: Span | None.type = None
val target: Target = Target.Dest
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.github.arainko.ducktape.*
import scala.collection.immutable.ListMap
import scala.quoted.*

final case class Function(args: ListMap[String, Type[?]], returnTpe: Type[?], expr: Expr[Any]) derives Debug {
private[ducktape] final case class Function(args: ListMap[String, Type[?]], returnTpe: Type[?], expr: Expr[Any]) derives Debug {

def appliedTo(using Quotes)(terms: List[quotes.reflect.Term]): Expr[Any] = {
import quotes.reflect.*
Expand All @@ -28,7 +28,7 @@ final case class Function(args: ListMap[String, Type[?]], returnTpe: Type[?], ex

}

object Function {
private[ducktape] object Function {
private type IsFuncArgs[A <: FunctionArguments] = A

def fromExpr(expr: Expr[Any])(using Quotes): Option[Function] = {
Expand All @@ -44,7 +44,9 @@ object Function {
}
}

def fromFunctionArguments[Args <: FunctionArguments: Type, Func: Type](functionExpr: Expr[Func])(using Quotes): Option[Function] = {
def fromFunctionArguments[Args <: FunctionArguments: Type, Func: Type](
functionExpr: Expr[Func]
)(using Quotes): Option[Function] = {
import quotes.reflect.*
val repr = TypeRepr.of[Args]
val func = TypeRepr.of[Func]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package io.github.arainko.ducktape.internal

import io.github.arainko.ducktape.internal.{ Metainformation }
import io.github.arainko.ducktape.internal.Metainformation

import scala.Ordering.Implicits.*
import scala.quoted.*
import scala.annotation.nowarn

private[ducktape] object Logger {

// Logger Config
private[ducktape] transparent inline given level: Level = Level.Off
private val output = Output.StdOut
@nowarn private def filter(msg: String, meta: Metainformation) = meta.contains("DeprecatedConfig")
private def filter(msg: String, meta: Metainformation) = true

enum Level {
case Off, Debug, Info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private[ducktape] object NonEmptyList {
private[ducktape] def fromList[A](list: List[A]): Option[NonEmptyList[A]] =
PartialFunction.condOpt(list) { case cons @ (_ :: _) => fromCons(cons) }

private [ducktape] given [A: Debug]: Debug[NonEmptyList[A]] = Debug.collection[A, List]
private[ducktape] given [A: Debug]: Debug[NonEmptyList[A]] = Debug.collection[A, List]

extension [A](self: NonEmptyList[A]) {
export toList.{ reduceLeft, head, tail, exists, filter, collect }
Expand All @@ -29,7 +29,7 @@ private[ducktape] object NonEmptyList {

private[ducktape] def map[B](f: A => B): NonEmptyList[B] = unsafeCoerce(toList.map(f))

private [ducktape] def groupBy[K](f: A => K): Map[K, NonEmptyList[A]] = unsafeCoerceK(self.groupBy(f))
private[ducktape] def groupBy[K](f: A => K): Map[K, NonEmptyList[A]] = unsafeCoerceK(self.groupBy(f))

private[ducktape] def reverse: NonEmptyList[A] = unsafeCoerce(toList.reverse)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.github.arainko.ducktape.internal.*
import scala.quoted.*
import scala.reflect.TypeTest

final case class Path(root: Type[?], segments: Vector[Path.Segment]) { self =>
private[ducktape] final case class Path(root: Type[?], segments: Vector[Path.Segment]) { self =>
def appended(segment: Path.Segment): Path = self.copy(segments = segments.appended(segment))

def prepended(segment: Path.Segment): Path = self.copy(segments = segments.prepended(segment))
Expand All @@ -26,7 +26,7 @@ final case class Path(root: Type[?], segments: Vector[Path.Segment]) { self =>
def isAncestorOrSiblingOf(that: Path)(using Quotes): Boolean = {
/*
import quotes.reflect.*
if (self.segments.length > that.segments.length) false
else
self.root.repr =:= that.root.repr && self.segments.zip(that.segments).forall {
Expand All @@ -36,7 +36,7 @@ final case class Path(root: Type[?], segments: Vector[Path.Segment]) { self =>
leftName == rightName && leftTpe.repr =:= rightTpe.repr
case _ => false
}
*/
*/
val thatRendered = that.render
val thisRendered = this.render
Logger.debug("that rendered", thatRendered)
Expand All @@ -58,7 +58,7 @@ final case class Path(root: Type[?], segments: Vector[Path.Segment]) { self =>
}
}

object Path {
private[ducktape] object Path {
def empty(root: Type[?]): Path = Path(root, Vector.empty)

given debug: Debug[Path] with {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.github.arainko.ducktape.internal
import scala.annotation.tailrec
import scala.quoted.*

object PathSelector {
private[ducktape] object PathSelector {
def unapply(using Quotes)(expr: quotes.reflect.Term): Some[Path] = {
import quotes.reflect.{ Selector as _, * }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import scala.annotation.tailrec
import scala.collection.immutable.ListMap
import scala.quoted.*

type PlanError = Plan.Error
private[ducktape] type PlanError = Plan.Error

enum Plan[+E <: PlanError] {
private[ducktape] enum Plan[+E <: PlanError] {
import Plan.*

def sourceTpe: Type[?]
Expand Down Expand Up @@ -139,7 +139,7 @@ enum Plan[+E <: PlanError] {
) extends Plan[Plan.Error]
}

object Plan {
private[ducktape] object Plan {

def unapply[E <: Plan.Error](plan: Plan[E]): (Type[?], Type[?]) = (plan.sourceTpe, plan.destTpe)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.github.arainko.ducktape.internal.*
import scala.collection.Factory
import scala.quoted.*

object PlanInterpreter {
private[ducktape] object PlanInterpreter {

def run[A: Type](plan: Plan[Nothing], sourceValue: Expr[A])(using Quotes): Expr[Any] =
recurse(plan, sourceValue)
Expand Down Expand Up @@ -78,7 +78,7 @@ object PlanInterpreter {
case ('[destCollTpe], '[srcElem], '[destElem]) =>
val sourceValue = value.asExprOf[Iterable[srcElem]]
// TODO: Make it nicer, move this into Planner since we cannot be sure that a facotry exists
val factory = Expr.summon[Factory[destElem, destCollTpe]].get
val factory = Expr.summon[Factory[destElem, destCollTpe]].get
def transformation(value: Expr[srcElem])(using Quotes): Expr[destElem] = recurse(plan, value).asExprOf[destElem]
'{ $sourceValue.map(src => ${ transformation('src) }).to($factory) }
}
Expand Down
Loading

0 comments on commit df2491a

Please sign in to comment.