我需要键入并生成一个包含字符串枚举值的配置。我设法定义了以下几行。最后,我需要将联合转换为字符串值。
let ParameterLocation = < Query : {}
| Header : {}
| Path : {}
| Cookie : {}
>
let ParameterObject = {
name : Text,
`in` : ParameterLocation,
required : Bool
}
let locationToText = \(loc : ParameterLocation) -> merge {
Query = \(_ : {}) -> "query",
Header = \(_ : {}) -> "header",
Path = \(_ : {}) -> "path",
Cookie = \(_ : {}) -> "cookie"
} loc
let t : ParameterObject = {
name = "organisation_id",
`in` = ParameterLocation.Query {=},
required = False
}
in t // { `in` = locationToText t.`in` }
这里包含联合/枚举的记录位于顶部,因此我可以很容易地访问它,但在最终配置中,ParameterObject si 嵌套得很深。
有没有办法
- “遍历”任意记录结构并在任何适用的地方应用 locationToText ?
- 或者用打印机提供 dhall-to-json/dhall-to-yaml 以获得这样的价值?
- 还是更好的方法来定义我的枚举以更轻松地实现我的目标?