From 603f73086d03f86eb55e8277ca1e72c01bfaf98b Mon Sep 17 00:00:00 2001 From: Matt Hicks Date: Sat, 16 Mar 2024 20:01:09 -0500 Subject: [PATCH] Work toward generator support --- example/src/main/scala/example/Person.scala | 18 +++++++++++++++ .../com/outr/arango/generator/Generator.scala | 23 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/example/src/main/scala/example/Person.scala b/example/src/main/scala/example/Person.scala index c36093b3..facd5109 100644 --- a/example/src/main/scala/example/Person.scala +++ b/example/src/main/scala/example/Person.scala @@ -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) @@ -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 diff --git a/generator/src/main/scala/com/outr/arango/generator/Generator.scala b/generator/src/main/scala/com/outr/arango/generator/Generator.scala index 7b6bcd4d..96602478 100644 --- a/generator/src/main/scala/com/outr/arango/generator/Generator.scala +++ b/generator/src/main/scala/com/outr/arango/generator/Generator.scala @@ -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" + } } \ No newline at end of file