I have a spring boot application which is structur...
# spring
l
I have a spring boot application which is structured in hexagonal architecture (using clean architecture). So I have a sealed class which is at domain layer and I would not like to "mess" it with annotations. The issue is that I wouldn't like to expose the domain, but a resource class. I usually do it easily, creating a
toDomain()
function. But in this case, I don't know how to do it. Since I'm new to sealed class, maybe there is a solution which I haven't found... The sealed class (domain) is something like:
Copy code
sealed class TypeInstruction(val type: String) {
    data class TypeOne(val isRequired: Boolean, val maxConcurrency: Short) : TypeInstruction(type = "typeOne")

    data class TypeTwo(val isRequired: Boolean, val maxAttemps: Short): TypeInstruction(type = "typeTwo")
}
And the resource is:
Copy code
sealed class TypeInstructionResource(val type: String) {
    data class TypeOne(val isRequired: Boolean, val maxConcurrency: Short) : TypeInstruction(type = "typeOne")

    data class TypeTwo(val isRequired: Boolean, val maxAttemps: Short): TypeInstruction(type = "typeTwo")
}
Then an extended function:
Copy code
fun TypeInstructionResource.toDomain() = when(type) {
    "typeOne" -> TypeInstruction.TypeOne(isRequired = isRequired, maxConcurrency = maxConcurrency)
    "typeTwo" -> TypeInstruction.TypeTwo(isRequired = isRequired, maxAttemps = maxAttemps)
    else -> /* */
}
But of course, the extended function does not recognize the "children" values, such as
isRequired
. Any suggestions?
t
you can use
is TypeOne
then it will automaticall cast it 🙂
Copy code
fun TypeInstructionResource.toDomain() = when(type) {
    is TypeOne -> TypeInstruction.TypeOne(isRequired = isRequired, maxConcurrency = maxConcurrency)
    is TypeTwo -> TypeInstruction.TypeTwo(isRequired = isRequired, maxAttemps = maxAttemps)
}
and also no else branch, since it is a sealed class this is not needed 🙂
l
thanks Ties but I dont get it. since type is a string, it cant be parsed to a subtype
t
ahh yes, sec
Copy code
sealed class TypeInstruction {
    data class TypeOne(val isRequired: Boolean, val maxConcurrency: Short) : TypeInstruction()

    data class TypeTwo(val isRequired: Boolean, val maxAttemps: Short): TypeInstruction()
}

sealed class TypeInstructionResource {
    data class TypeOne(val isRequired: Boolean, val maxConcurrency: Short) : TypeInstructionResource()

    data class TypeTwo(val isRequired: Boolean, val maxAttemps: Short): TypeInstructionResource()
}

fun TypeInstructionResource.toDomain() = when(this) {
    is TypeInstructionResource.TypeOne -> TypeInstruction.TypeOne(isRequired = isRequired, maxConcurrency = maxConcurrency)
    is TypeInstructionResource.TypeTwo -> TypeInstruction.TypeTwo(isRequired = isRequired, maxAttemps = maxAttemps)
}
👍 2
l
wow man, shame on me...
this
solved it.
t
No need to use a string to store the type in, you can just use the classes and pattern match over it 🙂 (and because they are sealed, you do not need to use an else branch since the compiler knows it can only be 2)
l
thanks!
t
no worries! good luck 🙂