我使用以下 JSON 来表示我们的基础架构。这是一个大大简化的版本。
{
"us-east-1": {
"qa": {
"etcd": {}
}
},
"eu-central-1": {
"dev": {
"etcd": {}
}
},
"eu-west-1": {
"prod": {
"etcd": {}
}
}
}
这可以映射到文件夹,并且很容易从代码中处理。
#!/usr/bin/env python
import json
import os, sys
with open('infrastructure.json', 'r') as read_file: infra = json.load(read_file)
regions = infra.keys()
for region in regions:
stages = infra[region].keys()
for stage in stages:
apps = infra[region][stage].keys()
for app in apps:
os.makedirs(os.path.join('infra', region, stage, app), 0o750, exist_ok=True)
print('region: {}, stage: {}, app: {}'.format(region, stage, app))
但是,我想使用 Dhall 生成此文件以将可能的键限制为列表。
Dhall 对此表示支持:
let AwsRegions : Type = < us-east-1 | eu-central-1 | eu-west-1 >
let Stages : Type = < dev | qa | prod >
let Applications : Type = < etcd | postgresql | hadoop >
唯一的问题是我找不到将记录键作为空类型的支持,只有值可以限制为那些。
这使我拥有以下方式的基础设施:
let infrastructure :
List { region: AwsRegions, stage: Stages,
applications: List Applications } =
[ { region = AwsRegions.us-east-1,
stage = Stages.dev,
applications = [
Applications.hadoop,
Applications.etcd ] },
{ region = AwsRegions.eu-west-1,
stage = Stages.dev,
applications = [
Applications.hadoop,
Applications.etcd ] },
{ region = AwsRegions.eu-central-1,
stage = Stages.dev,
applications = [
Applications.hadoop,
Applications.etcd ] },
{ region = AwsRegions.eu-west-1,
stage = Stages.qa,
applications = [
Applications.hadoop,
Applications.etcd ] },
{ region = AwsRegions.eu-west-1,
stage = Stages.prod,
applications = [
Applications.hadoop,
Applications.etcd ] } ]
in infrastructure
从代码中处理生成的 JSON 有点困难,我不能像其他表示一样轻松地引用我们基础设施的子集。
有没有办法让 Dhall 拥有像枚举(空类型的联合)这样的键?