Skip to content

Commit 1b07d78

Browse files
committed
use scala primitives (e.g. Integer -> Int)
motivation: minify diff for flatgraph migration
1 parent 5c45082 commit 1b07d78

File tree

5 files changed

+40
-37
lines changed

5 files changed

+40
-37
lines changed

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

+22-19
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ class CodeGen(schema: Schema) {
343343
properties.map { property =>
344344
val name = property.name
345345
val nameCamelCase = camelCase(name)
346-
val tpe = getCompleteType(property)
346+
val tpe = getCompleteType(property, nullable = false)
347347

348348
property.cardinality match {
349349
case Cardinality.One(_) =>
@@ -479,7 +479,7 @@ class CodeGen(schema: Schema) {
479479
def generateNodeBaseTypeSource(nodeBaseType: NodeBaseType): String = {
480480
val className = nodeBaseType.className
481481
val properties = nodeBaseType.properties
482-
val propertyAccessors = Helpers.propertyAccessors(properties)
482+
val propertyAccessors = Helpers.propertyAccessors(properties, nullable = false)
483483

484484
val mixinsForBaseTypes = nodeBaseType.extendz.map { baseTrait =>
485485
s"with ${baseTrait.className}"
@@ -601,7 +601,7 @@ class CodeGen(schema: Schema) {
601601

602602
val newNodePropertySetters = nodeBaseType.properties.map { property =>
603603
val camelCaseName = camelCase(property.name)
604-
val tpe = getCompleteType(property)
604+
val tpe = getCompleteType(property, nullable = false)
605605
s"def ${camelCaseName}_=(value: $tpe): Unit"
606606
}.mkString(lineSeparator)
607607

@@ -856,7 +856,7 @@ class CodeGen(schema: Schema) {
856856
case Cardinality.One(_) =>
857857
s"""this._$memberName = $newNodeCasted.$memberName""".stripMargin
858858
case Cardinality.ZeroOrOne =>
859-
s"""this._$memberName = $newNodeCasted.$memberName.orNull""".stripMargin
859+
s"""this._$memberName = $newNodeCasted.$memberName match { case None => null; case Some(value) => value }""".stripMargin
860860
case Cardinality.List =>
861861
s"""this._$memberName = if ($newNodeCasted.$memberName != null) $newNodeCasted.$memberName else collection.immutable.ArraySeq.empty""".stripMargin
862862
}
@@ -993,7 +993,7 @@ class CodeGen(schema: Schema) {
993993
s"""trait ${className}Base extends AbstractNode $mixinsForExtendedNodesBase $mixinsForMarkerTraits {
994994
| def asStored : StoredNode = this.asInstanceOf[StoredNode]
995995
|
996-
| ${Helpers.propertyAccessors(properties)}
996+
| ${Helpers.propertyAccessors(properties, nullable = false)}
997997
|
998998
| $abstractContainedNodeAccessors
999999
|}
@@ -1021,7 +1021,7 @@ class CodeGen(schema: Schema) {
10211021
val nodeRefImpl = {
10221022
val propertyDelegators = properties.map { key =>
10231023
val name = camelCase(key.name)
1024-
s"""override def $name: ${getCompleteType(key)} = get().$name"""
1024+
s"""override def $name: ${getCompleteType(key, nullable = false)} = get().$name"""
10251025
}.mkString(lineSeparator)
10261026

10271027
val propertyDefaultValues = propertyDefaultValueImpl(s"$className.PropertyDefaults", properties)
@@ -1095,14 +1095,14 @@ class CodeGen(schema: Schema) {
10951095

10961096
val updateSpecificPropertyImpl: String = {
10971097
import Property.Cardinality
1098-
def caseEntry(name: String, accessorName: String, cardinality: Cardinality, baseType: String) = {
1098+
def caseEntry(name: String, accessorName: String, cardinality: Cardinality, baseType: String, baseTypeNullable: String) = {
10991099
val setter = cardinality match {
11001100
case Cardinality.One(_) | Cardinality.ZeroOrOne =>
11011101
s"value.asInstanceOf[$baseType]"
11021102
case Cardinality.List =>
11031103
s"""value match {
11041104
| case null => collection.immutable.ArraySeq.empty
1105-
| case singleValue: $baseType => collection.immutable.ArraySeq(singleValue)
1105+
| case singleValue: $baseTypeNullable => collection.immutable.ArraySeq(singleValue)
11061106
| case coll: IterableOnce[Any] if coll.iterator.isEmpty => collection.immutable.ArraySeq.empty
11071107
| case arr: Array[_] if arr.isEmpty => collection.immutable.ArraySeq.empty
11081108
| case arr: Array[_] => collection.immutable.ArraySeq.unsafeWrapArray(arr).asInstanceOf[IndexedSeq[$baseType]]
@@ -1120,10 +1120,10 @@ class CodeGen(schema: Schema) {
11201120
s"""|case "$name" => this._$accessorName = $setter"""
11211121
}
11221122

1123-
val forKeys = properties.map(p => caseEntry(p.name, camelCase(p.name), p.cardinality, typeFor(p))).mkString(lineSeparator)
1123+
val forKeys = properties.map(p => caseEntry(p.name, camelCase(p.name), p.cardinality, typeFor(p), typeFor(p, nullable = true))).mkString(lineSeparator)
11241124

11251125
val forContainedNodes = nodeType.containedNodes.map(containedNode =>
1126-
caseEntry(containedNode.localName, containedNode.localName, containedNode.cardinality, containedNode.classNameForStoredNode)
1126+
caseEntry(containedNode.localName, containedNode.localName, containedNode.cardinality, containedNode.classNameForStoredNode, containedNode.classNameForStoredNode)
11271127
).mkString(lineSeparator)
11281128

11291129
s"""override protected def updateSpecificProperty(key:String, value: Object): Unit = {
@@ -1161,10 +1161,11 @@ class CodeGen(schema: Schema) {
11611161
val fieldName = s"_$publicName"
11621162
val (publicType, tpeForField, fieldAccessor, defaultValue) = {
11631163
val valueType = typeFor(property)
1164+
val valueTypeNullable = typeFor(property, nullable = true)
11641165
property.cardinality match {
11651166
case Cardinality.One(_) =>
1166-
(valueType, valueType, fieldName, s"$className.PropertyDefaults.${property.className}")
1167-
case Cardinality.ZeroOrOne => (s"Option[$valueType]", valueType, s"Option($fieldName)", "null")
1167+
(valueType, valueTypeNullable, fieldName, s"$className.PropertyDefaults.${property.className}")
1168+
case Cardinality.ZeroOrOne => (s"Option[$valueType]", valueTypeNullable, s"Option($fieldName).asInstanceOf[Option[$valueType]]", "null")
11681169
case Cardinality.List => (s"IndexedSeq[$valueType]", s"IndexedSeq[$valueType]", fieldName, "collection.immutable.ArraySeq.empty")
11691170
}
11701171
}
@@ -1732,27 +1733,29 @@ class CodeGen(schema: Schema) {
17321733

17331734
def generateNewNodeSource(nodeType: NodeType, properties: Seq[Property[?]], inEdges: Map[String, Set[String]], outEdges: Map[String, Set[String]]) = {
17341735
import Property.Cardinality
1735-
case class FieldDescription(name: String, valueType: String, fullType: String, cardinality: Cardinality)
1736+
case class FieldDescription(name: String, valueType: String, valueTypeNullable: String, fullType: String, cardinality: Cardinality)
17361737
val fieldDescriptions = mutable.ArrayBuffer.empty[FieldDescription]
17371738

17381739
for (property <- properties) {
17391740
fieldDescriptions += FieldDescription(
17401741
camelCase(property.name),
17411742
typeFor(property),
1742-
getCompleteType(property),
1743+
typeFor(property, nullable = true),
1744+
getCompleteType(property, nullable = false),
17431745
property.cardinality)
17441746
}
17451747

17461748
for (containedNode <- nodeType.containedNodes) {
17471749
fieldDescriptions += FieldDescription(
17481750
containedNode.localName,
17491751
typeFor(containedNode),
1752+
typeFor(containedNode),
17501753
getCompleteType(containedNode),
17511754
containedNode.cardinality)
17521755
}
17531756

17541757
val memberVariables = fieldDescriptions.reverse.map {
1755-
case FieldDescription(name, _, fullType, cardinality) =>
1758+
case FieldDescription(name, _, _, fullType, cardinality) =>
17561759
val defaultValue = cardinality match {
17571760
case Cardinality.One(default) => defaultValueImpl(default)
17581761
case Cardinality.ZeroOrOne => "None"
@@ -1812,22 +1815,22 @@ class CodeGen(schema: Schema) {
18121815
val mixins = nodeType.extendz.map{baseType => s"with ${baseType.className}New"}.mkString(" ")
18131816

18141817
val propertySettersImpl = fieldDescriptions.map {
1815-
case FieldDescription(name, valueType , _, cardinality) =>
1818+
case FieldDescription(name, valueType, valueTypeNullable, _, cardinality) =>
18161819
cardinality match {
18171820
case Cardinality.One(_) =>
1818-
s"""def $name(value: $valueType): this.type = {
1821+
s"""def $name(value: $valueTypeNullable): this.type = {
18191822
| this.$name = value
18201823
| this
18211824
|}
18221825
|""".stripMargin
18231826

18241827
case Cardinality.ZeroOrOne =>
1825-
s"""def $name(value: $valueType): this.type = {
1828+
s"""def $name(value: $valueTypeNullable): this.type = {
18261829
| this.$name = Option(value)
18271830
| this
18281831
|}
18291832
|
1830-
|def $name(value: Option[$valueType]): this.type = $name(value.orNull)
1833+
|def $name(value: Option[$valueType]): this.type = $name(value match { case None => null; case Some(value) => value: $valueTypeNullable })
18311834
|""".stripMargin
18321835

18331836
case Cardinality.List =>

codegen/src/main/scala/overflowdb/codegen/Helpers.scala

+13-14
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,17 @@ object Helpers {
3232
case nonEmptyString => Some(nonEmptyString)
3333
}
3434

35-
def typeFor[A](property: Property[A]): String = {
36-
val isMandatory = property.isMandatory
35+
def typeFor[A](property: Property[A], nullable: Boolean = false): String = {
3736
property.valueType match {
38-
case ValueType.Boolean => if (isMandatory) "Boolean" else "java.lang.Boolean"
37+
case ValueType.Boolean => if (nullable) "java.lang.Boolean" else "Boolean"
3938
case ValueType.String => "String"
40-
case ValueType.Byte => if (isMandatory) "Byte" else "java.lang.Byte"
41-
case ValueType.Short => if (isMandatory) "Short" else "java.lang.Short"
42-
case ValueType.Int => if (isMandatory) "scala.Int" else "Integer"
43-
case ValueType.Long => if (isMandatory) "Long" else "java.lang.Long"
44-
case ValueType.Float => if (isMandatory) "Float" else "java.lang.Float"
45-
case ValueType.Double => if (isMandatory) "Double" else "java.lang.Double"
46-
case ValueType.Char => if (isMandatory) "scala.Char" else "Character"
39+
case ValueType.Byte => if (nullable) "java.lang.Byte" else "Byte"
40+
case ValueType.Short => if (nullable) "java.lang.Short" else "Short"
41+
case ValueType.Int => if (nullable) "Integer" else "scala.Int"
42+
case ValueType.Long => if (nullable) "java.lang.Long" else "Long"
43+
case ValueType.Float => if (nullable) "java.lang.Float" else "Float"
44+
case ValueType.Double => if (nullable) "java.lang.Double" else "Double"
45+
case ValueType.Char => if (nullable) "Character" else "scala.Char"
4746
case ValueType.List => "Seq[_]"
4847
case ValueType.NodeRef => "overflowdb.NodeRef[_]"
4948
case ValueType.Unknown => "java.lang.Object"
@@ -120,8 +119,8 @@ object Helpers {
120119
}
121120
}
122121

123-
def getCompleteType[A](property: Property[?]): String =
124-
getCompleteType(property.cardinality, typeFor(property))
122+
def getCompleteType[A](property: Property[?], nullable: Boolean = true): String =
123+
getCompleteType(property.cardinality, typeFor(property, nullable))
125124

126125
def typeFor(containedNode: ContainedNode): String = {
127126
containedNode.nodeType match {
@@ -200,10 +199,10 @@ object Helpers {
200199
}.mkString(s"$lineSeparator| ")
201200
}
202201

203-
def propertyAccessors(properties: Seq[Property[?]]): String = {
202+
def propertyAccessors(properties: Seq[Property[?]], nullable: Boolean = true): String = {
204203
properties.map { property =>
205204
val camelCaseName = camelCase(property.name)
206-
val tpe = getCompleteType(property)
205+
val tpe = getCompleteType(property, nullable = nullable)
207206
s"def $camelCaseName: $tpe"
208207
}.mkString(lineSeparator)
209208
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import testschema01.nodes._
66
import testschema01.edges._
77
import testschema01.traversal._
88
import scala.jdk.CollectionConverters.IteratorHasAsScala
9+
910
class Schema01Test extends AnyWordSpec with Matchers {
1011
import testschema01.traversal._
1112
"constants" in {
@@ -58,7 +59,7 @@ class Schema01Test extends AnyWordSpec with Matchers {
5859
val edge2Specific = edge2.asInstanceOf[Edge2]
5960
val name: String = node1aSpecific.name
6061
name shouldBe "node 1a"
61-
val o1: Option[Integer] = node1aSpecific.order
62+
val o1: Option[Int] = node1aSpecific.order
6263
node1aSpecific.order shouldBe Some(2)
6364
node1bSpecific.order shouldBe None
6465
val o2: Seq[String] = node2aSpecific.options
@@ -97,7 +98,7 @@ class Schema01Test extends AnyWordSpec with Matchers {
9798
.name("name1")
9899
.node3(node3)
99100
.options(Seq("one", "two", "three"))
100-
.placements(Seq(1,2,3): Seq[Integer])
101+
.placements(Seq(1,2,3))
101102

102103
val builder = new BatchedUpdate.DiffGraphBuilder
103104
builder.addNode(newNode2)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Schema02Test extends AnyWordSpec with Matchers {
5555
copy.order(3)
5656
copy.order shouldBe Some(3)
5757

58-
copy.order(Some(4: Integer))
58+
copy.order(Some(4))
5959
copy.order shouldBe Some(4)
6060

6161
original.name shouldBe "A"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class Schema04Test extends AnyWordSpec with Matchers {
175175
val node1 = NewNode1()
176176
.bool(true)
177177
.str("foo")
178-
.intList(Seq(1,2,3): Seq[Integer])
178+
.intList(Seq(1,2,3))
179179

180180
val node2 = NewNode1().node1Inner(node1)
181181

0 commit comments

Comments
 (0)