Skip to content

Commit

Permalink
working example based on avro-kotlin-serialization snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
jangalinski committed May 16, 2024
1 parent 13b6d93 commit 986a168
Show file tree
Hide file tree
Showing 46 changed files with 203 additions and 587 deletions.
8 changes: 2 additions & 6 deletions lib/fixtures/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

<dependencies>
<dependency>
<groupId>io.toolisticon.lib</groupId>
<artifactId>avro-kotlin</artifactId>
<groupId>io.toolisticon.kotlin.avro</groupId>
<artifactId>avro-kotlin-serialization</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -66,10 +66,6 @@
<artifactId>jackson-avro-module</artifactId>
<version>${jackson-avro-module.version}</version>
</dependency>
<dependency>
<groupId>com.github.avro-kotlin.avro4k</groupId>
<artifactId>avro4k-core</artifactId>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package bankaccount.command

import bankaccount.conversions.MoneySerializer
import bankaccount.conversions.MoneyLogicalType
import kotlinx.serialization.Serializable
import org.javamoney.moneta.Money

@Serializable
data class CreateBankAccount(
val accountId: String,
@Serializable(with = MoneySerializer::class)
@Serializable(with = MoneyLogicalType.MoneySerializer::class)
val initialBalance: Money
) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package bankaccount.command

import bankaccount.conversions.MoneySerializer
import bankaccount.conversions.MoneyLogicalType
import kotlinx.serialization.Serializable
import org.axonframework.modelling.command.TargetAggregateIdentifier
import org.javamoney.moneta.Money
Expand All @@ -9,6 +9,6 @@ import org.javamoney.moneta.Money
data class DepositMoney(
@TargetAggregateIdentifier
val accountId: String,
@Serializable(with = MoneySerializer::class)
@Serializable(with = MoneyLogicalType.MoneySerializer::class)
val amount: Money
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package bankaccount.command

import bankaccount.conversions.MoneySerializer
import bankaccount.conversions.MoneyLogicalType
import kotlinx.serialization.Serializable
import org.axonframework.modelling.command.TargetAggregateIdentifier
import org.javamoney.moneta.Money
Expand All @@ -9,6 +9,6 @@ import org.javamoney.moneta.Money
data class WithdrawMoney(
@TargetAggregateIdentifier
val accountId: String,
@Serializable(with = MoneySerializer::class)
@Serializable(with = MoneyLogicalType.MoneySerializer::class)
val amount: Money
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package bankaccount.event

import bankaccount.conversions.MoneySerializer
import bankaccount.conversions.MoneyLogicalType
import com.github.avrokotlin.avro4k.serializer.InstantSerializer
import kotlinx.serialization.Serializable
import org.javamoney.moneta.Money
Expand All @@ -12,7 +12,7 @@ data class BankAccountAuditEvent(
@Serializable(with = InstantSerializer::class)
val timestamp: Instant,
val accountId: String,
@Serializable(with = MoneySerializer::class)
@Serializable(with = MoneyLogicalType.MoneySerializer::class)
val amount: Money,
val traceId: String,
val correlationId: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package bankaccount.query

import bankaccount.conversions.MoneySerializer
import bankaccount.conversions.MoneyLogicalType.MoneySerializer
import kotlinx.serialization.Serializable
import org.javamoney.moneta.Money

Expand Down
8 changes: 2 additions & 6 deletions lib/logical-types/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@

<dependencies>
<dependency>
<groupId>io.toolisticon.lib</groupId>
<artifactId>avro-kotlin</artifactId>
<groupId>io.toolisticon.kotlin.avro</groupId>
<artifactId>avro-kotlin-serialization</artifactId>
</dependency>

<dependency>
<groupId>com.github.avro-kotlin.avro4k</groupId>
<artifactId>avro4k-core</artifactId>
</dependency>

<dependency>
<groupId>org.javamoney.moneta</groupId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package bankaccount.conversions

import io.toolisticon.avro.kotlin.logical.StringLogicalType
import io.toolisticon.avro.kotlin.logical.StringLogicalTypeFactory
import io.toolisticon.avro.kotlin.logical.conversion.StringLogicalTypeConversion
import io.toolisticon.avro.kotlin.logical.conversion.TypeConverter
import io.toolisticon.avro.kotlin.value.LogicalTypeName.Companion.toLogicalTypeName
import io.toolisticon.kotlin.avro.serialization.serializer.StringLogicalTypeSerializer
import io.toolisticon.kotlin.avro.serialization.spi.AvroSerializerModuleFactory
import kotlinx.serialization.modules.SerializersModule
import org.javamoney.moneta.Money
import java.util.*
import javax.money.format.AmountFormatQuery
import javax.money.format.MonetaryFormats


object MoneyLogicalType : StringLogicalType("money".toLogicalTypeName()) {

val convertedType = Money::class
val conversion = MoneyConversion()

val converter = object : TypeConverter<String, Money> {
private val format = MonetaryFormats.getAmountFormat(AmountFormatQuery.of(Locale.GERMAN))

override fun fromAvro(value: String): Money {
return Money.from(format.parse(value))
}

override fun toAvro(value: Money): String {
return format.format(value)
}
}

class MoneyLogicalTypeFactory : StringLogicalTypeFactory<MoneyLogicalType>(logicalType = MoneyLogicalType)

class MoneyConversion : StringLogicalTypeConversion<MoneyLogicalType, Money>(
logicalType = MoneyLogicalType,
convertedType = convertedType
) {
override fun fromAvro(value: String): Money = converter.fromAvro(value)
override fun toAvro(value: Money): String = converter.toAvro(value)
}

class MoneySerializer : StringLogicalTypeSerializer<MoneyLogicalType, Money>(conversion)

class MoneySerializerModuleFactory : AvroSerializerModuleFactory {
override fun invoke(): SerializersModule = SerializersModule {
contextual(convertedType, MoneySerializer())
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 986a168

Please sign in to comment.