Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic records key type

Tags:

dhall

I'm trying to generate some YAML containing a map with dynamic keys, as described here. This works if I use Text keys, but not when the keys have any other type. I'd like to use a union type for the keys if possible.

I've tried using different types for mapKey, including a union type and Natural, but with no success. I can work around the problem by converting all mapKey values to Text, but this isn't ideal.

Here's a minimal example of what I'm trying to do:

let Union = <A | B>
in
[{mapKey = Union.A, mapValue = "foo"}]

I'd expect it to generate YAML to be:

A: foo

but instead, the generated YAML looks like this:

- mapKey: A
  mapValue: foo
like image 266
DaveWM Avatar asked Aug 19 '26 15:08

DaveWM


1 Answers

Your question inspired adding this feature, which will be available in the next release (Version 1.25.0). See:

https://github.com/dhall-lang/dhall-haskell/pull/1094

This will also work in the other direction, meaning that {json,yaml}-to-dhall will be able to decode record keys into a union if requested to by the schema:

$ json-to-dhall 'List { mapKey : < A | B >, mapValue : Natural }' <<< '{"A": 1, "B": 2}'
[ { mapKey = < A | B >.A, mapValue = 1 }, { mapKey = < A | B >.B, mapValue = 2 } ]
like image 148
Gabriella Gonzalez Avatar answered Aug 27 '26 01:08

Gabriella Gonzalez