Snowtype kotlin feedback

Hi! I have a few suggestions for the generated kotlin code to make it more safe and flexible!

  • Avoid mutability - the generated data classes are all using var instead of val. There should be no reason to mutate these values after the event has been created
  • Every model gets a fun toEvent() : SelfDescribing and fun toEntity() : SelfDescribingJson leaving room for misstakes. Generating toEvent only for events, and toEntity only for entities would make things a bit less error prone.
  • Use Interfaces. The structure seems perfect for a simple interface hierarcy such as
sealed interface SnowplowModel
sealed interface SnowplowEvent : SnowplowModel {
    fun toEvent(): SelfDescribing
}
sealed interface SnowplowEntity : SnowplowModel {
    fun toEntity(): SelfDescribingJson
}

Then having each generated model implement these as make sense. This would allow your users to create infrastructure around the generated events and/or make generic extension functions!

  • Use inner classes for namespacing. I noticed that for example enumerated strings generates a top level class with the same name as the field, making collisions very likely. When colliding, the generated code simply adds an incrementing number at the end of the name. A cleaner way to do this could be to make the enum class an inner class of the Event where it’s defined, so it can easily be referred to as MyEvent.MyEnumField.FOO.
  • Avoid default parameters for nonrequired fields. While sometimes convenient, it makes it really easy to accidently miss fields - especially if a schema is updated and adds a new optional parameter.
1 Like