Is it possible to define a class with arbitrary extra keys, or alternatively a Mapping with some required keys? #661
-
I'm trying to build a template for Python logging configuration. The problem: Each of the values in the I'm stuck defining the Pkl class for that class Handler {
`class`: String
level: String?
formatter: String?
filters: List<String>?
// TODO: Support extra handler keyword arguments
} So I'm currently forced to use a handlers = new Mapping {
["stdout"] = new Mapping {
["class"] = "logging.StreamHandler"
["level"] = "INFO"
["formatter"] = "simple"
["stream"] = "ext://sys.stdout"
}
["stderr"] = new Mapping {
["class"] = "logging.StreamHandler"
["level"] = "ERROR"
["formatter"] = "simple"
["stream"] = "ext://sys.stderr"
}
["file"] = new Mapping {
["class"] = "logging.FileHandler"
["formatter"] = "simple"
["filename"] = "app.log"
["mode"] = "w"
}
} So, it is possible to define a class with arbitrary extra keys, or alternatively a Mapping with some required keys? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
One way to do this is to have a 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
}
}
} |
Beta Was this translation helpful? Give feedback.
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.