dhall-haskell
dhall-haskell copied to clipboard
Addressing excessive usage of `Optional` values.
One of the complaints I receive when introducing Dhall to other developers is the overuse of Optional fields.
I believe these could be more easily avoided, especially during migrations from yaml or json if the tools provided some more options. For example:
yaml-to-dhall(and from JSON) could receive a Schema (a pair of Type and defaults) and fill-in the missing fields with the defaultsdhall-to-yaml(and JSON) could receive --remove-defaults which would in turn strip the default values from the output
yaml-to-dhall does accept a type (not a schema) and will fill in missing fields. For example:
$ yaml-to-dhall '{ x : Optional Natural, y : Optional Natural }' <<< 'x: 1'
{ x = Some 1, y = None Natural }
… and you can pipe that through dhall rewrite-with-schemas to elide optional values when a schema is present. Here is an example of doing that in the context of dhall-kubernetes:
https://github.com/Gabriella439/slides/blob/9c66543a55b67e195ccc15a8caa6bdedc344b904/dhall-intro/dhall-intro.md#real-world-example---yaml-to-dhall
Also, dhall-to-yaml has --omit-empty which I believe does what you requested with --remove-defaults
I meant avoiding optionals all-together by using a different set of defaults. This would look something like this:
yaml-to-dhall '{ Type: {x: Natural, y: Natural}, default: {x: 1, y: 1}}' <<< 'x: 2'
result:
{ x = 2, y = 1 }
I presume rewrite-with-schemas already works with defaults, rather then optionals, so that will result with
let schema = '../schema'
schema::MySchema {
x = 2
}
Similarly dhall-to-yaml would omit defaults, i.e. dhall-to-yaml of the above with a specified schema:
would emit
x: 2
In the above, y is not Optional, but dhall-to-yaml knows that according to the schema it has the default value, so it can be omitted from the generated yaml. This could also be optional i.e. only if --omit-defaults is specified.
Oh, I see what you mean now. Yeah, I would accept a PR to implement something like that, but I likely would not have time to do it myself