1

我正在尝试使用 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
  }
}

该规范有多个字段,但其中两个形成一个联合:BodyS3LocationBody. 意味着其中任何一个都应该存在。我知道对动态记录的支持,但显然只适用于具有单个记录的对象。编码这种行为的推荐方法是什么?

4

1 回答 1

2

这可能无法很好地概括,但对于这个特定的示例,您可以这样做:

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 }
    >
于 2020-01-24T05:24:40.923 回答