如何使用Dhall在字段子集上对并集进行编码?

时间:2020-01-23 14:11:54

标签: 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的支持,但显然仅适用于具有单个记录的对象。建议使用什么方式编码此行为?

1 个答案:

答案 0 :(得分: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 }
    >