Skip to content

Is it possible to define a class with arbitrary extra keys, or alternatively a Mapping with some required keys? #661

Answered by HT154
edgarrmondragon asked this question in Q&A
Discussion options

You must be logged in to vote

One way to do this is to have a Mapping property in your class for those extra fields and combine everything using an output converter, eg.

class Handler {
    `class`: String
    level: String?
    formatter: String?
    filters: List<String>?
    kwargs: Mapping<String(this != "class", this != "level", this != "formatter", this != "filters"), Any> // constrain keys to prevent overlap with predefined properties
}

output {
  renderer {
    converters {
      [Handler] = (it) -> it.toMap().remove("kwargs") + it.kwargs.toMap() // merge types properties and kwargs
    }
  }
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@edgarrmondragon
Comment options

Answer selected by edgarrmondragon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants