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

Support generics when deriving Parser in scala 3 #602

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -28,20 +28,21 @@ object LowPriorityParserImplicits {
t: Type[U]
): List[(q.reflect.Symbol, q.reflect.TypeRepr)] = {
import quotes.reflect.*
val sym = TypeRepr.of[U] match {
val (sym, typeParams) = TypeRepr.of[U] match {
case AppliedType(base, params) =>
base.typeSymbol
(base.typeSymbol, params)
case _ =>
TypeTree.of[U].symbol
(TypeTree.of[U].symbol, List())
}
val typeParamSyms = sym.primaryConstructor.paramSymss.flatten.filter(_.isTypeParam)

sym.primaryConstructor
.paramSymss
.flatten
.map(f => (f, f.tree))
.collect {
case (sym, v: ValDef) =>
(sym, v.tpt.tpe)
(sym, v.tpt.tpe.substituteTypes(typeParamSyms, typeParams))
}
}

Expand Down Expand Up @@ -76,7 +77,10 @@ object LowPriorityParserImplicits {
${ tupleParserImpl[T] }
private def tupleParserImpl[T](using q: Quotes, t: Type[T]): Expr[Parser[_]] = {
import quotes.reflect.*
val tSym = TypeTree.of[T].symbol
val (tSym, params) = TypeRepr.of[T] match {
case AppliedType(base, params) => (base.typeSymbol, Some(params))
case _ => (TypeTree.of[T].symbol, None)
}
val origin = shortName[T]
val fields0 = fields[T]

Expand All @@ -98,7 +102,8 @@ object LowPriorityParserImplicits {
.map(_.name)
val values = body.collect {
case d @ DefDef(name, _, _, _) if name.startsWith("$lessinit$greater$default") =>
Ref(d.symbol).asExpr
val ref = Ref(d.symbol)
params.fold(ref)(ref.appliedToTypes).asExpr
}
names.zip(values).toMap
case None =>
Expand Down
Empty file added mill.YgqpH0
Empty file.
Empty file added mill.diW7CU
Empty file.
Empty file added mill.qn1XWZ
Empty file.
13 changes: 13 additions & 0 deletions tests/shared/src/test/scala/caseapp/CaseAppTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,19 @@ object CaseAppTests extends TestSuite {
assert(stagesArg.tags == Seq(Tag("foo"), Tag("other")))
}

test("support generic args class") {
val res =
Parser[GenericArgs[SharedOptions]].detailedParse(
Seq("--main", "v1", "--other", "v2")
)

val expectedRes =
Right(
(GenericArgs("v1", SharedOptions("v2"), ""), RemainingArgs(List(), List()))
)
assert(res == expectedRes)
}

}

}
6 changes: 6 additions & 0 deletions tests/shared/src/test/scala/caseapp/demo/Demo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,9 @@ object ManualSubCommandStuff {
def run(options: ManualSubCommandOptions.Command2Opts, args: RemainingArgs): Unit = {}
}
}

case class GenericArgs[Shared](
main: String,
@Recurse() shared: Shared,
opt: String = ""
)