Skip to content

Commit 91bddb3

Browse files
committed
generate boolean and int properties to kotlin
1 parent 1684b99 commit 91bddb3

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,23 @@ class MetaModelGenerator(val outputDir: Path) {
236236
for (feature in concept.directFeaturesAndConflicts()) {
237237
when (val data = feature.data) {
238238
is PropertyData -> {
239-
val optionalString = String::class.asTypeName().copy(nullable = true)
240-
addProperty(PropertySpec.builder(feature.validName, optionalString)
239+
val accessorClass = when (data.type) {
240+
PropertyType.STRING -> StringPropertyAccessor::class
241+
PropertyType.BOOLEAN -> BooleanPropertyAccessor::class
242+
PropertyType.INT -> IntPropertyAccessor::class
243+
}
244+
addProperty(PropertySpec.builder(feature.validName, data.type.asKotlinType())
241245
.addModifiers(KModifier.OVERRIDE)
242246
.mutable(true)
243-
.delegate("""${PropertyAccessor::class.qualifiedName}(unwrap(), "${feature.originalName}")""")
247+
.delegate("""${accessorClass.qualifiedName}(unwrap(), "${feature.originalName}")""")
244248
.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+
}
245256
}
246257
is ChildLinkData -> {
247258
// TODO resolve link.type and ensure it exists
@@ -276,10 +287,14 @@ class MetaModelGenerator(val outputDir: Path) {
276287
for (feature in concept.directFeatures()) {
277288
when (val data = feature.data) {
278289
is PropertyData -> {
279-
val optionalString = String::class.asTypeName().copy(nullable = true)
280-
addProperty(PropertySpec.builder(feature.validName, optionalString)
290+
addProperty(PropertySpec.builder(feature.validName, data.type.asKotlinType())
281291
.mutable(true)
282292
.build())
293+
if (data.type != PropertyType.STRING) {
294+
addProperty(PropertySpec.builder("_raw_" + feature.validName, PropertyType.STRING.asKotlinType())
295+
.mutable(true)
296+
.build())
297+
}
283298
}
284299
is ChildLinkData -> {
285300
// TODO resolve link.type and ensure it exists
@@ -301,6 +316,13 @@ class MetaModelGenerator(val outputDir: Path) {
301316
}
302317
}
303318

319+
fun PropertyType.asKotlinType(): TypeName {
320+
return when (this) {
321+
PropertyType.STRING -> String::class.asTypeName().copy(nullable = true)
322+
PropertyType.BOOLEAN -> Boolean::class.asTypeName()
323+
PropertyType.INT -> Int::class.asTypeName()
324+
}
325+
}
304326
fun ConceptRef.conceptWrapperImplType() = ClassName(languageName, conceptName.conceptWrapperImplName())
305327
fun ConceptRef.conceptWrapperInterfaceType() = ClassName(languageName, conceptName.conceptWrapperInterfaceName())
306328
fun ConceptRef.nodeWrapperImplType() = ClassName(languageName, conceptName.nodeWrapperImplName())

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,30 @@ package org.modelix.metamodel
33
import org.modelix.model.api.INode
44
import kotlin.reflect.KProperty
55

6-
class PropertyAccessor(val node: INode, val role: String) {
7-
operator fun getValue(thisRef: Any, property: KProperty<*>): String? {
8-
return node.getPropertyValue(role)
6+
abstract class PropertyAccessor<ValueT>(val node: INode, val role: String) {
7+
operator fun getValue(thisRef: Any, property: KProperty<*>): ValueT {
8+
return convertRead(node.getPropertyValue(role))
99
}
1010

11-
operator fun setValue(thisRef: Any, property: KProperty<*>, value: String?) {
12-
node.setPropertyValue(role, value)
11+
operator fun setValue(thisRef: Any, property: KProperty<*>, value: ValueT) {
12+
node.setPropertyValue(role, convertWrite(value))
1313
}
14+
15+
abstract fun convertRead(value: String?): ValueT
16+
abstract fun convertWrite(value: ValueT): String?
17+
}
18+
19+
class StringPropertyAccessor(node: INode, role: String) : PropertyAccessor<String?>(node, role) {
20+
override fun convertRead(value: String?): String? = value
21+
override fun convertWrite(value: String?): String? = value
22+
}
23+
24+
class BooleanPropertyAccessor(node: INode, role: String) : PropertyAccessor<Boolean>(node, role) {
25+
override fun convertRead(value: String?): Boolean = value == "true"
26+
override fun convertWrite(value: Boolean): String? = if (value) "true" else "false"
27+
}
28+
29+
class IntPropertyAccessor(node: INode, role: String) : PropertyAccessor<Int>(node, role) {
30+
override fun convertRead(value: String?): Int = value?.toInt() ?: 0
31+
override fun convertWrite(value: Int): String? = value.toString()
1432
}

0 commit comments

Comments
 (0)