Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addressing excessive usage of Optional values. #2434

Open
spion opened this issue Jul 22, 2022 · 3 comments
Open

Addressing excessive usage of Optional values. #2434

spion opened this issue Jul 22, 2022 · 3 comments

Comments

@spion
Copy link

spion commented Jul 22, 2022

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 spion changed the title Excessive usage of Optional values. Addressing excessive usage of Optional values. Jul 22, 2022
@Gabriella439
Copy link
Collaborator

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

@spion
Copy link
Author

spion commented Jul 25, 2022

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.

@Gabriella439
Copy link
Collaborator

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

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

No branches or pull requests

2 participants