首页 > 解决方案 > dhall-to-yaml:表示嵌套在结构化 yaml 中的非结构化块

问题描述

我正在玩 dhall,试图代表我在 dhall 中拥有的现有大型 yaml 文件。

具体来说,我正在尝试为大厅管道定义构建 dhall 类型和助手。定义大厅管道的部分 yaml 看起来像我在这里编写的 yaml。我列表中的每个资源都包含一个名称、一个类型和一个源,其结构完全取决于资源的类型。

没有详尽的资源列表供我定义,因为明天有人可能会创建一个新的资源,而且我不想每次第三方创建新的源类型时都更新我的类型。

我为此拥有的 dhall 也在这里,但我不确定我该如何代表source。我曾考虑在我的类型中省略该字段并指示消费者使用 dhall 的//运算符添加源,但随后很难将resources 嵌入 a[resource]并仍然进行类型检查。

如何为包含非结构化字段的资源定义 dhall 类型。

resources:
- name: my-repo
  type: git
  source:
    $some_unstructured_yaml
{ name   : Text
, type   : Text
, source : Optional ???
}

标签: yamldhall

解决方案


这将在dhall-json.

例如,给定这个模式:

-- This will become: https://prelude.dhall-lang.org/JSON/Type
let JSON = https://raw.githubusercontent.com/dhall-lang/dhall-lang/40c3e57a4f09448b5a7c9d203a81b64f50ed30bd/Prelude/JSON/Type

in  { name   : Text
    , type   : Text
    , source : Optional JSON
    }

...以及此 YAML 配置:

name: my-repo
type: git
source:
  foo:
  - 1
  - bar: true
  baz: null

...它将产生这个 Dhall 表达式:

$ yaml-to-dhall ./schema.dhall < example.yml
{ name =
    "my-repo"
, source =
    Some
    (   λ(JSON : Type)
      → λ ( json
          : { array :
                List JSON → JSON
            , bool :
                Bool → JSON
            , null :
                JSON
            , number :
                Double → JSON
            , object :
                List { mapKey : Text, mapValue : JSON } → JSON
            , string :
                Text → JSON
            }
          )
      → json.object
        [ { mapKey =
              "foo"
          , mapValue =
              json.array
              [ json.number 1.0
              , json.object [ { mapKey = "bar", mapValue = json.bool True } ]
              ]
          }
        , { mapKey = "baz", mapValue = json.null }
        ]
    )
, type =
    "git"
}

有关更多详细信息,请参阅对标准的以下更改:

...以及对dhall-json包的以下更改:


推荐阅读