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

remove null from generated JSON #39

Open
sellout opened this issue May 19, 2024 · 1 comment · May be fixed by #42
Open

remove null from generated JSON #39

sellout opened this issue May 19, 2024 · 1 comment · May be fixed by #42

Comments

@sellout
Copy link

sellout commented May 19, 2024

I‘m trying to conditionalize some non-list values, like

  if impl(ghc >= 9.10.1)
    main-is: skipped.hs
  else
    main-is: doctests.hs

Encoding this in Dhall, I end up with

when =
  [ { condition = "impl(ghc >= 9.10.1"
    , then = hpack.emptyBuildOptions // { main = Some "skipped.hs" }
    , else = hpack.emptyBuildOptions // { main = Some "doctests.hs" }
    }
  ]

and emptyBuildOptions (which allows me to have a list of conditions that don’t all have to specify every field) looks like

      { cpp-options = [] : List Text
      , default-extensions = [] : List Text
      , dependencies = [] : List Text
      , ghc-options = [] : List Text
      , main = None Text
      }

The problem is that when there are conditions that don’t specify main, it ends up with main = None Text, which becomes "main": null in the generated JSON, and then hpack (reasonably) complains that it doesn’t know what to do with a null.

I think the solution here is to change dhallToJSON expr in

textToJson settings text = runExceptT $ do
expr <- liftIO $ check settings text
_ <- liftResult $ typeOf expr
liftResult $ ([],) <$> dhallToJSON expr
where
liftResult :: (Show b, Monad m) => Either b a -> ExceptT String m a
liftResult = ExceptT . return . first show
to Dhall.JSON.omitNull (dhallToJSON expr).

It might make sense to use Dhall.JSON.omitEmpty instead of omitNull, but I haven’t thought through the ramifications of that.

sellout added a commit to sellout/hpack-dhall that referenced this issue May 20, 2024
sellout added a commit to sellout/hpack-dhall that referenced this issue May 20, 2024
@sellout
Copy link
Author

sellout commented May 20, 2024

omitEmpty is the wrong thing. E.g.,

{ condition = "impl(ghc >= 9.10.1)"
, `then` = { ghc-options = "-Wall" }
, `else` = { }
}

produces

if impl(ghc >= 9.10.1)
  ghc-options = "-Wall"
else

which is valid Cabal. But with omitEmpty, the else is dropped, and then hpack complains about an invalid conditional.

The conditional could alternatively be written

{ condition = "impl(ghc >= 9.10.1)"
, ghc-options = "-Wall"
}

but I don’t think there’s any reason to force the user to do that.

@sellout sellout linked a pull request May 20, 2024 that will close this issue
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.

1 participant