Skip to content

Commit e30acb0

Browse files
committed
string properties return empty string if not set
To avoid null checks everywhere in the client code, the type of string properties is now `String` instead of `String?`. An additional '_raw_...' property is generated, like it is for bool/int, that is of type `String?`
1 parent 39e7a97 commit e30acb0

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

metamodel-generator/src/main/kotlin/org/modelix/metamodel/generator/MetaModelGenerator.kt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,11 @@ class MetaModelGenerator(val outputDir: Path) {
246246
.mutable(true)
247247
.delegate("""${accessorClass.qualifiedName}(unwrap(), "${feature.originalName}")""")
248248
.build())
249-
if (data.type != PropertyType.STRING) {
250-
addProperty(PropertySpec.builder("_raw_" + feature.validName, PropertyType.STRING.asKotlinType())
251-
.addModifiers(KModifier.OVERRIDE)
252-
.mutable(true)
253-
.delegate("""${StringPropertyAccessor::class.qualifiedName}(unwrap(), "${feature.originalName}")""")
254-
.build())
255-
}
249+
addProperty(PropertySpec.builder("_raw_" + feature.validName, String::class.asTypeName().copy(nullable = true))
250+
.addModifiers(KModifier.OVERRIDE)
251+
.mutable(true)
252+
.delegate("""${RawPropertyAccessor::class.qualifiedName}(unwrap(), "${feature.originalName}")""")
253+
.build())
256254
}
257255
is ChildLinkData -> {
258256
// TODO resolve link.type and ensure it exists
@@ -290,11 +288,9 @@ class MetaModelGenerator(val outputDir: Path) {
290288
addProperty(PropertySpec.builder(feature.validName, data.type.asKotlinType())
291289
.mutable(true)
292290
.build())
293-
if (data.type != PropertyType.STRING) {
294-
addProperty(PropertySpec.builder("_raw_" + feature.validName, PropertyType.STRING.asKotlinType())
295-
.mutable(true)
296-
.build())
297-
}
291+
addProperty(PropertySpec.builder("_raw_" + feature.validName, String::class.asTypeName().copy(nullable = true))
292+
.mutable(true)
293+
.build())
298294
}
299295
is ChildLinkData -> {
300296
// TODO resolve link.type and ensure it exists
@@ -318,7 +314,7 @@ class MetaModelGenerator(val outputDir: Path) {
318314

319315
fun PropertyType.asKotlinType(): TypeName {
320316
return when (this) {
321-
PropertyType.STRING -> String::class.asTypeName().copy(nullable = true)
317+
PropertyType.STRING -> String::class.asTypeName()
322318
PropertyType.BOOLEAN -> Boolean::class.asTypeName()
323319
PropertyType.INT -> Int::class.asTypeName()
324320
}

metamodel-runtime/src/commonMain/kotlin/org/modelix/metamodel/PropertyAccessor.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ abstract class PropertyAccessor<ValueT>(val node: INode, val role: String) {
1616
abstract fun convertWrite(value: ValueT): String?
1717
}
1818

19-
class StringPropertyAccessor(node: INode, role: String) : PropertyAccessor<String?>(node, role) {
19+
class RawPropertyAccessor(node: INode, role: String) : PropertyAccessor<String?>(node, role) {
2020
override fun convertRead(value: String?): String? = value
2121
override fun convertWrite(value: String?): String? = value
2222
}
2323

24+
class StringPropertyAccessor(node: INode, role: String) : PropertyAccessor<String>(node, role) {
25+
override fun convertRead(value: String?): String = value ?: ""
26+
override fun convertWrite(value: String): String? = value
27+
}
28+
2429
class BooleanPropertyAccessor(node: INode, role: String) : PropertyAccessor<Boolean>(node, role) {
2530
override fun convertRead(value: String?): Boolean = value == "true"
2631
override fun convertWrite(value: Boolean): String? = if (value) "true" else "false"

0 commit comments

Comments
 (0)