Skip to content

Conversation

amosr-msft
Copy link

Hello,
I'm working on a project where we are generating datatypes for a given Dhall schema. It would be helpful for us if dhall-json could generate a JSON representation of types, as well as values. Would you be interested in adding something like this?

This PR has two changes: I've modified dhallToJSON to be able to emit value types, and added a --type option to dhall-to-json.

I think this representation should be unambiguous and round-trippable, but I'm happy to use any representation. I have only briefly read about JSON schemas, to be honest, but I thought that a Dhall-specific type encoding would be better to faithfully preserve the original Dhall type.

The extra --type option is just a shorthand which might be useful, but we can easily make do without the option if it's not likely to be useful to anyone else.

Here's an example showing how the output looks: for an input schema:

{
    field: Text,
    nested: { nested_field: Natural },
    list: List Bool,
    optional: Optional Text,
    union: < A: Natural | B: Text | C >
}

This gives the following JSON type:

$ dhall-to-json --file dhall/schema_test.dhall
{
  "fields": {
    "field": "Text",
    "list": {
      "element": "Bool",
      "type": "List"
    },
    "nested": {
      "fields": {
        "nested_field": "Natural"
      },
      "type": "Record"
    },
    "optional": {
      "element": "Text",
      "type": "Optional"
    },
    "union": {
      "choices": {
        "A": "Natural",
        "B": "Text",
        "C": {}
      },
      "type": "Union"
    }
  },
  "type": "Record"
}

@amosr-msft
Copy link
Author

Hi @Gabriella439 , I hope I'm not pestering you, I just wanted to ping you in case you'd missed this - do you think this would be a reasonable feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant