-
Notifications
You must be signed in to change notification settings - Fork 9
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...in/src/main/kotlin/org/axonframework/extensions/kotlin/serialization/ArrayResponseType.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright (c) 2010-2024. Axon Framework | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
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) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.