Skip to content

Commit

Permalink
Use Initial unapply matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed Jul 3, 2023
1 parent 641c1bd commit 8a6bf14
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
22 changes: 11 additions & 11 deletions gen/rules/src/main/scala/clue/gen/Generator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ trait Generator {
val (extensionDefinitions, newParentBody) =
parentBody.partitionMap {
// q"..$mods trait $tname[..$tparams] extends $template"
case Defn.Trait.After_4_6_0(_, tname, _, _, Template.After_4_4_0(early, inits, _, _, _))
case Defn.Trait.Initial(_, tname, _, _, Template.Initial(early, inits, _, _))
if tname.value == typeName =>
(early, inits).asLeft
case other =>
Expand All @@ -46,9 +46,9 @@ trait Generator {
parentBody
.collectFirst {
// q"..$mods type $tname[..$tparams] = $tpe"
case Defn.Type.After_4_6_0(_, tname, _, _, _) if tname.value == typeName => Skip
case Defn.Type.Initial(_, tname, _, _) if tname.value == typeName => Skip
// q"..$mods class $tname[..$tparams] ..$ctorMods (...$paramss) extends $template"
case Defn.Class.After_4_6_0(_, tname, _, _, _) if tname.value == typeName => Skip
case Defn.Class.Initial(_, tname, _, _, _) if tname.value == typeName => Skip
}
.getOrElse(Define(parentBody, List.empty, List.empty))
case Some((early, inits)) =>
Expand All @@ -58,11 +58,11 @@ trait Generator {

private def nestedTypeTree(nestTree: Term.Ref, tpe: Type): Type =
tpe match {
case named @ Type.Name(_) => Type.Select(nestTree, named)
case named @ Type.Name(_) => Type.Select(nestTree, named)
// case Type.Select?
case Type.Apply.After_4_6_0(ttpe, args) =>
Type.Apply.After_4_6_0(ttpe, args.map(t => nestedTypeTree(nestTree, t)))
case other =>
case Type.Apply.Initial(ttpe, args) =>
Type.Apply.Initial(ttpe, args.map(t => nestedTypeTree(nestTree, t)))
case other =>
throw new Exception(s"Type structure [$other] not supported.")
}

Expand Down Expand Up @@ -93,18 +93,18 @@ trait Generator {
val t: Type = typeTree(nestTree, nestedTypes)
val d: Option[Term] = tpe match {
// case t"Option[$_]" =>
case Type.Apply.After_4_6_0(Type.Name("Option"), _) =>
case Type.Apply.Initial(Type.Name("Option"), _) =>
if (!asVals) q"None".some else none
// case t"clue.data.Input[_]" =>
case Type.Apply.After_4_6_0(
case Type.Apply.Initial(
Type.Select(
Term.Select(Term.Name("clue"), Term.Name("data")),
Type.Name("Input")
),
_
) =>
if (!asVals) q"clue.data.Ignore".some else none
case _ => none
case _ => none
}
val mods: List[Mod] = if (!asVals && overrides) List(mod"override") else List.empty
param"..$mods val $n: $t = $d"
Expand Down Expand Up @@ -461,7 +461,7 @@ trait Generator {
parentBody.foldLeft((List.empty[Stat], false)) { case ((newStats, modified), stat) =>
stat match {
// q"..$mods object $objName extends $template}"
case Defn.Object(mods, objName, Template.After_4_4_0(early, inits, self, body, _))
case Defn.Object(mods, objName, Template.Initial(early, inits, self, body))
if objName.value == moduleName =>
(
newStats :+ q"..$mods object $objName extends {..$early} with ..$inits { $self => ..${bodyMod(body)} }", // ${Template(early, inits, self, bodyMod(body))}",
Expand Down
20 changes: 10 additions & 10 deletions gen/rules/src/main/scala/clue/gen/GraphQLGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ class GraphQLGen(config: GraphQLGenConfig)
_
) =>
IO.pure(Patch.replaceTree(obj, ""))
case obj @ Defn.Trait.After_4_6_0(
case obj @ Defn.Trait.Initial(
mods @ GraphQLSchemaAnnotation(_),
templateName,
Nil,
_,
Template.After_4_4_0(early, inits, self, stats, _)
Template.Initial(early, inits, self, stats)
) =>
val objName = templateName.value
config.getSchema(objName).map { schema =>
Expand All @@ -87,15 +87,15 @@ class GraphQLGen(config: GraphQLGenConfig)
)
) + Patch.removeGlobalImport(GraphQLSchemaAnnotation.symbol)
}
case obj @ Defn.Trait.After_4_6_0(
case obj @ Defn.Trait.Initial(
mods @ GraphQLAnnotation(_),
templateName,
Nil,
_,
Template.After_4_4_0(early, inits, self, stats, _)
Template.Initial(early, inits, self, stats)
) if inits.exists {
case Init.After_4_6_0(
Type.Apply.After_4_6_0(Type.Name("GraphQLOperation"), _),
case Init.Initial(
Type.Apply.Initial(Type.Name("GraphQLOperation"), _),
_,
_
) =>
Expand Down Expand Up @@ -160,15 +160,15 @@ class GraphQLGen(config: GraphQLGenConfig)
}
}
}
case obj @ Defn.Class.After_4_6_0(
case obj @ Defn.Class.Initial(
mods @ GraphQLAnnotation(_),
templateName,
Nil,
_,
Template.After_4_4_0(early, inits, self, stats, _)
Template.Initial(early, inits, self, stats)
) if inits.exists {
case Init.After_4_6_0(
Type.Apply.After_4_6_0(Type.Name("GraphQLSubquery"), _),
case Init.Initial(
Type.Apply.Initial(Type.Name("GraphQLSubquery"), _),
_,
_
) =>
Expand Down
20 changes: 10 additions & 10 deletions gen/rules/src/main/scala/clue/gen/QueryGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ trait QueryGen extends Generator {
// TODO This could be more sophisticated.
protected def extractSchemaType(list: List[Init]): Option[Type.Name] =
list.collect {
case Init.After_4_6_0(
Type.Apply.After_4_6_0(Type.Name("GraphQLOperation"), List(tpe @ Type.Name(_))),
case Init.Initial(
Type.Apply.Initial(Type.Name("GraphQLOperation"), List(tpe @ Type.Name(_))),
_,
Nil
) =>
Expand All @@ -26,8 +26,8 @@ trait QueryGen extends Generator {

protected def extractSchemaAndRootTypes(list: List[Init]): Option[(Type.Name, String)] =
list.collect {
case Init.After_4_6_0(
Type.Apply.After_4_6_0(Type.Name("GraphQLSubquery"), List(tpe @ Type.Name(_))),
case Init.Initial(
Type.Apply.Initial(Type.Name("GraphQLSubquery"), List(tpe @ Type.Name(_))),
_,
List(List(Lit.String(rootType)))
) =>
Expand Down Expand Up @@ -428,10 +428,10 @@ trait QueryGen extends Generator {
parentBody.exists {
// We are not checking in pattern assignments
// case q"$_ val $tname: $_ = $_" => tname == tpe
case Defn.Val(_, List(Pat.Var(Term.Name(name))), _, _) => name == termName
case Defn.Val(_, List(Pat.Var(Term.Name(name))), _, _) => name == termName
// case q"$_ var $tname: $_ = $_" => tname == tpe
case Defn.Var.After_4_7_2(_, List(Pat.Var(Term.Name(name))), _, _) => name == termName
case _ => false
case Defn.Var.Initial(_, List(Pat.Var(Term.Name(name))), _, _) => name == termName
case _ => false
}

private def addValDef(
Expand All @@ -455,7 +455,7 @@ trait QueryGen extends Generator {
parentBody
.collectFirst {
// case q"$_ object $tname extends { ..$_ } with ..$_ { $_ => ..$dataBody }"
case Defn.Object(_, Term.Name(name), Template.After_4_4_0(_, _, _, dataBody, _))
case Defn.Object(_, Term.Name(name), Template.Initial(_, _, _, dataBody))
if name == moduleName =>
dataBody
}
Expand Down Expand Up @@ -487,11 +487,11 @@ trait QueryGen extends Generator {
parentBody
.collectFirst {
// case q"$_ class Variables $_(...$paramss) extends { ..$_ } with ..$_ { $_ => ..$_ }" =>
case Defn.Class.After_4_6_0(
case Defn.Class.Initial(
_,
Type.Name(name),
_,
Ctor.Primary.After_4_6_0(_, _, paramss),
Ctor.Primary.Initial(_, _, paramss),
_
) if name == "Variables" =>
// Strip "val" from mods.
Expand Down
7 changes: 3 additions & 4 deletions gen/rules/src/main/scala/clue/gen/annotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ class AnnotationPattern(fqcn: String) {
}

def removeFrom(mods: List[Mod]): List[Mod] = mods.filter {
case Mod.Annot(Init.After_4_6_0(typeTree, Name(""), _)) if isAnnotationNamed(name)(typeTree) =>
case Mod.Annot(Init.Initial(typeTree, Name(""), _)) if isAnnotationNamed(name)(typeTree) =>
false
case _ => true
case _ => true
}

def unapply(mods: List[Mod]): Option[List[Term]] = mods.reverse.collectFirst {
case Mod.Annot(Init.After_4_6_0(typeTree, Name(""), args))
if isAnnotationNamed(name)(typeTree) =>
case Mod.Annot(Init.Initial(typeTree, Name(""), args)) if isAnnotationNamed(name)(typeTree) =>
Some(args.toList.flatten)
}.flatten
}
Expand Down

0 comments on commit 8a6bf14

Please sign in to comment.