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 4fb71fc
Show file tree
Hide file tree
Showing 4 changed files with 65 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,23 @@ 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]

Check warning on line 202 in chimney/src/main/scala-2/io/scalaland/chimney/internal/compiletime/ChimneyTypesPlatform.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-2/io/scalaland/chimney/internal/compiletime/ChimneyTypesPlatform.scala#L202

Added line #L202 was not covered by tests
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
}
object Match extends MatchModule {
def apply[Subtype: Type, Instance <: runtime.Path: Type]: Type[runtime.Path.Match[Subtype, Instance]] =
weakTypeTag[runtime.Path.Match[Subtype, Instance]]

Check warning on line 212 in chimney/src/main/scala-2/io/scalaland/chimney/internal/compiletime/ChimneyTypesPlatform.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-2/io/scalaland/chimney/internal/compiletime/ChimneyTypesPlatform.scala#L204-L212

Added lines #L204 - L212 were not covered by tests
def unapply[A](A: Type[A]): Option[(??, ?<[runtime.Path])] =
if (A.isCtor[runtime.Path.Match[?, ?]]) Some(A.param(0) -> A.param_<[runtime.Path](1))
else scala.None
}
}

Check warning on line 217 in chimney/src/main/scala-2/io/scalaland/chimney/internal/compiletime/ChimneyTypesPlatform.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-2/io/scalaland/chimney/internal/compiletime/ChimneyTypesPlatform.scala#L214-L217

Added lines #L214 - L217 were not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,25 @@ private[compiletime] trait ChimneyTypesPlatform extends ChimneyTypes { this: Chi
val MacrosLogging: Type[runtime.PatcherFlags.MacrosLogging] = quoted.Type.of[runtime.PatcherFlags.MacrosLogging]
}
}

object Path extends PathModule {

Check warning on line 204 in chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/ChimneyTypesPlatform.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/ChimneyTypesPlatform.scala#L204

Added line #L204 was not covered by tests
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]))

Check warning on line 211 in chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/ChimneyTypesPlatform.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/ChimneyTypesPlatform.scala#L206-L211

Added lines #L206 - L211 were not covered by tests
case _ => scala.None
}
object Match extends MatchModule {
def apply[Subtype: Type, Instance <: runtime.Path: Type]: Type[runtime.Path.Match[Subtype, Instance]] =
quoted.Type.of[runtime.Path.Match[Subtype, Instance]]
def unapply[A](tpe: Type[A]): Option[(??, ?<[runtime.Path])] = tpe match
case '[runtime.Path.Match[subtype, instance]] =>
Some((Type[subtype].as_??, Type[instance].as_?<[runtime.Path]))

Check warning on line 219 in chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/ChimneyTypesPlatform.scala

View check run for this annotation

Codecov / codecov/patch

chimney/src/main/scala-3/io/scalaland/chimney/internal/compiletime/ChimneyTypesPlatform.scala#L214-L219

Added lines #L214 - L219 were not covered by tests
case _ => scala.None
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ 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 => }
val Match: MatchModule
trait MatchModule
extends Type.Ctor2UpperBounded[
Any,
runtime.Path,
runtime.Path.Match
] { this: Match.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,8 @@
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
final class Match[Subtype, Instance <: Path] extends Path
}

0 comments on commit 4fb71fc

Please sign in to comment.