Skip to content

Commit

Permalink
Introduce Path type intended for storing nested path in type level
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszKubuszok committed Sep 15, 2023
1 parent d53f562 commit 5701ab6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ private[compiletime] trait ChimneyTypesPlatform extends ChimneyTypes { this: Chi
def apply[F <: runtime.PatcherFlags.Flag: Type, Flags <: runtime.PatcherFlags: Type]
: Type[runtime.PatcherFlags.Enable[F, Flags]] =
weakTypeTag[runtime.PatcherFlags.Enable[F, Flags]]

def unapply[A](A: Type[A]): Option[(?<[runtime.PatcherFlags.Flag], ?<[runtime.PatcherFlags])] =
if (A.isCtor[runtime.PatcherFlags.Enable[?, ?]])
Some(A.param_<[runtime.PatcherFlags.Flag](0) -> A.param_<[runtime.PatcherFlags](1))
Expand All @@ -184,7 +183,6 @@ private[compiletime] trait ChimneyTypesPlatform extends ChimneyTypes { this: Chi
def apply[F <: runtime.PatcherFlags.Flag: Type, Flags <: runtime.PatcherFlags: Type]
: Type[runtime.PatcherFlags.Disable[F, Flags]] =
weakTypeTag[runtime.PatcherFlags.Disable[F, Flags]]

def unapply[A](A: Type[A]): Option[(?<[runtime.PatcherFlags.Flag], ?<[runtime.PatcherFlags])] =
if (A.isCtor[runtime.PatcherFlags.Disable[?, ?]])
Some(A.param_<[runtime.PatcherFlags.Flag](0) -> A.param_<[runtime.PatcherFlags](1))
Expand All @@ -199,5 +197,16 @@ private[compiletime] trait ChimneyTypesPlatform extends ChimneyTypes { this: Chi
val MacrosLogging: Type[runtime.PatcherFlags.MacrosLogging] = weakTypeTag[runtime.PatcherFlags.MacrosLogging]
}
}

object Path extends PathModule {
val Root: Type[runtime.Path.Root] = weakTypeTag[runtime.Path.Root]
object Select extends SelectModule {
def apply[Name <: String: Type, Instance <: runtime.Path: Type]: Type[runtime.Path.Select[Name, Instance]] =
weakTypeTag[runtime.Path.Select[Name, Instance]]
def unapply[A](A: Type[A]): Option[(?<[String], ?<[runtime.Path])] =
if (A.isCtor[runtime.Path.Select[?, ?]]) Some(A.param_<[String](0) -> A.param_<[runtime.Path](1))
else scala.None
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,17 @@ private[compiletime] trait ChimneyTypesPlatform extends ChimneyTypes { this: Chi
val MacrosLogging: Type[runtime.PatcherFlags.MacrosLogging] = quoted.Type.of[runtime.PatcherFlags.MacrosLogging]
}
}

object Path extends PathModule {
val Root: Type[runtime.Path.Root] = quoted.Type.of[runtime.Path.Root]
object Select extends SelectModule {
def apply[Name <: String: Type, Instance <: runtime.Path: Type]: Type[runtime.Path.Select[Name, Instance]] =
quoted.Type.of[runtime.Path.Select[Name, Instance]]
def unapply[A](tpe: Type[A]): Option[(?<[String], ?<[runtime.Path])] = tpe match
case '[runtime.Path.Select[name, instance]] =>
Some((Type[name].as_?<[String], Type[instance].as_?<[runtime.Path]))
case _ => scala.None
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ private[compiletime] trait ChimneyTypes { this: ChimneyDefinitions =>
}
}

val Path: PathModule
trait PathModule { this: Path.type =>
val Root: Type[runtime.Path.Root]
val Select: SelectModule
trait SelectModule
extends Type.Ctor2UpperBounded[
String,
runtime.Path,
runtime.Path.Select
] { this: Select.type => }
}

// You can `import ChimneyType.Implicits.*` in your shared code to avoid providing types manually, while avoiding conflicts
// with implicit types seen in platform-specific scopes (which would happen if those implicits were always used).
object Implicits {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.scalaland.chimney.internal.runtime

sealed abstract class Path
object Path {
final class Root extends Path
final class Select[Name <: String, Instance <: Path] extends Path
}

0 comments on commit 5701ab6

Please sign in to comment.