[tree generator] Merge default and custom implementations
This commit is contained in:
committed by
Space Team
parent
665bd5153a
commit
d904ce0939
+1
-13
@@ -154,22 +154,10 @@ abstract class AbstractElement<Element, Field, Implementation>(
|
|||||||
val transformerClass: Element
|
val transformerClass: Element
|
||||||
get() = transformerReturnType ?: baseTransformerType ?: element
|
get() = transformerReturnType ?: baseTransformerType ?: element
|
||||||
|
|
||||||
var defaultImplementation: Implementation? = null
|
val implementations = mutableListOf<Implementation>()
|
||||||
|
|
||||||
val customImplementations = mutableListOf<Implementation>()
|
|
||||||
|
|
||||||
var doesNotNeedImplementation: Boolean = false
|
var doesNotNeedImplementation: Boolean = false
|
||||||
|
|
||||||
val allImplementations: List<Implementation> by lazy {
|
|
||||||
if (doesNotNeedImplementation) {
|
|
||||||
emptyList()
|
|
||||||
} else {
|
|
||||||
val implementations = customImplementations.toMutableList()
|
|
||||||
defaultImplementation?.let { implementations += it }
|
|
||||||
implementations
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Types/functions that you want to additionally import in the file with the element class.
|
* Types/functions that you want to additionally import in the file with the element class.
|
||||||
*
|
*
|
||||||
|
|||||||
+1
-8
@@ -17,9 +17,6 @@ abstract class AbstractImplementation<Implementation, Element, Field>(
|
|||||||
Element : AbstractElement<Element, *, Implementation>,
|
Element : AbstractElement<Element, *, Implementation>,
|
||||||
Field : AbstractField<*> {
|
Field : AbstractField<*> {
|
||||||
|
|
||||||
private val isDefault: Boolean
|
|
||||||
get() = name == null
|
|
||||||
|
|
||||||
override val allParents: List<ImplementationKindOwner>
|
override val allParents: List<ImplementationKindOwner>
|
||||||
get() = listOf(element)
|
get() = listOf(element)
|
||||||
|
|
||||||
@@ -53,11 +50,7 @@ abstract class AbstractImplementation<Implementation, Element, Field>(
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
if (isDefault) {
|
element.implementations += this as Implementation
|
||||||
element.defaultImplementation = this as Implementation
|
|
||||||
} else {
|
|
||||||
element.customImplementations += this as Implementation
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override val hasAcceptChildrenMethod: Boolean
|
override val hasAcceptChildrenMethod: Boolean
|
||||||
|
|||||||
+6
-6
@@ -85,10 +85,10 @@ abstract class AbstractBuilderConfigurator<Element, Implementation, BuilderField
|
|||||||
|
|
||||||
private fun Element.extractImplementation(type: String?): Implementation {
|
private fun Element.extractImplementation(type: String?): Implementation {
|
||||||
return if (type == null) {
|
return if (type == null) {
|
||||||
allImplementations.singleOrNull { it.kind?.hasLeafBuilder == true } ?: this@AbstractBuilderConfigurator.run {
|
implementations.singleOrNull { it.kind?.hasLeafBuilder == true } ?: this@AbstractBuilderConfigurator.run {
|
||||||
val message = buildString {
|
val message = buildString {
|
||||||
appendLine("${this@extractImplementation} has multiple implementations:")
|
appendLine("${this@extractImplementation} has multiple implementations:")
|
||||||
for (implementation in allImplementations) {
|
for (implementation in implementations) {
|
||||||
appendLine(" - ${implementation.typeName}")
|
appendLine(" - ${implementation.typeName}")
|
||||||
}
|
}
|
||||||
appendLine("Please specify implementation is needed")
|
appendLine("Please specify implementation is needed")
|
||||||
@@ -96,10 +96,10 @@ abstract class AbstractBuilderConfigurator<Element, Implementation, BuilderField
|
|||||||
throw IllegalArgumentException(message)
|
throw IllegalArgumentException(message)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
allImplementations.firstOrNull { it.typeName == type } ?: this@AbstractBuilderConfigurator.run {
|
implementations.firstOrNull { it.typeName == type } ?: this@AbstractBuilderConfigurator.run {
|
||||||
val message = buildString {
|
val message = buildString {
|
||||||
appendLine("${this@extractImplementation} has not implementation $type. Existing implementations:")
|
appendLine("${this@extractImplementation} has not implementation $type. Existing implementations:")
|
||||||
for (implementation in allImplementations) {
|
for (implementation in implementations) {
|
||||||
appendLine(" - ${implementation.typeName}")
|
appendLine(" - ${implementation.typeName}")
|
||||||
}
|
}
|
||||||
appendLine("Please specify implementation is needed")
|
appendLine("Please specify implementation is needed")
|
||||||
@@ -118,7 +118,7 @@ abstract class AbstractBuilderConfigurator<Element, Implementation, BuilderField
|
|||||||
implementationPredicate: (Implementation) -> Boolean = { true }
|
implementationPredicate: (Implementation) -> Boolean = { true }
|
||||||
): Collection<Implementation> {
|
): Collection<Implementation> {
|
||||||
return elements
|
return elements
|
||||||
.flatMap { it.allImplementations }
|
.flatMap { it.implementations }
|
||||||
.mapNotNullTo(mutableSetOf()) { implementation ->
|
.mapNotNullTo(mutableSetOf()) { implementation ->
|
||||||
if (!implementationPredicate(implementation)) return@mapNotNullTo null
|
if (!implementationPredicate(implementation)) return@mapNotNullTo null
|
||||||
if (implementation.element == element) return@mapNotNullTo null
|
if (implementation.element == element) return@mapNotNullTo null
|
||||||
@@ -128,7 +128,7 @@ abstract class AbstractBuilderConfigurator<Element, Implementation, BuilderField
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val allLeafBuilders: List<LeafBuilder<BuilderField, Element, Implementation>>
|
private val allLeafBuilders: List<LeafBuilder<BuilderField, Element, Implementation>>
|
||||||
get() = elements.flatMap { it.allImplementations }.mapNotNull { it.builder }
|
get() = elements.flatMap { it.implementations }.mapNotNull { it.builder }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows to batch-apply [config] to certain fields in _all_ the builders that satisfy the given
|
* Allows to batch-apply [config] to certain fields in _all_ the builders that satisfy the given
|
||||||
|
|||||||
+3
-6
@@ -60,11 +60,8 @@ abstract class AbstractImplementationConfigurator<Implementation, Element, Imple
|
|||||||
* @return The configured implementation.
|
* @return The configured implementation.
|
||||||
*/
|
*/
|
||||||
protected fun impl(element: Element, name: String? = null, config: ImplementationContext.() -> Unit = {}): Implementation {
|
protected fun impl(element: Element, name: String? = null, config: ImplementationContext.() -> Unit = {}): Implementation {
|
||||||
val implementation = if (name == null) {
|
val implementation = element.implementations.firstOrNull { it.name == name }
|
||||||
element.defaultImplementation
|
?: createImplementation(element, name)
|
||||||
} else {
|
|
||||||
element.customImplementations.firstOrNull { it.name == name }
|
|
||||||
} ?: createImplementation(element, name)
|
|
||||||
val context = ImplementationContext(implementation)
|
val context = ImplementationContext(implementation)
|
||||||
context.apply(config)
|
context.apply(config)
|
||||||
elementsWithImpl += element
|
elementsWithImpl += element
|
||||||
@@ -103,7 +100,7 @@ abstract class AbstractImplementationConfigurator<Implementation, Element, Imple
|
|||||||
config: ImplementationContext.(field: String) -> Unit,
|
config: ImplementationContext.(field: String) -> Unit,
|
||||||
) {
|
) {
|
||||||
for (element in elementsWithImpl) {
|
for (element in elementsWithImpl) {
|
||||||
for (implementation in element.allImplementations) {
|
for (implementation in element.implementations) {
|
||||||
if (!implementationPredicate(implementation)) continue
|
if (!implementationPredicate(implementation)) continue
|
||||||
if (!implementation.allFields.any { it.name == field }) continue
|
if (!implementation.allFields.any { it.name == field }) continue
|
||||||
if (!fieldPredicate(implementation.getField(field))) continue
|
if (!fieldPredicate(implementation.getField(field))) continue
|
||||||
|
|||||||
+1
-1
@@ -101,7 +101,7 @@ fun <Element, Implementation, ElementField, ImplementationField> generateTree(
|
|||||||
detectBaseTransformerTypes(model)
|
detectBaseTransformerTypes(model)
|
||||||
}
|
}
|
||||||
implementationConfigurator?.configureImplementations(model)
|
implementationConfigurator?.configureImplementations(model)
|
||||||
val implementations = model.elements.flatMap { it.allImplementations }
|
val implementations = model.elements.flatMap { it.implementations }
|
||||||
InterfaceAndAbstractClassConfigurator((model.elements + implementations))
|
InterfaceAndAbstractClassConfigurator((model.elements + implementations))
|
||||||
.configureInterfacesAndAbstractClasses()
|
.configureInterfacesAndAbstractClasses()
|
||||||
addPureAbstractElement(model.elements, pureAbstractElement)
|
addPureAbstractElement(model.elements, pureAbstractElement)
|
||||||
|
|||||||
Reference in New Issue
Block a user