Skip to content

Commit 930cf1c

Browse files
committed
add codegen for nodestarters with integrated filters
1 parent bd2d60f commit 930cf1c

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

codegen/src/main/scala/overflowdb/codegen/CodeGen.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ class CodeGen(schema: Schema) {
8585
starters.append(
8686
s"""@overflowdb.traversal.help.Doc(info = "All nodes of type ${typ.className}, i.e. with label ${typ.name}")
8787
|def ${typ.starterName.get}: Iterator[nodes.${typ.className}] = overflowdb.traversal.InitialTraversal.from[nodes.${typ.className}](wrapper.graph, "${typ.name}")""".stripMargin)
88+
typ.primaryKey match {
89+
case Some(property:Property[_]) =>
90+
//fixme
91+
val nameCamelCase = camelCase(property.name)
92+
val baseType = typeFor(property)
93+
starters.append(
94+
s"""@overflowdb.traversal.help.Doc(info = "Shorthand for ${typ.starterName.get}.${nameCamelCase}")
95+
|def ${typ.starterName.get}(key: ${baseType}): Iterator[nodes.${typ.className}] =
96+
| new ${traversalsPackage}.${typ.className}TraversalExtGen(${typ.starterName.get}).${nameCamelCase}(key)""".stripMargin)
97+
case _ =>
98+
}
8899
}
89100
for (typ <- schema.nodeBaseTypes if typ.starterName.isDefined) {
90101
val types = schema.nodeTypes.filter{_.extendzRecursively.contains(typ)}

codegen/src/main/scala/overflowdb/schema/Schema.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ class NodeType(name: String, comment: Option[String], schemaInfo: SchemaInfo)
139139
extends AbstractNodeType(name, comment, schemaInfo) with HasOptionalProtoId {
140140
protected val _containedNodes: mutable.Set[ContainedNode] = mutable.Set.empty
141141

142+
private var _primaryKey: Option[Property[_]] = None
143+
144+
def primaryKey(p: Property[_]): this.type = {
145+
this._primaryKey = Some(p)
146+
this
147+
}
148+
149+
def primaryKey: Option[Property[_]] = this._primaryKey
150+
142151
lazy val classNameDb = s"${className}Db"
143152

144153
/** all node types that extend this node */

integration-tests/schemas/src/main/scala/TestSchema01.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TestSchema01 extends TestSchema {
1313
val node1 = builder.addNodeType(
1414
name = "NODE1",
1515
comment = "sample node 1"
16-
).addProperties(name, order)
16+
).addProperties(name, order).primaryKey(name)
1717

1818
val node2 = builder.addNodeType(
1919
name = "NODE2",

integration-tests/tests/src/test/scala/Schema01Test.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ class Schema01Test extends AnyWordSpec with Matchers {
8181
// TODO generate domain-specific setters in codegen
8282
}
8383

84+
"use generated node starters" in {
85+
val domainspecific = new testschema01.TestSchema(graph)
86+
object TmpImplicit {
87+
implicit def toStarter(wrapped: testschema01.TestSchema): testschema01.GeneratedNodeStarterExt = new testschema01.GeneratedNodeStarterExt(wrapped)
88+
}
89+
import TmpImplicit._
90+
domainspecific.node1.toList shouldBe List(node1a, node1b)
91+
domainspecific.node1(".*1b").toList shouldBe List(node1b)
92+
93+
}
94+
8495
"generate NewNodes" in {
8596
val newNode2 = NewNode2()
8697
.name("name1")

0 commit comments

Comments
 (0)