如何使用 Dhall 对字段子集进行联合编码?

How to encode union on the subset of fields using Dhall?

我正在尝试使用 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。意味着它们中的任何一个都应该存在。我知道对 dynamic records 的支持,但显然只适用于具有单个记录的对象。对这种行为进行编码的推荐方法是什么?

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

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 }
    >