1

I'm playing around with dhall, trying to represent an existing large yaml file I have instead in dhall.

Specifically, I'm trying to build dhall types and helpers for a concourse pipeline definition. Part of the yaml that defines a concourse pipeline looks like the yaml I've written here. Each resource in my list contains a name, a type and then a source whose structure depends entirely on the type of the resource.

There is no exhaustive list of resources for me to define since someone could create a new one tomorrow and I wouldn't want to update my types each time some third party creates a new source type.

The dhall I have for this is witten here too, but I'm unsure how I could represent source. I had considered omitting this field from my type and instructing consumers to use dhall's // operator to add a source, but then it becomes difficult to embed resources in a [resource] and still have type checking.

How can I define a dhall type for resource that contains within it a field whose value is unstructured.

resources:
- name: my-repo
  type: git
  source:
    $some_unstructured_yaml
{ name   : Text
, type   : Text
, source : Optional ???
}
BooleanCat
  • 21
  • 2
  • Your YAML is invalid. Your final sentence should be changed to a direct question (i.e. one about how to solve your problem, not if someone exists that can solve your problem). – Anthon May 12 '19 at 11:47
  • Changed the last sentence to a direct question. The yaml is literally wrong sure, but that's because I'm trying to convey that source could be anything. I'll use notation other than {} to be clearer – BooleanCat May 12 '19 at 18:46
  • You might want to checkout the Map in the dhall Prelude or perhaps the JSON types, also in the Prelude – sbdchd Jul 07 '19 at 04:00

1 Answers1

2

This will be possible in the next release (version 1.3.1) of dhall-json.

For example, given this schema:

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

... and this YAML configuration:

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

... it will produce this Dhall expression:

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

For more details, see the following change to the standard:

... and the following change to the dhall-json package:

Gabriella Gonzalez
  • 34,863
  • 3
  • 77
  • 135