我将从一个激动人心的配置示例开始,它几乎代表了特使代理配置:)
virtual_hosts:
- name: webxp-api_http
domains: ["*"]
routes:
- match: { prefix: "/static/v5.0" }
route: { cluster: bodhi_static }
- match: { prefix: "/"}
route: { cluster: bodhi-web }
clusters:
- name: bodhi_web
- name: bodhi_static规则是,必须定义该名称clusters列表才能在配置的route部分中使用。如果仔细观察,该配置将无法加载,因为bodhi_web不是bodhi-web。我该如何在Dhall中编码它呢?
一方面,我可以将clusters作为let绑定中的一个列表,这会有所帮助,但并不会强迫我使用该绑定,实际上,我希望将clusters视为cluster:字段的一个sum类型?依赖类型对我有帮助吗(例如,我记得在purescript中做过这样的事情,对于依赖类型编程有一些有限的容量)
或者我应该只创建一个构造函数/验证器函数并滥用断言来验证它?
或者我就是不应该?:)
发布于 2019-12-05 00:58:12
我会通过编写一个实用函数来实现这一点,该函数可以生成按构造更正的配置。
以您的示例为例,如果我们想确保clusters字段下面的列表始终与路由列表匹配,那么我们从routes字段派生clusters字段:
let Prelude = https://prelude.dhall-lang.org/package.dhall
let Route = { match : { prefix : Text }, route : { cluster : Text } }
let toVirtualHosts =
\(args : { name : Text, domains : List Text, routes : List Route })
-> { virtual_hosts =
args
// { clusters =
Prelude.List.map
Route
Text
(\(r : Route) -> r.route.cluster)
args.routes
}
}
in toVirtualHosts
{ name = "webxp-api_http"
, domains = [ "*" ]
, routes =
[ { match = { prefix = "/static/v5.0" }
, route = { cluster = "bodhi_static" }
}
, { match = { prefix = "/" }
, route = { cluster = "bodhi_web" }
}
]
}$ dhall-to-yaml --file ./example.dhall
virtual_hosts:
clusters:
- bodhi_static
- bodhi_web
domains:
- *
name: webxp-api_http
routes:
- match:
prefix: /static/v5.0
route:
cluster: bodhi_static
- match:
prefix: /
route:
cluster: bodhi_web发布于 2019-12-14 00:21:23
我的替代方案很大程度上依赖于这样一个事实,即当转换为yaml时,空的替代方案最终将是文本,即:{cluster = <static | web>.static}被解释为cluster: static
这意味着,我可以:
let Clusters = < bodhi_static | bodhi_web >
let Route =
{ Type = { match : { prefix : Text }, cluster : Clusters }
, default = {=}
}
let Cluster = { Type = { name : Clusters }, default = {=} }
in { matches =
[ Route::{ match = { prefix = "/" }, cluster = Clusters.bodhi_web }
, Route::{
, match = { prefix = "/static" }
, cluster = Clusters.bodhi_static
}
]
, clusters =
[ Cluster::{ name = Clusters.bodhi_static }
, Cluster::{ name = Clusters.bodhi_web }
]
}更多的重复,但更简单的i.m.o?
https://stackoverflow.com/questions/59156540
复制相似问题