我正在尝试使用Dhall来生成AWS Cloudformation,我尝试编码的第一件事是AWS::ApiGatewayV2::Api。它有以下json规范:
{
"Type" : "AWS::ApiGatewayV2::Api",
"Properties" : {
"ApiKeySelectionExpression" : String,
"BasePath" : String,
"Body" : Json,
"BodyS3Location" : BodyS3Location,
"CorsConfiguration" : Cors,
"CredentialsArn" : String,
"Description" : String,
"DisableSchemaValidation" : Boolean,
"FailOnWarnings" : Boolean,
"Name" : String,
"ProtocolType" : String,
"RouteKey" : String,
"RouteSelectionExpression" : String,
"Tags" : Json,
"Target" : String,
"Version" : String
}
}
该规范有多个字段,但其中两个形成一个并集:BodyS3Location
和Body
。意味着他们中的任何一个都应该存在。我知道对动态记录的支持,但显然仅适用于具有单个记录的对象。对此行为进行编码的推荐方法是什么?
这可能不能很好地概括,但对于这个特定示例,您可以这样做:
let JSON = https://prelude.dhall-lang.org/v12.0.0/JSON/package.dhall
-- These are just placeholders for whatever the real types are
let BodyS3Location = {}
let Cors = {}
let Shared =
{ ApiKeySelectionExpression : Text
, BasePath : Text
, CorsConfiguration : Cors
, CredentialsArn : Text
, Description : Text
, DisableSchemaValidation : Bool
, FailOnWarnings : Bool
, Name : Text
, ProtocolType : Text
, RouteKey : Text
, RouteSelectionExpression : Text
, Tags : JSON.Type
, Target : Text
, Version : Text
}
in < A : Shared //\ { BodyS3Location : BodyS3Location }
| B : Shared //\ { Body : JSON.Type }
>