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

Empty alternative does not trigger JSON name preservation #1201

Closed
wezm opened this issue Aug 5, 2019 · 3 comments · Fixed by #1204
Closed

Empty alternative does not trigger JSON name preservation #1201

wezm opened this issue Aug 5, 2019 · 3 comments · Fixed by #1204
Assignees

Comments

@wezm
Copy link

wezm commented Aug 5, 2019

Possibly related to #1080, #1145

As per the dhall-json tests nesting{0,1}, the use of a record with fields field, nesting, and contents is supposed to preserve the name of the union alternative alongside the value, either Inline or Nested. However the behaviour does not trigger when the union alternative is empty.

For example, with this input:

let Example = < Left : { foo : Natural } | Middle | Right : { bar : Bool } >

let Nesting = < Inline | Nested : Text >

in  { field    = "name"
    , nesting  = Nesting.Inline
    , contents = Example.Middle
    }

I expect this output:

{ "name": "Middle" }

But instead get:

{ "field": "name", "contents": "Middle", "nesting": "Inline" }

I have added a test case for this in my fork but I'm not familiar enough with Haskell to implement a fix: master...wezm:json-nesting-empty-alternative

@sjakobi
Copy link
Collaborator

sjakobi commented Aug 5, 2019

Thanks for the testcase! That expected output looks very reasonable.

We should also handle empty alternatives with the Nested layout. For

let Example = < Left : { foo : Natural } | Middle | Right : { bar : Bool } >

let Nesting = < Inline | Nested : Text >

in  { field    = "name"
    , nesting  = Nesting.Nested "value"
    , contents = Example.Middle
    }

I'd propose generating { "name": "Middle", "value": {} } by default. With --omitEmpty that would become just { "name": "Middle" }.

I'll submit a PR soon.

@sjakobi sjakobi self-assigned this Aug 5, 2019
@sjakobi
Copy link
Collaborator

sjakobi commented Aug 5, 2019

I'd propose generating { "name": "Middle", "value": {} } by default.

Actually that should probably be { "name": "Middle", "value": null } since Nested already supports non-record alternatives like < Foo : Text >.Foo "bar".

@Gabriel439 Thoughts?

@Gabriella439
Copy link
Collaborator

@sjakobi: Yeah, null seems safer. Also, null can be combined with the --omitNull flag so that the value field can be filtered out entirely at the user's discretion

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 a pull request may close this issue.

3 participants