Skip to content

Commit

Permalink
optimise usings for csharp generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Jul 31, 2024
1 parent 9724c14 commit 1ed0411
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ fun AnyType.deprecated() : Boolean {
val anno = this.getAnnotation("deprecated")
return (anno != null && (anno.value as BooleanInstance).value)
}

fun AnyType.markDeprecated() : Boolean {
val anno = this.getAnnotation("markDeprecated")
return (anno != null && (anno.value as BooleanInstance).value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package io.vrap.codegen.languages.csharp.extensions

import io.vrap.codegen.languages.extensions.*
import io.vrap.rmf.codegen.firstUpperCase
import io.vrap.rmf.codegen.types.VrapArrayType
import io.vrap.rmf.codegen.types.VrapEnumType
import io.vrap.rmf.codegen.types.VrapObjectType
import io.vrap.rmf.codegen.types.VrapType
import io.vrap.rmf.codegen.types.*
import io.vrap.rmf.raml.model.types.ArrayType
import io.vrap.rmf.raml.model.types.BooleanInstance
import io.vrap.rmf.raml.model.types.DateOnlyType
import io.vrap.rmf.raml.model.types.DateTimeType
import io.vrap.rmf.raml.model.types.ObjectType
import io.vrap.rmf.raml.model.types.Property
import io.vrap.codegen.languages.extensions.EObjectExtensions

const val ANNOTATION_ABSTRACT = "abstract"

Expand All @@ -32,27 +33,52 @@ interface CsharpObjectTypeExtensions : ExtensionsBase {
return usingsList
}

fun ObjectType.usings() : String {
fun ObjectType.usings(provider: VrapTypeProvider, isInterface: Boolean = false, isDictionary: Boolean = false) : String {
var usingsAsList = this.getUsings()
val vrapType = vrapTypeProvider.doSwitch(this) as VrapObjectType
var packageName = vrapType.`package`
usingsAsList = usingsAsList.dropLastWhile { it== packageName }
usingsAsList = usingsAsList.filterNot { it == vrapType.csharpPackage() }
var usings= usingsAsList.map { "using $it;" }.joinToString(separator = "\n")
var commonUsings = this.getCommonUsings()
var commonUsings = this.getCommonUsings(provider, isInterface, isDictionary)

var allusings = if(usings.isNotEmpty()) usings +"\n"+ commonUsings else usings+ commonUsings

return allusings
}

fun ObjectType.getCommonUsings() : String {
return """using System;
|using System.Collections.Generic;
|using System.Linq;
|using System.Text.Json.Serialization;
|using commercetools.Base.CustomAttributes;
|using commercetools.Base.Models;
|"""
// fun ObjectType.getCommonUsings() : String {
// return """using System;
// |using System.Collections.Generic;
// |using System.Linq;
// |using System.Text.Json.Serialization;
// |using commercetools.Base.CustomAttributes;
// |using commercetools.Base.Models;
// |"""
// }

fun ObjectType.getCommonUsings(provider: VrapTypeProvider, isInterface: Boolean = false, isDictionary: Boolean = false) : String {

var usings = listOf<String>()
val props = this.allProperties.map { provider.doSwitch(it.type) }
if (props.any { it.simpleName() == "Object" || it.simpleName() == "DateTime" || it.simpleName() == "TimeSpan" } || this.allProperties.any { it.markDeprecated() } || this.markDeprecated()) {
usings = usings.plus("System")
}
if (isDictionary || props.any { it is VrapArrayType }) {
usings = usings.plus("System.Collections.Generic")
.plus("System.Linq")
}
if (props.any { it is VrapArrayType && (it.itemType.simpleName() == "Object" || it.itemType.simpleName() == "DateTime" || it.itemType.simpleName() == "TimeSpan") }) {
usings = usings.plus("System")
}
if (props.any { it.simpleName() == "Date" }) {
usings = usings.plus("commercetools.Base.Models")
}
if (isInterface || this.discriminator.isNullOrEmpty().not() || (this.isInlineType && (this.type as ObjectType).discriminator.isNullOrEmpty().not())) {
usings = usings.plus("commercetools.Base.CustomAttributes")
.plus("System")
}

return usings.distinct().joinToString("\n") { "using $it;" }
}

public fun ObjectType.objectClassName(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,12 @@ fun Property.deprecated() : Boolean {
return (typeAnno != null && (typeAnno.value as BooleanInstance).value)
}

fun Property.markDeprecated() : Boolean {
val anno = this.getAnnotation("markDeprecated")
if (anno != null) {
return (anno.value as BooleanInstance).value
}
val typeAnno = this.type.getAnnotation("markDeprecated")
return (typeAnno != null && (typeAnno.value as BooleanInstance).value)
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.vrap.rmf.codegen.di.BasePackageName
import io.vrap.rmf.codegen.io.TemplateFile
import io.vrap.rmf.codegen.rendering.ObjectTypeRenderer
import io.vrap.rmf.codegen.rendering.utils.escapeAll
import io.vrap.rmf.codegen.rendering.utils.keepAngleIndent
import io.vrap.rmf.codegen.rendering.utils.keepIndentation
import io.vrap.rmf.codegen.types.VrapArrayType
import io.vrap.rmf.codegen.types.VrapObjectType
Expand All @@ -36,20 +37,20 @@ class CsharpModelInterfaceRenderer constructor(override val vrapTypeProvider: Vr
).filterNotNull()

var content : String = """
|${type.usings()}
|${type.usings(vrapTypeProvider, true)}
|// ReSharper disable CheckNamespace
|namespace ${vrapType.csharpPackage()}
|{
| <${type.DeserializationAttributes()}>
| public partial interface I${vrapType.simpleClassName} ${if (extends.isNotEmpty()) { ": ${extends.joinToString(separator = ", ")}" } else ""}
| public partial interface I${vrapType.simpleClassName}${if (extends.isNotEmpty()) { " : ${extends.joinToString(separator = ", ")}" } else ""}
| {
| ${type.toProperties(" ")}
|
| <${type.toProperties()}>
|
| <${type.subtypeFactories()}>
| }
|}
|
""".trimMargin().keepIndentation()
""".trimMargin().keepIndentation().split("\n").joinToString(separator = "\n") { it.trimEnd() }


if(type.isADictionaryType())
Expand All @@ -68,9 +69,9 @@ class CsharpModelInterfaceRenderer constructor(override val vrapTypeProvider: Vr
private fun ObjectType.toProperties(indent: String = "") : String = this.properties
.filterNot { it.deprecated() }
.filterNot { property -> property.isPatternProperty() }
.map { it.toCsharpProperty(this) }.joinToString(separator = "\n\n$indent")
.map { it.toCsharpProperty(this) }.joinToString(separator = "\n\n")

private fun Property.toCsharpProperty(objectType: ObjectType): String {
private fun Property.toCsharpProperty(objectType: ObjectType, indent: String = ""): String {
val propName = this.name.firstUpperCase()
val typeName = this.type.toVrapType().simpleName()
val overrideProp = this.shouldOverrideThisProperty(objectType)
Expand All @@ -81,8 +82,8 @@ class CsharpModelInterfaceRenderer constructor(override val vrapTypeProvider: Vr

return """
|${deprecationAttr}${newKeyword}${typeName}$nullableChar $propName { get; set; }${if (this.type.toVrapType() is VrapArrayType) """
|${deprecationAttr}${newKeyword}IEnumerable\<${(this.type.toVrapType() as VrapArrayType).itemType.simpleName()}\>$nullableChar ${propName}Enumerable { set =\> $propName = value$nullableChar.ToList(); }
|""" else ""}
|
|${deprecationAttr}${newKeyword}IEnumerable\<${(this.type.toVrapType() as VrapArrayType).itemType.simpleName()}\>$nullableChar ${propName}Enumerable { set =\> $propName = value$nullableChar.ToList(); }""" else ""}
""".trimMargin()
}

Expand Down Expand Up @@ -114,7 +115,7 @@ class CsharpModelInterfaceRenderer constructor(override val vrapTypeProvider: Vr
val property = this.properties[0]

return """
|${this.usings()}
|${this.usings(vrapTypeProvider, true, true)}
|
|namespace ${vrapType.csharpPackage()}
|{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ class CsharpObjectTypeRenderer constructor(override val vrapTypeProvider: VrapTy
val vrapType = vrapTypeProvider.doSwitch(type) as VrapObjectType

var content : String = """
|${type.usings()}
|${type.usings(vrapTypeProvider)}
|
|namespace ${vrapType.csharpPackage()}
|{
| ${if (type.markDeprecated()) "[Obsolete(\"usage of this endpoint has been deprecated.\", false)]" else ""}
| public partial class ${type.objectClassName()} : I${vrapType.simpleClassName}
| {
| ${if(type.isADictionaryType()) "" else type.toProperties(" ")}
| <${if(type.isADictionaryType()) "" else type.toProperties()}>
| <${type.renderConstructor(vrapType.simpleClassName)}>
| }
|}
|
""".trimMargin().keepIndentation()
""".trimMargin().keepIndentation().split("\n").joinToString(separator = "\n") { it.trimEnd() }

if(type.isADictionaryType())
{
Expand All @@ -56,7 +56,7 @@ class CsharpObjectTypeRenderer constructor(override val vrapTypeProvider: VrapTy
var property = this.properties[0]

return """
|${this.usings()}
|${this.usings(vrapTypeProvider, false, true)}
|
|// ReSharper disable CheckNamespace
|namespace ${vrapType.csharpPackage()}
Expand All @@ -70,10 +70,10 @@ class CsharpObjectTypeRenderer constructor(override val vrapTypeProvider: VrapTy
"""
}

private fun ObjectType.toProperties(indent: String = "") : String = this.allProperties
private fun ObjectType.toProperties() : String = this.allProperties
.filterNot { it.deprecated() }
.filterNot { property -> property.isPatternProperty() }
.map { it.toCsharpProperty(this) }.joinToString(separator = "\n\n$indent")
.map { it.toCsharpProperty(this) }.joinToString(separator = "\n\n")

private fun Property.toCsharpProperty(objectType: ObjectType): String {
val propName = this.name.firstUpperCase()
Expand All @@ -84,8 +84,8 @@ class CsharpObjectTypeRenderer constructor(override val vrapTypeProvider: VrapTy

return """
|${deprecationAttr}public ${typeName}$nullableChar $propName { get; set; }${if (this.type.toVrapType() is VrapArrayType) """
|${deprecationAttr}public IEnumerable\<${(this.type.toVrapType() as VrapArrayType).itemType.simpleName()}\>$nullableChar ${propName}Enumerable { set =\> $propName = value$nullableChar.ToList(); }
|""" else ""}
|
|${deprecationAttr}public IEnumerable\<${(this.type.toVrapType() as VrapArrayType).itemType.simpleName()}\>$nullableChar ${propName}Enumerable { set =\> $propName = value$nullableChar.ToList(); }""" else ""}
""".trimMargin()
}

Expand Down

0 comments on commit 1ed0411

Please sign in to comment.