Tmpod
12/30/2024, 5:04 PMtype
discriminant field + a simple enum of all variants. Very hacky, I know.
Now that I'm starting to work on writing this properly, my first thought was to model this with sealed classes. Sometimes I need to filter for specific types of documents, and since it was also useful in many other places, I'd like to keep the type
field in my sealed class. This creates a duplication of fields, however, since kx.ser adds a type
field (or ___type
in case of a name clash, as is the case here) with the class FQN. I'd like to avoid that completely and just use the enum (serialized as its name string).
Is there any way to achieve this? If not, what do you suggest I do instead?
Thank you in advance 😄
asked this first here, but figured I'd ask in the more active Slack too :pTmpod
12/30/2024, 5:05 PMCLOVIS
12/31/2024, 9:57 AMCLOVIS
12/31/2024, 9:57 AMtype
field does exist, so you can make queries on it. The problem is that it's not an actual field declared on the class so you won't be able to access it in a type-safe way…CLOVIS
12/31/2024, 10:00 AMTmpod
01/03/2025, 4:21 PMThe problem is that it's not an actual field declared on the class so you won't be able to access it in a type-safe way…I'm not sure what you mean here. Let me know you my current code:
@Serializable
sealed class Base {
abstract val type: TypeEnum
abstract val baseField: Int
}
@Serializable
class Foo(
override val baseField: Int
override val foo: String
) {
override val type = TypeEnum.FOO
}
@Serializable
class Bar(
override val baseField: Int
override val Bar: Long
) {
override val type = TypeEnum.BAR
}
My current issue is that kx.ser adds an extra ___type
field that it uses to discriminate the child type. I would like for it to instead use the existing type
field.CLOVIS
01/03/2025, 4:25 PMI know there is KtMongo, but it's not very matureDon't hesitate to create issues with additional features you'd like to see blob smile happy
My current issue is that kx.ser adds an extraYeah, that's the field I'm talking about. This one doesn't actually exist, so you can't use it as part of the KMongo/KtMongo DSLs.field that it uses to discriminate the child type.___type
I would like for it to instead use the existingI don't think this is possible at the moment (without writing your own custom serializer). Maybe they would be open to adding it, though.field.type
Tmpod
01/03/2025, 4:33 PMCLOVIS
01/03/2025, 4:53 PMCLOVIS
01/03/2025, 4:54 PMCLOVIS
01/03/2025, 5:01 PMTmpod
01/03/2025, 5:04 PM