Skip to content

Commit

Permalink
generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ValdemarGr committed Aug 15, 2023
1 parent e2d376b commit 581e807
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions generator/src/main/scala/spice4s/generator/Generator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,23 @@ object Generator extends App {
.map(x => State(x.toMap))
}

def resource = q"""
def resourceHeaders = List(
q"""
trait Spice4sResourceConstants {
def objectType: Type
}
""",
q"""
trait Spice4sResource {
def constants: Spice4sResourceConstants
def value: String
def objectType: Type
def ref: ObjectReference = ObjectReference(
objectType,
constants.objectType,
Id.unsafeFromString(value)
)
}
"""
)

def snake2camel(x: String) = {
val xs = x.split("_")
Expand Down Expand Up @@ -128,7 +135,7 @@ object Generator extends App {
def resourceRelationTrait(relation: String, possibleTypes: NonEmptyList[String]) = {
val traitName = snake2Type(relation)
val pts = possibleTypes.map { pt =>
val ext = Init(Type.Apply(traitName, Type.ArgClause(List(snake2Type(pt)))), Name.Anonymous(), Seq.empty)
val ext = Init(Type.Apply(traitName, Type.ArgClause(List(definitionTypeReference(pt)))), Name.Anonymous(), Seq.empty)
q"implicit object ${snake2Obj(relation.capitalize + "_" + pt.capitalize)} extends $ext"
}

Expand All @@ -150,7 +157,7 @@ object Generator extends App {
val n = relation + subjectRelation.foldMap("_" + _)
tpe match {
case Right(x) =>
q"def ${Term.Name(snake2camel(n))}(that: ${snake2Type(x)}): CheckPermissionRequest = $impl"
q"def ${Term.Name(snake2camel(n))}(that: ${definitionTypeReference(x)}): CheckPermissionRequest = $impl"
case Left(companion) =>
q"""
def ${Term.Name(snake2camel(n))}[A <: Resource](
Expand Down Expand Up @@ -196,17 +203,19 @@ object Generator extends App {
}
}

val companionContent = ys.collect { case (rd, mr, None) => rd -> mr }.toNel.toList.map { nel =>
snake2Obj(res.name) -> nel
}
val objName = snake2Obj(res.name)

val companionContent = ys.collect { case (rd, mr, None) => rd -> mr }

val c: List[Defn.Object] = companionContent.map { case (objName, nel) =>
val members = nel.flatMap { case (rd, mr) =>
resourceRelationTrait(rd.name + mr.subjectRelation.foldMap("_" + _), mr.possibleTypes)
val companion: Defn.Object = {
val objType = q"def objectType: Type = Type.unsafeFromString(${Lit.String(res.name)})"

val members = objType :: companionContent.flatMap { case (rd, mr) =>
resourceRelationTrait(rd.name + mr.subjectRelation.foldMap("_" + _), mr.possibleTypes).toList
}
q"""
object ${objName} {
..${members.toList}
object ${objName} extends Spice4sResourceConstants {
..${members}
}
"""
}
Expand All @@ -219,21 +228,19 @@ object Generator extends App {
resourceRelationMethod(rd.name, Right(name), mr.subjectRelation)
}

val unionCls = companionContent.flatMap { case (companionName, nel) =>
nel.toList.map { case (rd, mr) =>
resourceRelationMethod(rd.name, Left(companionName), mr.subjectRelation)
}
val unionCls = companionContent.map { case (rd, mr) =>
resourceRelationMethod(rd.name, Left(objName), mr.subjectRelation)
}

val combined = q"def objectType: Type = Type.unsafeFromString(${Lit.String(res.name)})" ::
val combined = q"def constants: Spice4sResourceConstants = ${objName}" ::
(rds ++ singularCls ++ unionCls)
List(
q"""
case class ${snake2Type(res.name)}(value: String) extends Spice4sResource {
..${combined}
}
"""
) ++ c
) ++ List(companion)
}

def relationRelationName(rd: String): Term.Name =
Expand Down Expand Up @@ -262,10 +269,10 @@ object Generator extends App {
val computed = compute(state)
val namespaced = ress.map { res =>
val parts = res.name.split("/")
val namespace =
if (parts.size > 1) parts.headOption
else None
namespace -> res
parts.toList match {
case ns :: name :: Nil => (Some(ns) -> res.copy(name = name))
case _ => (None -> res)
}
}
namespaced
.groupMap { case (ns, _) => ns } { case (_, res) => res }
Expand Down Expand Up @@ -294,9 +301,8 @@ object Generator extends App {
convertSchema(schema).map { xs =>
val prefix = List(
q"package spice4s.generated",
q"import spice4s.client.models._",
resource
)
q"import spice4s.client.models._"
) ++ resourceHeaders
val all = prefix ++ xs
all.map(_.syntax).mkString("\n\n")
// q"..$all".syntax
Expand Down

0 comments on commit 581e807

Please sign in to comment.