From 9bb26b3423794575d5b6622058f4060f16a38a7d Mon Sep 17 00:00:00 2001 From: Andreas Ernst Date: Sat, 23 Mar 2024 13:51:52 +0100 Subject: [PATCH] fixes --- .../java/com/serious/portal/mapper/Mapper.kt | 178 ++++++++---------- 1 file changed, 82 insertions(+), 96 deletions(-) diff --git a/portal/core/src/main/java/com/serious/portal/mapper/Mapper.kt b/portal/core/src/main/java/com/serious/portal/mapper/Mapper.kt index c5977675..08867d33 100644 --- a/portal/core/src/main/java/com/serious/portal/mapper/Mapper.kt +++ b/portal/core/src/main/java/com/serious/portal/mapper/Mapper.kt @@ -37,7 +37,6 @@ fun property(name: String) : MappingDefinition.PropertyAccessor { return MappingDefinition.PropertyAccessor(name) } - // local classes class OperationBuilder(private val matches: MutableCollection) { @@ -236,7 +235,7 @@ class OperationBuilder(private val matches: MutableCollection = accessor.type if (isInnerNode) { - val valueReceiver = computeValueReceiver() + val valueReceiver = computeValueReceiver(targetTree) // done @@ -292,11 +299,9 @@ class OperationBuilder(private val matches: MutableCollection { - val sourceProperty: Transformer.Property = sourceNode.fetchProperty!! - - val deep = match!!.deep - var conversion = match.conversion + private fun calculateConversion(sourceNode: SourceTree.Node) : Conversion? { + var conversion = match!!.conversion + val deep = match.deep // check conversion @@ -318,33 +323,34 @@ class OperationBuilder(private val matches: MutableCollection? + } + + private fun makeOperation(tree: TargetTree, sourceNode: SourceTree.Node): Transformer.Operation { + val sourceProperty: Transformer.Property = sourceNode.fetchProperty!! + + val deep = match!!.deep + val conversion = calculateConversion(sourceNode) + // compute operation - return if (isRoot) { - // root node + var writeProperty = accessor.makeTransformerProperty( (isRoot && tree.composite == null) || (!isRoot && !parent!!.immutable)) // property, constant or synchronizer - val writeProperty = accessor.makeTransformerProperty( tree.composite == null) + if (isRoot) { + // chain, if composite? - if (deep) - Transformer.Operation(sourceProperty, mapDeep(sourceNode.accessor, accessor, writeProperty)) - else { - if (tree.composite != null/*readOnly || immutable*/) { - if (tree.composite != null) - Transformer.Operation(sourceProperty, maybeConvert(Mapping.SetCompositeArgument(tree.composite!!, accessor), conversion)) - else - throw MapperDefinitionException("${accessor.name} is read only") - } - else { - Transformer.Operation(sourceProperty, maybeConvert(accessor.makeTransformerProperty(true/* write */), conversion)) - } - } - } - else { - Transformer.Operation( - sourceProperty, - maybeConvert(Mapping.SetCompositeArgument(parent!!.composite!!, accessor), conversion) - ) + if (tree.composite != null) + writeProperty = Mapping.SetCompositeArgument(tree.composite!!, accessor.index, writeProperty) } + else + writeProperty = Mapping.SetCompositeArgument(parent!!.composite!!, accessor.index, writeProperty) + + if ( deep ) + writeProperty = mapDeep(sourceNode.accessor, accessor, writeProperty) + else + writeProperty = maybeConvert(writeProperty, conversion) + + return Transformer.Operation(sourceProperty, writeProperty) } private fun tryConvert(sourceType: KClass, targetType: KClass): Conversion { @@ -439,9 +445,9 @@ class OperationBuilder(private val matches: MutableCollection(val sourceClass: KClass, val target fun makeTransformerProperty(write: Boolean): Transformer.Property - fun getValue(instance: Any): Any? - - fun setValue(instance: Any, value: Any?, mappingContext: Mapping.Context) - fun description() : String } @@ -783,7 +785,7 @@ class MappingDefinition(val sourceClass: KClass, val target if (write && readOnly && !clazz.isData) throw MapperDefinitionException("property ${clazz.simpleName}.${readProperty.name} is read only") - if ( write && !clazz.isData ) + if ( this.readProperty is KMutableProperty1<*, *> ) this.writeProperty = this.readProperty as KMutableProperty1 } catch (e: MapperDefinitionException) { @@ -793,17 +795,15 @@ class MappingDefinition(val sourceClass: KClass, val target throw MapperDefinitionException("unknown property ${clazz.simpleName}.${name}") } - this.index = clazz.declaredMemberProperties.indexOf(this.readProperty) - this.overallIndex = clazz.memberProperties.indexOf(this.readProperty) - } - - override fun getValue(instance: Any): Any? { - return this.readProperty.get(instance) - } - - override fun setValue(instance: Any, value: Any?, mappingContext: Mapping.Context) { - if ( value != null ) - this.writeProperty?.set(instance, value) + if ( clazz.isData) { + val param = clazz.constructors.first().parameters.find { parameter-> parameter.name == this.name } + this.index = clazz.constructors.first().parameters.indexOf(param) + this.overallIndex = index + } + else { + this.index = clazz.declaredMemberProperties.indexOf(this.readProperty) + this.overallIndex = clazz.memberProperties.indexOf(this.readProperty) + } } // override Any @@ -849,13 +849,9 @@ class MappingDefinition(val sourceClass: KClass, val target return Mapping.ConstantValue(constant) } - override fun getValue(instance: Any): Any { + /*TODO override fun getValue(instance: Any): Any { return constant - } - - override fun setValue(instance: Any, value: Any?, mappingContext: Mapping.Context) { - // no - } + }*/ override fun description() :String{ return "constant ${constant}" @@ -1280,7 +1276,7 @@ class Mapping( // abstract - abstract fun set(instance: Any, value: Any?, accessor: MappingDefinition.Accessor?, index: Int, mappingContext: Context) + abstract fun set(instance: Any, value: Any?, property: Property?, index: Int, mappingContext: Context) } class ImmutableCompositeBuffer(definition: MappingDefinition.CompositeDefinition, nargs: Int) : CompositeBuffer(definition, nargs) { @@ -1290,7 +1286,7 @@ class Mapping( // override - override fun set(instance: Any, value: Any?, accessor: MappingDefinition.Accessor?, index: Int, mappingContext: Context) { + override fun set(instance: Any, value: Any?, property: Property?, index: Int, mappingContext: Context) { arguments[index] = value // are we done? @@ -1312,8 +1308,8 @@ class Mapping( // public - override fun set(instance: Any, value: Any?, accessor: MappingDefinition.Accessor?, index: Int, mappingContext: Context) { - accessor!!.setValue(newInstance, value, mappingContext) + override fun set(instance: Any, value: Any?, property: Property?, index: Int, mappingContext: Context) { + property!!.set(newInstance, value, mappingContext) // are we done? @@ -1328,19 +1324,19 @@ class Mapping( fun receive(context: Context, instance: Any, value: Any) } - class SetPropertyValueReceiver(val accessor: MappingDefinition.Accessor) : ValueReceiver { + class SetPropertyValueReceiver(val property: Property) : ValueReceiver { // implement ValueReceiver override fun receive(context: Context, instance: Any, value: Any) { - accessor.setValue(instance, value, context) + property.set(instance, value, context) } } - class SetMutableCompositePropertyValueReceiver(val composite: MappingDefinition.CompositeDefinition, val accessor: MappingDefinition.Accessor) : ValueReceiver { + class SetMutableCompositePropertyValueReceiver(val composite: MappingDefinition.CompositeDefinition, val property: Property) : ValueReceiver { // implement ValueReceiver override fun receive(context: Context, instance: Any, value: Any) { - context.getCompositeBuffer(composite.index).set(instance, value, accessor, 0, context) + context.getCompositeBuffer(composite.index).set(instance, value, property, 0, context) } } @@ -1362,24 +1358,6 @@ class Mapping( // properties - open class AccessorValue(val accessor: MappingDefinition.Accessor) : Property { - // implement - - override fun get(instance: Any, context: Context): Any? { - return accessor.getValue(instance) - } - - override fun set(instance: Any, value: Any?, context: Context) { - accessor.setValue(instance, value, context) - } - - // override - - override fun toString(): String { - return accessor.toString() - } - } - class ConstantValue(val value: Any?) : Property { // implement @@ -1528,8 +1506,8 @@ class Mapping( } } - class MapCollection2Collection(sourceClass: KClass<*>, targetClass: KClass<*>, property: MappingDefinition.Accessor) - : AccessorValue(property) { + class MapCollection2Collection(sourceClass: KClass<*>, targetClass: KClass<*>, val property: Property) + : Property { // local classes interface Container { @@ -1691,20 +1669,28 @@ class Mapping( while ( reader.hasMore()) writer.set(reader.get()) - super.set(instance, result, context) + property.set(instance, result, context) } // if } + override fun get(instance: Any, context: Context): Any? { + return null + } + // override Any override fun toString() : String { - return "map deep collection ${this.accessor.name}" + return "map deep collection ${this.property}" } } - class MapDeep(property: MappingDefinition.Accessor) : AccessorValue(property) { + class MapDeep(val targetProperty: Property) : Property { // override AccessorValue + override fun get(insatnce: Any, context: Context): Any? { + return null + } + override fun set(instance: Any, value: Any?, context: Context) { //context.setOrigin(origin) @@ -1715,17 +1701,17 @@ class Mapping( //context.setOrigin(null) } - super.set(instance, transformed, context) + targetProperty.set(instance, transformed, context) } // override Any override fun toString() : String { - return "map deep " + accessor.description() + return "map deep " + targetProperty } } - class SetCompositeArgument(private val composite: MappingDefinition.CompositeDefinition, private val accessor: MappingDefinition.Accessor) : Property { + class SetCompositeArgument(private val composite: MappingDefinition.CompositeDefinition, private val index: Int, private val property: Property) : Property { // implement Property override operator fun get(instance: Any, context: Context): Any? { @@ -1733,16 +1719,16 @@ class Mapping( } override operator fun set(instance: Any, value: Any?, context: Context) { - context.getCompositeBuffer(composite.index).set(instance, value, accessor, accessor.overallIndex, context) + context.getCompositeBuffer(composite.index).set(instance, value, property, index, context) } // override Any override fun toString() : String { if ( composite.immutable()) - return "${composite.clazz.simpleName}[${accessor.overallIndex}]" + return "${composite.clazz.simpleName}[${index}]" else - return "${composite.clazz.simpleName}.${accessor.description()}" + return "${composite.clazz.simpleName}.${property}" // TODO } } @@ -1752,7 +1738,7 @@ class Mapping( val targetClass: KClass<*> = definition.targetClass val isData = targetClass.isData - //val constructor = targetClass.constructors.find { ctr -> ctr.parameters.size == 0}!! + //TODO val constructor = targetClass.constructors.find { ctr -> ctr.parameters.size == 0}!! //fun createInstance() : Any { // return constructor.callBy(NO_PARAMETERS)