Skip to content

Commit

Permalink
generate node based propertykeys (#184)
Browse files Browse the repository at this point in the history
* refactor: extract PropertyKeysImpl for reuse

* generate node based propertykeys

* fixup codegen for scala 2.12
  • Loading branch information
mpollmeier authored Apr 30, 2024
1 parent 1305740 commit 6a66c35
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,21 @@ class DomainClassesGenerator(schema: Schema) {
val sourceLines = Seq.newBuilder[String]
nodeType.properties.map { property =>
s"""${scaladocMaybe(property.comment)}
|val ${camelCaseCaps(property.name)} = "${property.name}" """.stripMargin
|val ${camelCaseCaps(property.name)} = "${property.name}" """.stripMargin
}.map(sourceLines.addOne)
nodeType.containedNodes.map { containedNode =>
s"""${scaladocMaybe(containedNode.comment)}
|val ${camelCaseCaps(containedNode.localName)} = "${containedNode.localName}" """.stripMargin
|val ${camelCaseCaps(containedNode.localName)} = "${containedNode.localName}" """.stripMargin.trim
}.map(sourceLines.addOne)
sourceLines.result().mkString("")
sourceLines.result().mkString("\n")
}

val propertyKeys = {
val sourceLines = Seq.newBuilder[String]
nodeType.properties.map { property =>
propertyKeySource(property, propertyKindByProperty(property))
}.map(sourceLines.addOne)
sourceLines.result().mkString("\n")
}

val propertyDefaults = nodeType.properties.collect {
Expand Down Expand Up @@ -497,6 +505,9 @@ class DomainClassesGenerator(schema: Schema) {
| object PropertyNames {
| $propertyNames
| }
| object PropertyKeys {
| $propertyKeys
| }
| object PropertyDefaults {
| $propertyDefaults
| }
Expand Down Expand Up @@ -1041,40 +1052,21 @@ class DomainClassesGenerator(schema: Schema) {
)
}

// PropertyKeys.scala <start>
// TODO refactor: extract to method
val propertyKeysConstantsSource = {
schema.properties.filter(kindContexts.propertyKindByProperty.contains).map { property =>
val kind = kindContexts.propertyKindByProperty(property)
val valueTypeUnpacked = unpackTypeUnboxed(property.valueType, isStored = false)
val documentation = property.comment.filter(_.nonEmpty).map(comment => s"""/** $comment */""").getOrElse("")
val propertyKeyConstantImpl = property.cardinality match {
case Cardinality.One(default) =>
val defaultValueImpl = Helpers.defaultValueImpl(default)
s"""flatgraph.SinglePropertyKey[$valueTypeUnpacked](kind = $kind, name = "${property.name}", default = $defaultValueImpl)"""
case Cardinality.ZeroOrOne =>
s"""flatgraph.OptionalPropertyKey[$valueTypeUnpacked](kind = $kind, name = "${property.name}")"""
case Cardinality.List =>
s"""flatgraph.MultiPropertyKey[$valueTypeUnpacked](kind = $kind, name = "${property.name}")"""
}
s"""$documentation
|val ${camelCaseCaps(property.name)} = $propertyKeyConstantImpl
|""".stripMargin
propertyKeySource(property, propertyKind = kindContexts.propertyKindByProperty(property))
}
}.mkString("\n\n")
val file = outputDir / "PropertyKeys.scala"
os.write(
file,
s"""package ${schema.basePackage}
|
|import flatgraph.PropertyKey
|
|object PropertyKeys {
|$propertyKeysConstantsSource
|}""".stripMargin
)
results.addOne(file)
// PropertyKeys.scala <end>

results.result()
}
Expand Down Expand Up @@ -1337,6 +1329,7 @@ class DomainClassesGenerator(schema: Schema) {
case _ => ???
}
}

def unpackTypeUnboxed(tpe: ValueType[?], isStored: Boolean, raised: Boolean = false): String = {
tpe match {
case ValueType.Boolean => "Boolean"
Expand Down Expand Up @@ -1370,6 +1363,21 @@ class DomainClassesGenerator(schema: Schema) {
}
}

def propertyKeySource(property: Property[?], propertyKind: Int): String = {
val valueTypeUnpacked = unpackTypeUnboxed(property.valueType, isStored = false)
val propertyKeyImpl = property.cardinality match {
case Cardinality.One(default) =>
val defaultValueImpl = Helpers.defaultValueImpl(default)
s"""flatgraph.SinglePropertyKey[$valueTypeUnpacked](kind = $propertyKind, name = "${property.name}", default = $defaultValueImpl)"""
case Cardinality.ZeroOrOne =>
s"""flatgraph.OptionalPropertyKey[$valueTypeUnpacked](kind = $propertyKind, name = "${property.name}")"""
case Cardinality.List =>
s"""flatgraph.MultiPropertyKey[$valueTypeUnpacked](kind = $propertyKind, name = "${property.name}")"""
}
s"""${scaladocMaybe(property.comment)}
|val ${camelCaseCaps(property.name)} = $propertyKeyImpl""".stripMargin.trim
}

private case class PropertyContexts(properties: Array[Property[?]], containedNodesByName: Map[String, mutable.HashSet[NodeType]])

private case class KindContexts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object Helpers {
s""""$string""""

def scaladocMaybe(comment: Option[String]): String =
comment.map(text => s"/** $text */").getOrElse("")
comment.filter(_.nonEmpty).map(content => s"/** $content */").getOrElse("")

def typeFor[A](property: Property[A]): String = {
val isMandatory = property.isMandatory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package flatgraph.testdomains.generic

import flatgraph.PropertyKey

object PropertyKeys {

val IntList = flatgraph.MultiPropertyKey[Int](kind = 0, name = "int_list")

val IntMandatory = flatgraph.SinglePropertyKey[Int](kind = 1, name = "int_mandatory", default = 42: Int)
Expand All @@ -15,5 +12,4 @@ object PropertyKeys {
val StringMandatory = flatgraph.SinglePropertyKey[String](kind = 4, name = "string_mandatory", default = "<empty>")

val StringOptional = flatgraph.OptionalPropertyKey[String](kind = 5, name = "string_optional")

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,26 @@ object NodeA {
val Label = "node_a"
object PropertyNames {

val IntList = "int_list"
val IntMandatory = "int_mandatory"
val IntOptional = "int_optional"
val StringList = "string_list"
val IntList = "int_list"

val IntMandatory = "int_mandatory"

val IntOptional = "int_optional"

val StringList = "string_list"

val StringMandatory = "string_mandatory"
val StringOptional = "string_optional"
val NodeB = "node_b"

val StringOptional = "string_optional"
val NodeB = "node_b"
}
object PropertyKeys {
val IntList = flatgraph.MultiPropertyKey[Int](kind = 0, name = "int_list")
val IntMandatory = flatgraph.SinglePropertyKey[Int](kind = 1, name = "int_mandatory", default = 42: Int)
val IntOptional = flatgraph.OptionalPropertyKey[Int](kind = 2, name = "int_optional")
val StringList = flatgraph.MultiPropertyKey[String](kind = 3, name = "string_list")
val StringMandatory = flatgraph.SinglePropertyKey[String](kind = 4, name = "string_mandatory", default = "<empty>")
val StringOptional = flatgraph.OptionalPropertyKey[String](kind = 5, name = "string_optional")
}
object PropertyDefaults {
val IntMandatory = 42: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ object NodeB {

val StringOptional = "string_optional"
}
object PropertyKeys {
val StringOptional = flatgraph.OptionalPropertyKey[String](kind = 5, name = "string_optional")
}
object PropertyDefaults {}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package flatgraph.testdomains.gratefuldead

import flatgraph.PropertyKey

object PropertyKeys {

val Name = flatgraph.SinglePropertyKey[String](kind = 0, name = "name", default = "")

val Songtype = flatgraph.OptionalPropertyKey[String](kind = 1, name = "songType")

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ object Artist {

val Name = "name"
}
object PropertyKeys {
val Name = flatgraph.SinglePropertyKey[String](kind = 0, name = "name", default = "")
}
object PropertyDefaults {
val Name = ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ object Song {
val Label = "song"
object PropertyNames {

val Name = "name"
val Name = "name"

val Songtype = "songType"
}
object PropertyKeys {
val Name = flatgraph.SinglePropertyKey[String](kind = 0, name = "name", default = "")
val Songtype = flatgraph.OptionalPropertyKey[String](kind = 1, name = "songType")
}
object PropertyDefaults {
val Name = ""
}
Expand Down

0 comments on commit 6a66c35

Please sign in to comment.