Skip to content

Commit fd18158

Browse files
committed
add factory methods for subtypes
1 parent 780ce37 commit fd18158

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

languages/csharp/src/main/kotlin/io/vrap/codegen/languages/csharp/model/CsharpModelInterfaceRenderer.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class CsharpModelInterfaceRenderer constructor(override val vrapTypeProvider: Vr
4343
| public partial interface I${vrapType.simpleClassName} ${if (extends.isNotEmpty()) { ": ${extends.joinToString(separator = ", ")}" } else ""}
4444
| {
4545
| <${type.toProperties()}>
46+
|
47+
| <${type.subtypeFactories()}>
4648
| }
4749
|}
4850
|
@@ -122,6 +124,33 @@ class CsharpModelInterfaceRenderer constructor(override val vrapTypeProvider: Vr
122124
"""
123125
}
124126

127+
private fun ObjectType.subtypeFactories(): String {
128+
return if (this.hasSubtypes())
129+
"""
130+
|<${this.subTypes.plus(this.subTypes.flatMap { it.subTypes }).distinctBy { it.name }
131+
.asSequence()
132+
.filterIsInstance<ObjectType>()
133+
.filter { it.getAnnotation("deprecated") == null }
134+
.filter { it.discriminatorValue != null }
135+
.sortedBy { anyType -> anyType.discriminatorValue.lowercase(Locale.getDefault()) }
136+
.map { it.subtypeFactory() }
137+
.joinToString(separator = "\n")}>
138+
""".trimMargin()
139+
else ""
140+
}
141+
142+
private fun ObjectType.subtypeFactory(): String {
143+
val vrapType = vrapTypeProvider.doSwitch(this) as VrapObjectType
144+
val className = "${vrapType.`package`.toCsharpPackage()}.${this.objectClassName()}"
145+
return """
146+
|static $className ${this.discriminatorValue.firstUpperCase()}(Action\<$className\> init = null) {
147+
| var t = new $className();
148+
| init?.Invoke(t);
149+
| return t;
150+
|}
151+
""".trimMargin()
152+
}
153+
125154
fun ObjectType.DeserializationAttributes(): String {
126155
val vrapType = vrapTypeProvider.doSwitch(this) as VrapObjectType
127156

0 commit comments

Comments
 (0)