Skip to content

Add kotlin.serialization implementation of Serializer #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import java.util.stream.Stream
* @see ResponseTypes
* @since 0.1.0
*/
inline fun <reified R, reified Q> QueryGateway.queryMany(query: Q): CompletableFuture<List<R>> {
inline fun <reified R, reified Q: Any> QueryGateway.queryMany(query: Q): CompletableFuture<List<R>> {
return this.query(query, ResponseTypes.multipleInstancesOf(R::class.java))
}

Expand All @@ -57,7 +57,7 @@ inline fun <reified R, reified Q> QueryGateway.queryMany(query: Q): CompletableF
* @see ResponseTypes
* @since 0.1.0
*/
inline fun <reified R, reified Q> QueryGateway.queryMany(queryName: String, query: Q): CompletableFuture<List<R>> {
inline fun <reified R, reified Q: Any> QueryGateway.queryMany(queryName: String, query: Q): CompletableFuture<List<R>> {
return this.query(queryName, query, ResponseTypes.multipleInstancesOf(R::class.java))
}

Expand All @@ -72,7 +72,7 @@ inline fun <reified R, reified Q> QueryGateway.queryMany(queryName: String, quer
* @see ResponseTypes
* @since 0.1.0
*/
inline fun <reified R, reified Q> QueryGateway.query(query: Q): CompletableFuture<R> {
inline fun <reified R, reified Q: Any> QueryGateway.query(query: Q): CompletableFuture<R> {
return this.query(query, ResponseTypes.instanceOf(R::class.java))
}

Expand All @@ -88,7 +88,7 @@ inline fun <reified R, reified Q> QueryGateway.query(query: Q): CompletableFutur
* @see ResponseTypes
* @since 0.1.0
*/
inline fun <reified R, reified Q> QueryGateway.query(queryName: String, query: Q): CompletableFuture<R> {
inline fun <reified R, reified Q: Any> QueryGateway.query(queryName: String, query: Q): CompletableFuture<R> {
return this.query(queryName, query, ResponseTypes.instanceOf(R::class.java))
}

Expand All @@ -103,7 +103,7 @@ inline fun <reified R, reified Q> QueryGateway.query(queryName: String, query: Q
* @see ResponseTypes
* @since 0.1.0
*/
inline fun <reified R, reified Q> QueryGateway.queryOptional(query: Q): CompletableFuture<Optional<R>> {
inline fun <reified R, reified Q: Any> QueryGateway.queryOptional(query: Q): CompletableFuture<Optional<R>> {
return this.query(query, ResponseTypes.optionalInstanceOf(R::class.java))
}

Expand All @@ -119,7 +119,7 @@ inline fun <reified R, reified Q> QueryGateway.queryOptional(query: Q): Completa
* @see ResponseTypes
* @since 0.1.0
*/
inline fun <reified R, reified Q> QueryGateway.queryOptional(queryName: String, query: Q): CompletableFuture<Optional<R>> {
inline fun <reified R, reified Q: Any> QueryGateway.queryOptional(queryName: String, query: Q): CompletableFuture<Optional<R>> {
return this.query(queryName, query, ResponseTypes.optionalInstanceOf(R::class.java))
}

Expand All @@ -136,7 +136,7 @@ inline fun <reified R, reified Q> QueryGateway.queryOptional(queryName: String,
* @see ResponseTypes
* @since 0.2.0
*/
inline fun <reified R, reified Q> QueryGateway.scatterGather(query: Q, timeout: Long,
inline fun <reified R, reified Q: Any> QueryGateway.scatterGather(query: Q, timeout: Long,
timeUnit: TimeUnit): Stream<R> {
return this.scatterGather(query, ResponseTypes.instanceOf(R::class.java), timeout, timeUnit)
}
Expand All @@ -155,7 +155,7 @@ inline fun <reified R, reified Q> QueryGateway.scatterGather(query: Q, timeout:
* @see ResponseTypes
* @since 0.2.0
*/
inline fun <reified R, reified Q> QueryGateway.scatterGather(queryName: String, query: Q, timeout: Long,
inline fun <reified R, reified Q: Any> QueryGateway.scatterGather(queryName: String, query: Q, timeout: Long,
timeUnit: TimeUnit): Stream<R> {
return this.scatterGather(queryName, query, ResponseTypes.instanceOf(R::class.java), timeout, timeUnit)
}
Expand All @@ -173,7 +173,7 @@ inline fun <reified R, reified Q> QueryGateway.scatterGather(queryName: String,
* @see ResponseTypes
* @since 0.2.0
*/
inline fun <reified R, reified Q> QueryGateway.scatterGatherMany(query: Q, timeout: Long,
inline fun <reified R, reified Q: Any> QueryGateway.scatterGatherMany(query: Q, timeout: Long,
timeUnit: TimeUnit): Stream<List<R>> {
return this.scatterGather(query, ResponseTypes.multipleInstancesOf(R::class.java), timeout, timeUnit)
}
Expand All @@ -192,7 +192,7 @@ inline fun <reified R, reified Q> QueryGateway.scatterGatherMany(query: Q, timeo
* @see ResponseTypes
* @since 0.2.0
*/
inline fun <reified R, reified Q> QueryGateway.scatterGatherMany(queryName: String, query: Q, timeout: Long,
inline fun <reified R, reified Q: Any> QueryGateway.scatterGatherMany(queryName: String, query: Q, timeout: Long,
timeUnit: TimeUnit): Stream<List<R>> {
return this.scatterGather(queryName, query, ResponseTypes.multipleInstancesOf(R::class.java), timeout, timeUnit)
}
Expand All @@ -210,7 +210,7 @@ inline fun <reified R, reified Q> QueryGateway.scatterGatherMany(queryName: Stri
* @see ResponseTypes
* @since 0.2.0
*/
inline fun <reified R, reified Q> QueryGateway.scatterGatherOptional(query: Q, timeout: Long,
inline fun <reified R, reified Q: Any> QueryGateway.scatterGatherOptional(query: Q, timeout: Long,
timeUnit: TimeUnit): Stream<Optional<R>> {
return this.scatterGather(query, ResponseTypes.optionalInstanceOf(R::class.java), timeout, timeUnit)
}
Expand All @@ -229,7 +229,7 @@ inline fun <reified R, reified Q> QueryGateway.scatterGatherOptional(query: Q, t
* @see ResponseTypes
* @since 0.2.0
*/
inline fun <reified R, reified Q> QueryGateway.scatterGatherOptional(queryName: String, query: Q, timeout: Long,
inline fun <reified R, reified Q: Any> QueryGateway.scatterGatherOptional(queryName: String, query: Q, timeout: Long,
timeUnit: TimeUnit): Stream<Optional<R>> {
return this.scatterGather(queryName, query, ResponseTypes.optionalInstanceOf(R::class.java), timeout, timeUnit)
}
Expand All @@ -246,7 +246,7 @@ inline fun <reified R, reified Q> QueryGateway.scatterGatherOptional(queryName:
* @see ResponseTypes
* @since 0.3.0
*/
inline fun <reified Q, reified I, reified U> QueryGateway.subscriptionQuery(query: Q): SubscriptionQueryResult<I, U> =
inline fun <reified Q: Any, reified I, reified U> QueryGateway.subscriptionQuery(query: Q): SubscriptionQueryResult<I, U> =
this.subscriptionQuery(query, ResponseTypes.instanceOf(I::class.java), ResponseTypes.instanceOf(U::class.java))

/**
Expand All @@ -262,5 +262,5 @@ inline fun <reified Q, reified I, reified U> QueryGateway.subscriptionQuery(quer
* @see ResponseTypes
* @since 0.3.0
*/
inline fun <reified Q, reified I, reified U> QueryGateway.subscriptionQuery(queryName: String, query: Q): SubscriptionQueryResult<I, U> =
inline fun <reified Q: Any, reified I, reified U> QueryGateway.subscriptionQuery(queryName: String, query: Q): SubscriptionQueryResult<I, U> =
this.subscriptionQuery(queryName, query, ResponseTypes.instanceOf(I::class.java), ResponseTypes.instanceOf(U::class.java))
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.axonframework.extensions.kotlin.serialization

import org.axonframework.common.ReflectionUtils
import org.axonframework.common.TypeReflectionUtils
import org.axonframework.messaging.responsetypes.AbstractResponseType
import org.axonframework.messaging.responsetypes.InstanceResponseType
import org.axonframework.messaging.responsetypes.ResponseType
import java.lang.reflect.Type
import java.util.concurrent.Future

/**
* A [ResponseType] implementation that will match with query handlers which return a multiple instances of the
* expected response type. If matching succeeds, the [ResponseType.convert] function will be called, which
* will cast the query handler it's response to an [Array] with element type [E].
*
* @param E The element type which will be matched against and converted to
* @author Gerard de Leeuw
* @see org.axonframework.messaging.responsetypes.MultipleInstancesResponseType
*/
class ArrayResponseType<E>(elementType: Class<E>) : AbstractResponseType<Array<E>>(elementType) {

companion object {
/**
* Indicates that the response matches with the [Type] while returning an iterable result.
*
* @see ResponseType.MATCH
*
* @see ResponseType.NO_MATCH
*/
const val ITERABLE_MATCH = 1024
}

private val instanceResponseType: InstanceResponseType<E> = InstanceResponseType(elementType)

/**
* Match the query handler's response [Type] with this implementation's [E].
* Will return true in the following scenarios:
*
* * If the response type is an [Array]
* * If the response type is a [E]
*
* If there is no match at all, it will return false to indicate a non-match.
*
* @param responseType the response [Type] of the query handler which is matched against
* @return true for [Array] or [E] and [ResponseType.NO_MATCH] for non-matches
*/
override fun matches(responseType: Type): Boolean =
matchRank(responseType) > NO_MATCH

/**
* Match the query handler's response [Type] with this implementation's [E].
* Will return a value greater than 0 in the following scenarios:
*
* * [ITERABLE_MATCH]: If the response type is an [Array]
* * [ResponseType.MATCH]: If the response type is a [E]
*
* If there is no match at all, it will return [ResponseType.NO_MATCH] to indicate a non-match.
*
* @param responseType the response [Type] of the query handler which is matched against
* @return [ITERABLE_MATCH] for [Array], [ResponseType.MATCH] for [E] and [ResponseType.NO_MATCH] for non-matches
*/
override fun matchRank(responseType: Type): Int = when {
isMatchingArray(responseType) -> ITERABLE_MATCH
else -> instanceResponseType.matchRank(responseType)
}

/**
* Converts the given [response] of type [Object] into an [Array] with element type [E] from
* this [ResponseType] instance. Should only be called if [ResponseType.matches] returns true.
* Will throw an [IllegalArgumentException] if the given response
* is not convertible to an [Array] of the expected response type.
*
* @param response the [Object] to convert into an [Array] with element type [E]
* @return an [Array] with element type [E], based on the given [response]
*/
override fun convert(response: Any): Array<E> {
val responseType: Class<*> = response.javaClass
if (responseType.isArray) {
@Suppress("UNCHECKED_CAST")
return response as Array<E>
}
throw IllegalArgumentException(
"Retrieved response [$responseType] is not convertible to an array with the expected element type [$expectedResponseType]"
)
}

@Suppress("UNCHECKED_CAST")
override fun responseMessagePayloadType(): Class<Array<E>> =
Array::class.java as Class<Array<E>>

override fun toString(): String = "ArrayResponseType[$expectedResponseType]"

private fun isMatchingArray(responseType: Type): Boolean {
val unwrapped = ReflectionUtils.unwrapIfType(responseType, Future::class.java)
val iterableType = TypeReflectionUtils.getExactSuperType(unwrapped, Array::class.java)
return iterableType != null && isParameterizedTypeOfExpectedType(iterableType)
}
}
Loading