Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsamson7 committed Mar 23, 2024
1 parent 9bb26b3 commit db47f15
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 308 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,6 @@ interface PortalAdministrationService : Service {
@DeleteMapping("delete-application-version/{application}/{version}")
fun deleteApplicationVersion(@PathVariable application: String, @PathVariable version: String)

// TEST

@GetMapping("compute-application-version-configuration/{application}")
fun computeApplicationVersionConfiguration(@PathVariable application: Long)


// TEST

// microfrontend

@GetMapping("read-microfrontends")
Expand Down

Large diffs are not rendered by default.

43 changes: 27 additions & 16 deletions portal/core/src/main/java/com/serious/portal/mapper/Mapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ import kotlin.reflect.jvm.jvmErasure
import kotlin.reflect.jvm.reflect

typealias Conversion<I, O> = (I) -> O
typealias Finalizer<S, T> = (S,T) -> Unit

typealias PKGetter<T,PK> = (any: T) -> PK

typealias Finalizer<S, T> = (S,T) -> Unit

// mapper

// top level functions

fun constant(value: Any) : MappingDefinition.Accessor {
return MappingDefinition.ConstantAccessor(value)
Expand Down Expand Up @@ -116,7 +113,7 @@ class OperationBuilder(private val matches: MutableCollection<MappingDefinition.
// inner node or leaf

fetchProperty = Mapping.PeekValueProperty(
parent!!.index,
parent!!.stackIndex,
accessor.makeTransformerProperty(false /* read */)
)
type = accessor.type
Expand All @@ -127,7 +124,7 @@ class OperationBuilder(private val matches: MutableCollection<MappingDefinition.
if (!isLeaf) {
// store the intermediate result
stackIndex = sourceTree.stackSize++ // that's my index
operations.add(Transformer.Operation(fetchProperty!!, Mapping.PushValueProperty(index)))
operations.add(Transformer.Operation(fetchProperty!!, Mapping.PushValueProperty(stackIndex)))
}
}
}
Expand Down Expand Up @@ -320,7 +317,7 @@ class OperationBuilder(private val matches: MutableCollection<MappingDefinition.
if ( to != targetType)
throw MapperDefinitionException("conversion target type ${to.simpleName} does not match ${targetType.simpleName}", null)
}
else if (sourceType != targetType && !deep )
else if (sourceType !== targetType && !sourceType.isSubclassOf(targetType) && !deep )
conversion = tryConvert(sourceType, targetType) // try automatic conversion for low-level types

return conversion as Conversion<Any?,Any?>?
Expand Down Expand Up @@ -559,7 +556,7 @@ class MappingDefinition<S : Any, T : Any>(val sourceClass: KClass<S>, val target

var deep = false
var conversion: Conversion<Any,Any>? = null
var synchronizer: MapperRelationSynchronizer<Any,Any,Any?>? = null
var synchronizer: RelationSynchronizer<Any,Any,Any?>? = null
var sourceAccessor : Array<Accessor>? = null
var targetAccessor : Array<Accessor>? = null
var properties : Properties? = null
Expand Down Expand Up @@ -607,6 +604,20 @@ class MappingDefinition<S : Any, T : Any>(val sourceClass: KClass<S>, val target
return This
}

infix fun Accessor.to(target: String) :MapBuilder<S,T> {
sourceAccessor = arrayOf(this)
targetAccessor = arrayOf(PropertyAccessor(target))

return This
}

infix fun Accessor.to(target: Array<String>) :MapBuilder<S,T> {
sourceAccessor = arrayOf(this)
targetAccessor = target.map { PropertyAccessor(it) }.toTypedArray()

return This
}

// property

infix fun KProperty1<S,*>.to(target: KProperty1<T,*>):MapBuilder<S,T> {
Expand All @@ -624,8 +635,8 @@ class MappingDefinition<S : Any, T : Any>(val sourceClass: KClass<S>, val target

// synchronize

infix fun<TO: Any,ENTITY:Any, PK:Any?> synchronize(synchronizer: MapperRelationSynchronizer<TO,ENTITY, PK>) : MapBuilder<S,T> {
this.synchronizer = synchronizer as MapperRelationSynchronizer<Any,Any,Any?>
infix fun<TO: Any,ENTITY:Any, PK:Any?> synchronize(synchronizer: RelationSynchronizer<TO,ENTITY, PK>) : MapBuilder<S,T> {
this.synchronizer = synchronizer as RelationSynchronizer<Any,Any,Any?>
this.deep = true

return this
Expand Down Expand Up @@ -1143,7 +1154,7 @@ class MappingDefinition<S : Any, T : Any>(val sourceClass: KClass<S>, val target
return this
}

fun <TO: Any, ENTITY: Any, PK: Any?> synchronize(from: String, to: String, synchronizer: MapperRelationSynchronizer<TO, ENTITY, PK>): MappingDefinition<S, T> {
fun <TO: Any, ENTITY: Any, PK: Any?> synchronize(from: String, to: String, synchronizer: RelationSynchronizer<TO, ENTITY, PK>): MappingDefinition<S, T> {
operations.add(MapAccessor(
arrayOf(PropertyAccessor(from)),
arrayOf(Mapping.RelationshipAccessor(to, synchronizer)),
Expand Down Expand Up @@ -1404,7 +1415,7 @@ class Mapping<S : Any, T : Any>(
return if (value != null)
property.get(value, context)
else
UNDEFINED
null//UNDEFINED // TODO!!!!!!!!!
}

override fun set(instance: Any, value: Any?, context: Context) {
Expand Down Expand Up @@ -1472,7 +1483,7 @@ class Mapping<S : Any, T : Any>(
}
}

class SynchronizeMultiValuedRelationship<TO:Any, ENTITY:Any, PK:Any?>(val property: KProperty1<Any, Any?>, val synchronizer: MapperRelationSynchronizer<TO, ENTITY, PK>)
class SynchronizeMultiValuedRelationship<TO:Any, ENTITY:Any, PK:Any?>(val property: KProperty1<Any, Any?>, val synchronizer: RelationSynchronizer<TO, ENTITY, PK>)
:Property<Context> {
// override

Expand All @@ -1493,7 +1504,7 @@ class Mapping<S : Any, T : Any>(
}
}

class RelationshipAccessor<TO: Any, ENTITY: Any, PK: Any?>(accessor: String, val synchronizer: MapperRelationSynchronizer<TO, ENTITY, PK>) : MappingDefinition.PropertyAccessor(accessor) {
class RelationshipAccessor<TO: Any, ENTITY: Any, PK: Any?>(accessor: String, val synchronizer: RelationSynchronizer<TO, ENTITY, PK>) : MappingDefinition.PropertyAccessor(accessor) {
override fun makeTransformerProperty(write: Boolean): Property<Context> {
return if (!write)
throw MapperDefinitionException("error")
Expand Down Expand Up @@ -1667,7 +1678,7 @@ class Mapping<S : Any, T : Any>(
val result = writer.create(reader.size())

while ( reader.hasMore())
writer.set(reader.get())
writer.set(context.mapper.map(reader.get(), context)!!)

property.set(instance, result, context)
} // if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ package com.serious.portal.mapper

import java.util.HashMap

typealias PKGetter<T,PK> = (any: T) -> PK

abstract class MapperRelationSynchronizer<TO, ENTITY, PK> protected constructor(private val toPK: PKGetter<TO,PK>, private val entityPK: PKGetter<ENTITY,PK>) {
abstract class RelationSynchronizer<TO, ENTITY, PK> protected constructor(private val toPK: PKGetter<TO,PK>, private val entityPK: PKGetter<ENTITY,PK>) {
// protected

protected open fun missingPK(pk: PK) : Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ class CollectionTest {
}

class Bar {
var name: String = "line"
var name: String = "bar"

var baz : Array<Baz> = arrayOf(Baz())
}

class Baz {
var name: String = "baz"
}


// test

@Test
fun testVersion() {
fun testCollection() {
val mapper = Mapper(
Mapping.build(Foo::class, Foo::class) {
map { "barArray" to "barList" deep true}
Expand All @@ -33,18 +39,23 @@ class CollectionTest {


Mapping.build(Bar::class, Bar::class) {
map { properties() }
map { "name" to "name" }
map { "baz" to "baz" deep true}
},

Mapping.build(Baz::class, Baz::class) {
map { "name" to "name" }
}
)

val foo = Foo()

val result = mapper.map<Foo>(foo)


// eq
val result = mapper.map<Foo>(foo)!!

//assertEquals(true, Version("1.0").eq(Version("1.0")), "expected eq")
assertEquals(1, result.barArray.size)
assertEquals(1, result.barList.size)
assertEquals(1, result.barArray[0].baz.size)
assertEquals("baz", result.barArray[0].baz[0].name)
//assertEquals(false, Version("1.0").eq(Version("1.1.1.1")), "expected !eq")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ConversionTest {
// test

@Test
fun testVersion() {
fun testConversion() {
val mapper = Mapper(
Mapping.build(Foo::class, Foo::class) {
map { "short" to "short" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class InheritanceTest {
// test

@Test
fun testVersion() {
fun testInheritance() {
val baseMapping = Mapping.build(Base::class,Base::class) {
map { properties() }
}
Expand Down
41 changes: 41 additions & 0 deletions portal/core/src/test/java/com/serious/portal/mapper/MapperTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ class MapperTest {

class ReadOnly(val name: String)

@Test()
fun testPath() {
// classes

data class Price(val currency: String, val value: Long)

class Product (var name: String = "", val price1: Price?, val price2: Price)

data class Target(var name: String, val price1: Long?, val price2: Long)


// mapper

val mapper = Mapper(
Mapping.build(Product::class, Target::class) {
map { properties("name")}
map { path("price1", "value") to "price1"}
map { path("price2", "value") to "price2"}
})

println(mapper.describe())

val source = Product("product", null/*Price("EU", 1)*/, Price("EU", 2))
val target = mapper.map<Target>(source)!!

assertEquals(1, target.price1)
assertEquals(2, target.price2)
}

@Test()
fun testDeepCollectionLevel1Data() {
// classes
Expand Down Expand Up @@ -172,6 +201,18 @@ class MapperTest {
}
}

@Test()
fun testConstant() {
val mapper = Mapper(
Mapping.build(From::class, To::class) {
map { constant("name") to "name"}
})

val result = mapper.map<To>(From(""))!!

assertEquals("name", result.name)
}

@Test()
fun testExceptionMissingSpec() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SynchronizerTest {
}


class BarSynchronizer : MapperRelationSynchronizer<Bar, BarEntity, Int>({bar: Bar -> bar.id}, {bar: BarEntity -> bar.id}) {
class BarSynchronizer : RelationSynchronizer<Bar, BarEntity, Int>({ bar: Bar -> bar.id}, { bar: BarEntity -> bar.id}) {
override fun provide(bar: Bar, context: Mapping.Context): BarEntity {
return context.mapper.map(bar, context)!!
}
Expand Down

0 comments on commit db47f15

Please sign in to comment.