Skip to content

Commit b73bcf2

Browse files
committed
Properties.java -> Properties.scala to avoid java interop issues
scala performs quite some magic to handle Option[Int], and the bytecode looks differently if javac or scalac compile something like `val x: Option[Int]`: ``` // scalac: public static flatgraph.SinglePropertyKey<java.lang.Object> ArgumentIndex(); //javac: public static final overflowdb.PropertyKey<scala.Int> ARGUMENT_INDEX; ``` Hence, the best way forward is to define these constants in scala.
1 parent 56cb7ea commit b73bcf2

File tree

4 files changed

+43
-38
lines changed

4 files changed

+43
-38
lines changed

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

+17-11
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,26 @@ class CodeGen(schema: Schema) {
254254
})
255255
}
256256

257-
writeConstantsFile("Properties", schema.properties.map { property =>
258-
val src = {
257+
// Properties.scala
258+
val propertyKeysConstantsSource = {
259+
schema.properties.map { property =>
259260
val valueType = typeFor(property)
260-
val cardinality = property.cardinality
261-
import Property.Cardinality
262-
val completeType = cardinality match {
263-
case Cardinality.One(_) => valueType
264-
case Cardinality.ZeroOrOne => valueType
265-
case Cardinality.List => s"scala.collection.IndexedSeq<$valueType>"
261+
val completeType = property.cardinality match {
262+
case Property.Cardinality.One(_) => valueType
263+
case Property.Cardinality.ZeroOrOne => valueType
264+
case Property.Cardinality.List => s"IndexedSeq[$valueType]"
266265
}
267-
s"""public static final overflowdb.PropertyKey<$completeType> ${property.name} = new overflowdb.PropertyKey<>("${property.name}");"""
266+
s"""val ${camelCaseCaps(property.name)}: overflowdb.PropertyKey[$completeType] = new overflowdb.PropertyKey("${property.name}")""".stripMargin.trim
268267
}
269-
ConstantContext(property.name, src, property.comment)
270-
})
268+
}.mkString("\n\n")
269+
val file = baseDir.createChild("Properties.scala").write(
270+
s"""package ${schema.basePackage}
271+
|
272+
|object Properties {
273+
|$propertyKeysConstantsSource
274+
|}""".stripMargin
275+
)
276+
results.append(file)
271277

272278
results.toSeq
273279
}

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ class Schema01Test extends AnyWordSpec with Matchers {
1111
import testschema01.traversal._
1212
"constants" in {
1313
PropertyNames.NAME shouldBe "NAME"
14-
Properties.ORDER.name shouldBe "ORDER"
14+
Properties.Order.name shouldBe "ORDER"
1515
PropertyNames.ALL.contains("OPTIONS") shouldBe true
16-
Properties.ALL.contains(Properties.OPTIONS) shouldBe true
1716

1817
NodeTypes.NODE1 shouldBe "NODE1"
1918
NodeTypes.ALL.contains(Node2.Label) shouldBe true
@@ -43,8 +42,8 @@ class Schema01Test extends AnyWordSpec with Matchers {
4342

4443
"lookup and traverse nodes/edges/properties" in {
4544
// generic traversal
46-
graph.nodes.asScala.property(Properties.NAME).toSetMutable shouldBe Set("node 1a", "node 1b", "node 2a", "node 2b")
47-
graph.edges.asScala.property(Properties.NAME).toSetMutable shouldBe Set("edge 2")
45+
graph.nodes.asScala.property(Properties.Name).toSetMutable shouldBe Set("node 1a", "node 1b", "node 2a", "node 2b")
46+
graph.edges.asScala.property(Properties.Name).toSetMutable shouldBe Set("edge 2")
4847
node1Traversal.out.toList shouldBe Seq(node2a)
4948
node1Traversal.name.toSetMutable shouldBe Set("node 1a", "node 1b")
5049
node1Traversal.order.l shouldBe Seq(2)

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,18 @@ class Schema04Test extends AnyWordSpec with Matchers {
7878
val node2 = graph.addNode(Node1.Label).asInstanceOf[Node1]
7979
val edge1 = node1.addEdge(Edge1.Label, node2).asInstanceOf[Edge1]
8080
val properties = Seq(
81-
Properties.BOOL.of(false),
82-
Properties.STR.of("foo"),
83-
Properties.BYTE.of(100: Byte),
84-
Properties.SHORT.of(101: Short),
85-
Properties.INT.of(102),
86-
Properties.LONG.of(103),
87-
Properties.FLOAT1.of(Float.NaN),
88-
Properties.FLOAT2.of(104.4f),
89-
Properties.DOUBLE1.of(Double.NaN),
90-
Properties.DOUBLE2.of(105.5),
91-
Properties.CHAR.of('Z'),
92-
Properties.INT_LIST.of(ArraySeq(3, 4, 5)),
81+
Properties.Bool.of(false),
82+
Properties.Str.of("foo"),
83+
Properties.Byte.of(100: Byte),
84+
Properties.Short.of(101: Short),
85+
Properties.Int.of(102),
86+
Properties.Long.of(103),
87+
Properties.Float1.of(Float.NaN),
88+
Properties.Float2.of(104.4f),
89+
Properties.Double1.of(Double.NaN),
90+
Properties.Double2.of(105.5),
91+
Properties.Char.of('Z'),
92+
Properties.IntList.of(ArraySeq(3, 4, 5)),
9393
)
9494
properties.foreach(node1.setProperty)
9595
properties.foreach(edge1.setProperty)
@@ -164,8 +164,8 @@ class Schema04Test extends AnyWordSpec with Matchers {
164164
val node1 = graph.addNode(Node1.Label).asInstanceOf[Node1]
165165
val node2 = graph.addNode(Node1.Label).asInstanceOf[Node1]
166166
val edge1 = node1.addEdge(Edge1.Label, node2).asInstanceOf[Edge1]
167-
node1.setProperty(Properties.INT_LIST.name, Array(1,2,3))
168-
edge1.setProperty(Properties.INT_LIST.name, Array(3,4,5))
167+
node1.setProperty(Properties.IntList.name, Array(1,2,3))
168+
edge1.setProperty(Properties.IntList.name, Array(3,4,5))
169169

170170
node1.intList shouldBe IndexedSeq(1,2,3)
171171
edge1.intList shouldBe IndexedSeq(3,4,5)

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ class Schema05Test extends AnyWordSpec with Matchers {
4949
val node2 = graph.addNode(Node1.Label).asInstanceOf[Node1]
5050
val edge1 = node1.addEdge(Edge1.Label, node2).asInstanceOf[Edge1]
5151
val properties = Seq(
52-
Properties.BOOL.of(false),
53-
Properties.STR.of("foo"),
54-
Properties.BYTE.of(100: Byte),
55-
Properties.SHORT.of(101: Short),
56-
Properties.INT.of(102),
57-
Properties.LONG.of(103),
58-
Properties.FLOAT.of(104.4f),
59-
Properties.DOUBLE.of(105.5),
60-
Properties.CHAR.of('Z'),
52+
Properties.Bool.of(false),
53+
Properties.Str.of("foo"),
54+
Properties.Byte.of(100: Byte),
55+
Properties.Short.of(101: Short),
56+
Properties.Int.of(102),
57+
Properties.Long.of(103),
58+
Properties.Float.of(104.4f),
59+
Properties.Double.of(105.5),
60+
Properties.Char.of('Z'),
6161
)
6262
properties.foreach(node1.setProperty)
6363
properties.foreach(edge1.setProperty)

0 commit comments

Comments
 (0)