Skip to content

Commit

Permalink
Work toward generator support
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Mar 17, 2024
1 parent 7f7cc5a commit 603f730
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions example/src/main/scala/example/Person.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package example
import com.outr.arango.{Document, DocumentModel, Field, Id, Index}
import fabric.rw._

import java.util.Calendar

//case class Person(name: String,
// age: Int = 21,
// address: Address)
Expand All @@ -24,6 +26,22 @@ case class JustDoc(name: String) extends Foo with Document[JustDoc] {
def doSomething(): Unit = scribe.info("Testing!")
}

object JustDoc {}

/*case class JustDoc(name: String, _id: Id[JustDoc] = JustDoc.id()) extends Document[JustDoc] {
// Custom method
def doSomething(): Unit = scribe.info("Testing!")
}
object JustDoc extends DocumentModel[JustDoc] {
override implicit val rw: RW[JustDoc] = RW.gen
override val collectionName: String = "JustDoc"
val name: Field[String] = field("name")
override def indexes: List[Index] = Nil
}*/

//case class Correct(name: String, _id: Id[Correct] = Correct.id()) extends Document[Correct]
//
//object Correct extends CorrectModel
Expand Down
23 changes: 23 additions & 0 deletions generator/src/main/scala/com/outr/arango/generator/Generator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,33 @@ case class Generator(path: Path,
c.ctor.children.foreach { t =>
scribe.info(s"Child: $t (${t.getClass.getName})")
}
if (c.mods.map(_.toString()).contains("case") && isDocumentClass(c)) {
val className = c.name.toString()
var params = c.ctor.paramClauses.head.values.map(p => Param(
name = p.name.toString(),
clazz = p.decltpe.get.toString()
))
params.find(_.name == "_id") match {
case None => params = params ::: List(Param("_id", s"Id[$className]"))
case Some(p) if p.clazz != s"Id[$className]" =>
params = params.filterNot(_.name == "_id") ::: List(Param("_id", s"Id[$className]"))
case _ => // Ignore
}
val template = c.templ.toString()
scribe.info(s"Params: ${params.mkString(" | ")}")
val pre = s"case class $className"
val sep = s",\n${List.fill(pre.length + 1)(' ').mkString}"
val replacement = s"""$pre(${params.mkString(sep)}) $template"""
scribe.info(replacement)
}
}
}

private def isDocumentClass(c: Defn.Class): Boolean = c.templ.children.exists { t =>
t.toString().contains(s"Document[${c.name}]")
} || c.mods.exists(_.toString() == "case")

case class Param(name: String, clazz: String) {
override def toString: String = s"$name: $clazz"
}
}

0 comments on commit 603f730

Please sign in to comment.