Skip to content

Define class with variable number of properties #881

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

You must be logged in to vote

This isn't currently possible in Pkl. One way this sort of pattern can be implemented is via output converters:

class Example {
    description: String
    extraProperties: Dynamic // or Mapping, as appropriate
}

example = new Example {
    description = "this is a test"
    extraProperties {
        additional_prop = "I am"
        another_prop = "flattening"
        last_prop = "a listing"
    }
}

output {
    renderer {
        converters {
            [Example] = (it) -> it.toMap().remove("extraProperties") + it.extraProperties.toMap().remove("description")
        }
    }
}

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@StefMa
Comment options

@HT154
Comment options

@ghyatzo
Comment options

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