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

Add JSON.field to prelude #1143

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add JSON.field to prelude #1143

wants to merge 2 commits into from

Conversation

SiriusStarr
Copy link
Collaborator

JSON.field is a helper function for making JSON.object entries, a simple convenience that helps with constructing basically any JSON in Dhall. This function is in every JSON interface I've made in Dhall and seems reasonable for inclusion in the prelude.

Since the map value is always JSON.Type for JSON objects, it's more convenient to have this function that having to write JSON.keyValue JSON.Type every single time you construct a field for an object (or to write out the map entry by hand).

For example, the basic example object goes from

let JSON = ./package.dhall

in  JSON.render
      ( JSON.object
          [ { mapKey = "foo", mapValue = JSON.double 1.0 }
          , { mapKey = "bar", mapValue = JSON.bool True }
          ]
      )

to

let JSON = ./package.dhall

in  JSON.render
      ( JSON.object
          [ JSON.field "foo" (JSON.double 1.0)
          , JSON.field "bar" (JSON.bool True)
          ]
      )

with this new function.

JSON.field is a helper function for making JSON.object entries
@@ -0,0 +1,27 @@
{-|
Create a JSON field for a JSON object from a Dhall `Text` key and a JSON value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth mentioning in this documentation that another alternative for people interested in this is to use toMap:

let JSON = ./package.dhall

in  JSON.render
      ( JSON.object
          ( toMap
              { foo = JSON.double 1.0
              , bar = JSON.bool True
              }
          )
      )

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am...not smart and completely forgot the toMap builtin existed. In light of that, do you still think JSON.field is a useful addition, or just clutter in the Prelude?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SiriusStarr: In that case, maybe we don't need the JSON.field utility, or at least wait to see if somebody requests it even knowing about toMap

@philandstuff
Copy link
Collaborator

philandstuff commented Mar 8, 2021 via email

@SiriusStarr
Copy link
Collaborator Author

I have previously written a similar utility which takes an Optional x and
provides a JSON map entry in a list of length one if present, or an empty
list if absent. Then these optional entries can be pasted together with #
(using toMap for the non-optional entries).

(Having done the same previously) this might honestly be a better prelude inclusion than field, since there's little reason to not just use toMap for non-optionals, but that doesn't work for fields you want to optionally exclude.

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

Successfully merging this pull request may close these issues.

None yet

5 participants