dhall-haskell icon indicating copy to clipboard operation
dhall-haskell copied to clipboard

Addressing excessive usage of `Optional` values.

Open spion opened this issue 3 years ago • 3 comments

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 defaults
  • dhall-to-yaml (and JSON) could receive --remove-defaults which would in turn strip the default values from the output

spion avatar Jul 22 '22 13:07 spion

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

Gabriella439 avatar Jul 24 '22 18:07 Gabriella439

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.

spion avatar Jul 25 '22 13:07 spion

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

Gabriella439 avatar Aug 15 '22 00:08 Gabriella439