KReplica
Home
Playground
Guide
Home
Playground
Guide
Starter Template:
1. Basic Replication
2. Versioned DTOs
3. Nominal Typing Part I: Usage Guide
4. Nominal Typing Part II: Edge Case Examples
5. Directly Applying Annotations
6. Serialization Part I: The Apply Annotation
7. Serialization Part II: Advanced Features
8. Contextual Nesting (Unversioned)
9. Contextual Nesting (Versioned)
10. The Hide Annotation
Editor
Output
Run
Reset All
Reset All
Clear Output
package io.availe.demo.playground import io.availe.Replicate import io.availe.models.DtoVariant import io.availe.models.NominalTyping /* This assumes you read the prior playground template on Nominal Typing. This example shows how KReplica handles versioned schemas when a property with the same name has different types across different DTO versions. Additionally, "KReplica auto-generates value class names using PascalCase, even when the original variable names use a different naming convention like snake_case. */ private interface Order { @Replicate.Model(variants = [DtoVariant.DATA], nominalTyping = NominalTyping.ENABLED) private interface V1 : Order { // notice how in V1 `orderId` is an `Int`, but in V2 it's a `Long` val id: Int val customerId: Long // notice how this is in snake_case val total_amount: Double // notice how this is in kebab case val status: Boolean @Replicate.Property(nominalTyping = NominalTyping.DISABLED) val totalAmount: Double } @Replicate.Model(variants = [DtoVariant.DATA], nominalTyping = NominalTyping.ENABLED) private interface V2 : Order { // Notice how in V1 `orderId` is an `Int`, but in V2 it's a `Long` // All conflicting value classes are groupped together under the // "CONFLICTED (VERSION-SPECIFIC) VALUE CLASSES" comment in the output file. val id: Long val customerId: Long @Replicate.Property(nominalTyping = NominalTyping.DISABLED) val totalAmount: Double } } /* Personally, since developing it, I've come to believe that DTOs owning the value classes are an anti-pattern, as I prefer the source model to own it. I've since grown to prefer the "Compile-Safe API Mappers" pattern. This pattern is not generated by KReplica, but instead a pattern that KReplica DTOs enable. Check the documentation for more info. However, I'm aware that some developers opt for a "flat-model," with no distinction between DTOs and a domain model. If that's your style, this feature might be useful for you. */
Click "Run" to see the generated code.
Run
Reset All
Reset All
Clear Output