I am developing a system where we create documents that are created from json-files. The json files are described by a json schema of the following kind, where a key either could have a static default value or have one of multiple enumerated values:
{
"name" : { "default" : "John Doe" },
"bilingual" : { "type" : "boolean" },
"kind_of_document" : {
"type" : "integer",
"enum" : [0, 1, 2]
}
}
What I want to do is to create test files for all possible combinations of values from the schema. In the above case there would be six different json files:
{ "name" : "John Doe", "bilingual" : true, "kind_of_document" : 0 }
{ "name" : "John Doe", "bilingual" : true, "kind_of_document" : 1 }
{ "name" : "John Doe", "bilingual" : true, "kind_of_document" : 2 }
{ "name" : "John Doe", "bilingual" : false, "kind_of_document" : 0 }
{ "name" : "John Doe", "bilingual" : false, "kind_of_document" : 1 }
{ "name" : "John Doe", "bilingual" : false, "kind_of_document" : 2 }
This is a simplified version; in a normal case there may be 20-30 keys, of which most have default values, but there may still 5-10 keys that can have multiple values. They number of keys may vary from one document to another.
The problem: How do I iterate over the list of keys to generate all possible combinations of values? I suppose that some form of recursion is the way to go, but I can't figure out what the algorithm should be. It seems to me that this should be a fairly common problem, so somebody has most likely solved it before.
I have tried googling for it, but I don't know how to formulate the query to avoid just general questions about generating combinations of list elements, such as
How to get all possible combinations of a list’s elements?
I have also looked at itertools, but I am not sure how it could me solve the problem: https://docs.python.org/3/library/itertools.html#itertools.permutations
I am writing this in python, but of course the general algorithm for solving this would be language independent.