Skip to content

Commit

Permalink
implement multiple message types as list #163
Browse files Browse the repository at this point in the history
  • Loading branch information
zambrovski committed Oct 18, 2024
1 parent 3fe8c0d commit 89dcecc
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
"doc": "Creates a new bank account.",
"meta": {
"group": "BankAccountAggregate",
"type": "deciderInit"
"types": [ "decider", "factory" ]
},
"request": [
{
Expand All @@ -265,7 +265,7 @@
"withdrawMoney": {
"meta": {
"group": "BankAccountAggregate",
"type": "decider"
"types": ["decider"]
},
"request": [
{
Expand All @@ -278,7 +278,7 @@
"depositMoney": {
"meta": {
"group": "BankAccountAggregate",
"type": "decider"
"types": ["decider"]
},
"request": [
{
Expand All @@ -293,7 +293,7 @@
"findCurrentBalanceForAccountId": {
"meta": {
"group": "BankAccountProjection",
"type": "query"
"types": ["query"]
},
"request": [
{
Expand All @@ -309,7 +309,7 @@
"findAllMoneyTransfersForAccountId": {
"meta": {
"group": "BankAccountProjection",
"type": "query"
"types": ["query"]
},
"request": [
{
Expand Down
8 changes: 4 additions & 4 deletions axon-avro-generation/src/main/kotlin/meta/MessageMetaData.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.holixon.axon.avro.generation.meta

import io.holixon.axon.avro.generation.meta.FieldMetaData.Companion.KEYS.TYPE
import io.holixon.axon.avro.generation.meta.MessageMetaData.Companion.KEYS.TYPES
import io.holixon.axon.avro.generation.meta.MessageMetaData.Companion.KEYS.GROUP
import io.toolisticon.kotlin.avro.model.wrapper.AvroProtocol
import io.toolisticon.kotlin.avro.value.Name
Expand All @@ -11,12 +11,12 @@ import io.toolisticon.kotlin.avro.value.property.meta
*/
data class MessageMetaData(
val group: Name?,
val type: MessageMetaDataType?
val types: List<MessageMetaDataType>?
) : AxonAvroMetaData {
companion object {
object KEYS {
const val GROUP = "group"
const val TYPE = "type"
const val TYPES = "types"
}

/**
Expand All @@ -25,7 +25,7 @@ data class MessageMetaData(
fun AvroProtocol.Message.messageMetaData(): MessageMetaData? = this.properties.meta.metaData {
MessageMetaData(
group = this[GROUP]?.let { it as String }?.let { Name(it.trim()) },
type = this[TYPE]?.let { it as String }?.let { MessageMetaDataType[it.trim()] }
types = this[TYPES]?.let { types -> types as List<String> }?.mapNotNull { type -> MessageMetaDataType[type.trim()] }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.util.*
*/
enum class MessageMetaDataType {
Decider,
DeciderInit,
Factory,
Query;

val decapitalizedName = name.replaceFirstChar { c -> c.lowercase(Locale.getDefault()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.holixon.axon.avro.generation.strategy

import _ktx.StringKtx.firstUppercase
import com.squareup.kotlinpoet.*
import io.holixon.axon.avro.generation.meta.AxonAvroMetaData.Companion.metaData
import io.holixon.axon.avro.generation.meta.FieldMetaData.Companion.fieldMetaData
import io.holixon.axon.avro.generation.meta.FieldMetaDataType
import io.holixon.axon.avro.generation.meta.MessageMetaData.Companion.messageMetaData
Expand All @@ -22,7 +21,6 @@ import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildAnnotation
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.builder
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.builder.funBuilder
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.builder.objectBuilder
import io.toolisticon.kotlin.generation.builder.KotlinAnnotationSpecBuilder.Companion.member
import io.toolisticon.kotlin.generation.builder.KotlinFunSpecBuilder
import io.toolisticon.kotlin.generation.spec.KotlinFileSpec
import io.toolisticon.kotlin.generation.spi.processor.executeAll
Expand Down Expand Up @@ -59,7 +57,7 @@ class AxonCommandHandlerProtocolInterfaceStrategy : AvroFileSpecFromProtocolDecl
Single interface for each command
*/
input.protocol.messages
.filterValues { message -> message.isDecider() || message.isDeciderInit() }
.filterValues { message -> message.isDecider() }
.entries
.groupBy { message -> message.value.messageMetaData()?.group?.value ?: UNKNOWN_GROUP }
.mapNotNull { (groupName, messages) ->
Expand Down Expand Up @@ -128,7 +126,7 @@ class AxonCommandHandlerProtocolInterfaceStrategy : AvroFileSpecFromProtocolDecl
addAnnotation(CommandHandler::class)
addParameter(command.name.value, avroPoetTypes[command.schema.hashCode].typeName)

if (message.isDeciderInit()) {
if (message.isFactory()) {
addAnnotation(
buildAnnotation(CreationPolicy::class) {
addEnumMember("value", AggregateCreationPolicy.ALWAYS)
Expand All @@ -149,6 +147,6 @@ class AxonCommandHandlerProtocolInterfaceStrategy : AvroFileSpecFromProtocolDecl
override fun test(context: ProtocolDeclarationContext, input: Any): Boolean {
return super.test(context, input)
&& input is ProtocolDeclaration
&& input.protocol.messages.values.any { message -> message.isDecider() || message.isDeciderInit() }
&& input.protocol.messages.values.any { message -> message.isDecider() || message.isFactory() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class AxonEventSourcingHandlerProtocolInterfaceStrategy : AvroFileSpecFromProtoc
Single interface for each event souring handler
*/
input.protocol.messages.filterTwoWay()
.filterValues { message -> message.isDecider() || message.isDeciderInit() }
.filterValues { message -> message.isDecider() || message.isFactory() }
.entries
.groupBy { message -> message.value.messageMetaData()?.group?.value ?: UNKNOWN_GROUP }
.mapNotNull { (groupName, messages) ->
Expand Down Expand Up @@ -107,6 +107,6 @@ class AxonEventSourcingHandlerProtocolInterfaceStrategy : AvroFileSpecFromProtoc
override fun test(context: ProtocolDeclarationContext, input: Any): Boolean {
return super.test(context, input)
&& input is ProtocolDeclaration
&& input.protocol.messages.values.any { message -> message.isDecider() || message.isDeciderInit() }
&& input.protocol.messages.values.any { message -> message.isDecider() || message.isFactory() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ fun AvroProtocol.Message.isQuery(): Boolean {
.any { RecordMetaDataType.Query == it.type }
// TODO: analyze response of query
*/
return this.messageMetaData()?.type == MessageMetaDataType.Query
return this.messageMetaData()?.types?.contains(MessageMetaDataType.Query) ?: false
}

fun AvroProtocol.Message.isDecider(): Boolean {
return this.messageMetaData()?.type == MessageMetaDataType.Decider
return this.messageMetaData()?.types?.contains(MessageMetaDataType.Decider) ?: false
}

fun AvroProtocol.Message.isDeciderInit(): Boolean {
return this.messageMetaData()?.type == MessageMetaDataType.DeciderInit
fun AvroProtocol.Message.isFactory(): Boolean {
return this.messageMetaData()?.types?.contains(MessageMetaDataType.Factory) ?: false
}


Expand Down

0 comments on commit 89dcecc

Please sign in to comment.