Jsonnet is a templating language and tool to generate JSON/YAML files. Since already have a language
instead of configuration
, we can generate a bunch of configuration issues with simple code.
For example, I use loop
in Jsonnet to write repeat items:
local food_type = ['cake', 'fruit', 'vegetable', 'rice', 'eggs', 'milk', 'meat', 'pizza'];
local final_meal = [
{ name: 'food_' + type, value: 'like' }
for type in food_type
];
{ 'sample.yaml': std.manifestYamlDoc(
final_meal,
) }
The std.manifestYamlDoc
is used for generate YAML file instead of JSON.
After run jsonnet sample.jsonnet -m . -S
, it will generate sample.yaml
as:
- "name": "food_cake"
"value": "like"
- "name": "food_fruit"
"value": "like"
- "name": "food_vegetable"
"value": "like"
- "name": "food_rice"
"value": "like"
- "name": "food_eggs"
"value": "like"
- "name": "food_milk"
"value": "like"
- "name": "food_meat"
"value": "like"
- "name": "food_pizza"
"value": "like"
Less code but more human readable. I think this is the power of Jsonnet.