首页 > 解决方案 > Dynamic records key type

问题描述

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

标签: dhall

解决方案


您的问题启发了添加此功能,该功能将在下一个版本(版本 1.25.0)中提供。看:

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

这也将在另一个方向上起作用,这意味着{json,yaml}-to-dhall如果架构要求,它将能够将记录键解码为联合:

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

推荐阅读