@@ -254,20 +254,26 @@ class CodeGen(schema: Schema) {
254
254
})
255
255
}
256
256
257
- writeConstantsFile(" Properties" , schema.properties.map { property =>
258
- val src = {
257
+ // Properties.scala
258
+ val propertyKeysConstantsSource = {
259
+ schema.properties.map { property =>
259
260
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] "
266
265
}
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
268
267
}
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)
271
277
272
278
results.toSeq
273
279
}
@@ -343,7 +349,7 @@ class CodeGen(schema: Schema) {
343
349
properties.map { property =>
344
350
val name = property.name
345
351
val nameCamelCase = camelCase(name)
346
- val tpe = getCompleteType(property)
352
+ val tpe = getCompleteType(property, nullable = false )
347
353
348
354
property.cardinality match {
349
355
case Cardinality .One (_) =>
@@ -479,7 +485,7 @@ class CodeGen(schema: Schema) {
479
485
def generateNodeBaseTypeSource (nodeBaseType : NodeBaseType ): String = {
480
486
val className = nodeBaseType.className
481
487
val properties = nodeBaseType.properties
482
- val propertyAccessors = Helpers .propertyAccessors(properties)
488
+ val propertyAccessors = Helpers .propertyAccessors(properties, nullable = false )
483
489
484
490
val mixinsForBaseTypes = nodeBaseType.extendz.map { baseTrait =>
485
491
s " with ${baseTrait.className}"
@@ -601,7 +607,7 @@ class CodeGen(schema: Schema) {
601
607
602
608
val newNodePropertySetters = nodeBaseType.properties.map { property =>
603
609
val camelCaseName = camelCase(property.name)
604
- val tpe = getCompleteType(property)
610
+ val tpe = getCompleteType(property, nullable = false )
605
611
s " def ${camelCaseName}_=(value: $tpe): Unit "
606
612
}.mkString(lineSeparator)
607
613
@@ -856,7 +862,7 @@ class CodeGen(schema: Schema) {
856
862
case Cardinality .One (_) =>
857
863
s """ this._ $memberName = $newNodeCasted. $memberName""" .stripMargin
858
864
case Cardinality .ZeroOrOne =>
859
- s """ this._ $memberName = $newNodeCasted. $memberName.orNull """ .stripMargin
865
+ s """ this._ $memberName = $newNodeCasted. $memberName match { case None => null; case Some(value) => value } """ .stripMargin
860
866
case Cardinality .List =>
861
867
s """ this._ $memberName = if ( $newNodeCasted. $memberName != null) $newNodeCasted. $memberName else collection.immutable.ArraySeq.empty """ .stripMargin
862
868
}
@@ -993,7 +999,7 @@ class CodeGen(schema: Schema) {
993
999
s """ trait ${className}Base extends AbstractNode $mixinsForExtendedNodesBase $mixinsForMarkerTraits {
994
1000
| def asStored : StoredNode = this.asInstanceOf[StoredNode]
995
1001
|
996
- | ${Helpers .propertyAccessors(properties)}
1002
+ | ${Helpers .propertyAccessors(properties, nullable = false )}
997
1003
|
998
1004
| $abstractContainedNodeAccessors
999
1005
|}
@@ -1021,7 +1027,7 @@ class CodeGen(schema: Schema) {
1021
1027
val nodeRefImpl = {
1022
1028
val propertyDelegators = properties.map { key =>
1023
1029
val name = camelCase(key.name)
1024
- s """ override def $name: ${getCompleteType(key)} = get(). $name"""
1030
+ s """ override def $name: ${getCompleteType(key, nullable = false )} = get(). $name"""
1025
1031
}.mkString(lineSeparator)
1026
1032
1027
1033
val propertyDefaultValues = propertyDefaultValueImpl(s " $className.PropertyDefaults " , properties)
@@ -1095,14 +1101,14 @@ class CodeGen(schema: Schema) {
1095
1101
1096
1102
val updateSpecificPropertyImpl : String = {
1097
1103
import Property .Cardinality
1098
- def caseEntry (name : String , accessorName : String , cardinality : Cardinality , baseType : String ) = {
1104
+ def caseEntry (name : String , accessorName : String , cardinality : Cardinality , baseType : String , baseTypeNullable : String ) = {
1099
1105
val setter = cardinality match {
1100
1106
case Cardinality .One (_) | Cardinality .ZeroOrOne =>
1101
1107
s " value.asInstanceOf[ $baseType] "
1102
1108
case Cardinality .List =>
1103
1109
s """ value match {
1104
1110
| case null => collection.immutable.ArraySeq.empty
1105
- | case singleValue: $baseType => collection.immutable.ArraySeq(singleValue)
1111
+ | case singleValue: $baseTypeNullable => collection.immutable.ArraySeq(singleValue)
1106
1112
| case coll: IterableOnce[Any] if coll.iterator.isEmpty => collection.immutable.ArraySeq.empty
1107
1113
| case arr: Array[_] if arr.isEmpty => collection.immutable.ArraySeq.empty
1108
1114
| case arr: Array[_] => collection.immutable.ArraySeq.unsafeWrapArray(arr).asInstanceOf[IndexedSeq[ $baseType]]
@@ -1120,10 +1126,10 @@ class CodeGen(schema: Schema) {
1120
1126
s """ |case " $name" => this._ $accessorName = $setter"""
1121
1127
}
1122
1128
1123
- val forKeys = properties.map(p => caseEntry(p.name, camelCase(p.name), p.cardinality, typeFor(p))).mkString(lineSeparator)
1129
+ val forKeys = properties.map(p => caseEntry(p.name, camelCase(p.name), p.cardinality, typeFor(p), typeFor(p, nullable = true ) )).mkString(lineSeparator)
1124
1130
1125
1131
val forContainedNodes = nodeType.containedNodes.map(containedNode =>
1126
- caseEntry(containedNode.localName, containedNode.localName, containedNode.cardinality, containedNode.classNameForStoredNode)
1132
+ caseEntry(containedNode.localName, containedNode.localName, containedNode.cardinality, containedNode.classNameForStoredNode, containedNode.classNameForStoredNode )
1127
1133
).mkString(lineSeparator)
1128
1134
1129
1135
s """ override protected def updateSpecificProperty(key:String, value: Object): Unit = {
@@ -1161,10 +1167,11 @@ class CodeGen(schema: Schema) {
1161
1167
val fieldName = s " _ $publicName"
1162
1168
val (publicType, tpeForField, fieldAccessor, defaultValue) = {
1163
1169
val valueType = typeFor(property)
1170
+ val valueTypeNullable = typeFor(property, nullable = true )
1164
1171
property.cardinality match {
1165
1172
case Cardinality .One (_) =>
1166
- (valueType, valueType , fieldName, s " $className.PropertyDefaults. ${property.className}" )
1167
- case Cardinality .ZeroOrOne => (s " Option[ $valueType] " , valueType , s " Option( $fieldName) " , " null" )
1173
+ (valueType, valueTypeNullable , fieldName, s " $className.PropertyDefaults. ${property.className}" )
1174
+ case Cardinality .ZeroOrOne => (s " Option[ $valueType] " , valueTypeNullable , s " Option( $fieldName).asInstanceOf[Option[ $valueType ]] " , " null" )
1168
1175
case Cardinality .List => (s " IndexedSeq[ $valueType] " , s " IndexedSeq[ $valueType] " , fieldName, " collection.immutable.ArraySeq.empty" )
1169
1176
}
1170
1177
}
@@ -1732,27 +1739,29 @@ class CodeGen(schema: Schema) {
1732
1739
1733
1740
def generateNewNodeSource (nodeType : NodeType , properties : Seq [Property [? ]], inEdges : Map [String , Set [String ]], outEdges : Map [String , Set [String ]]) = {
1734
1741
import Property .Cardinality
1735
- case class FieldDescription (name : String , valueType : String , fullType : String , cardinality : Cardinality )
1742
+ case class FieldDescription (name : String , valueType : String , valueTypeNullable : String , fullType : String , cardinality : Cardinality )
1736
1743
val fieldDescriptions = mutable.ArrayBuffer .empty[FieldDescription ]
1737
1744
1738
1745
for (property <- properties) {
1739
1746
fieldDescriptions += FieldDescription (
1740
1747
camelCase(property.name),
1741
1748
typeFor(property),
1742
- getCompleteType(property),
1749
+ typeFor(property, nullable = true ),
1750
+ getCompleteType(property, nullable = false ),
1743
1751
property.cardinality)
1744
1752
}
1745
1753
1746
1754
for (containedNode <- nodeType.containedNodes) {
1747
1755
fieldDescriptions += FieldDescription (
1748
1756
containedNode.localName,
1749
1757
typeFor(containedNode),
1758
+ typeFor(containedNode),
1750
1759
getCompleteType(containedNode),
1751
1760
containedNode.cardinality)
1752
1761
}
1753
1762
1754
1763
val memberVariables = fieldDescriptions.reverse.map {
1755
- case FieldDescription (name, _, fullType, cardinality) =>
1764
+ case FieldDescription (name, _, _, fullType, cardinality) =>
1756
1765
val defaultValue = cardinality match {
1757
1766
case Cardinality .One (default) => defaultValueImpl(default)
1758
1767
case Cardinality .ZeroOrOne => " None"
@@ -1812,22 +1821,22 @@ class CodeGen(schema: Schema) {
1812
1821
val mixins = nodeType.extendz.map{baseType => s " with ${baseType.className}New " }.mkString(" " )
1813
1822
1814
1823
val propertySettersImpl = fieldDescriptions.map {
1815
- case FieldDescription (name, valueType , _, cardinality) =>
1824
+ case FieldDescription (name, valueType, valueTypeNullable , _, cardinality) =>
1816
1825
cardinality match {
1817
1826
case Cardinality .One (_) =>
1818
- s """ def $name(value: $valueType ): this.type = {
1827
+ s """ def $name(value: $valueTypeNullable ): this.type = {
1819
1828
| this. $name = value
1820
1829
| this
1821
1830
|}
1822
1831
| """ .stripMargin
1823
1832
1824
1833
case Cardinality .ZeroOrOne =>
1825
- s """ def $name(value: $valueType ): this.type = {
1826
- | this. $name = Option(value)
1834
+ s """ def $name(value: $valueTypeNullable ): this.type = {
1835
+ | this. $name = Option(value).asInstanceOf[Option[ $valueType ]]
1827
1836
| this
1828
1837
|}
1829
1838
|
1830
- |def $name(value: Option[ $valueType]): this.type = $name(value.orNull )
1839
+ |def $name(value: Option[ $valueType]): this.type = $name(value match { case None => null; case Some(value) => value: $valueTypeNullable } )
1831
1840
| """ .stripMargin
1832
1841
1833
1842
case Cardinality .List =>
0 commit comments