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 a better error message when decoding a bare None #895

Merged
merged 1 commit into from
Apr 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 41 additions & 14 deletions dhall-json/src/Dhall/JSON.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

{-| This library only exports a single `dhallToJSON` function for translating a
Dhall syntax tree to a JSON syntax tree (i.e. a `Value`) for the @aeson@
Expand Down Expand Up @@ -178,7 +177,6 @@ import Control.Exception (Exception, throwIO)
import Data.Aeson (Value(..), ToJSON(..))
import Data.Monoid ((<>), mempty)
import Data.Text (Text)
import Data.Typeable (Typeable)
import Dhall.Core (Expr)
import Dhall.TypeCheck (X)
import Dhall.Map (Map)
Expand All @@ -202,19 +200,43 @@ import qualified Options.Applicative
Because the majority of Dhall language features do not translate to JSON
this just returns the expression that failed
-}
data CompileError = Unsupported (Expr X X) deriving (Typeable)
data CompileError
= Unsupported (Expr X X)
| BareNone

instance Show CompileError where
show BareNone =
Data.Text.unpack $
_ERROR <> ": A bare ❰None❱ is not valid \n\
Copy link
Member

Choose a reason for hiding this comment

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

❰None❱ on its own is not valid

\ \n\
\Explanation: The conversion to JSON/YAML does not accept ❰None❱ in isolation as \n\
\a valid way to represent ❰null❱. In Dhall, ❰None❱ is a function whose input is \n\
\a type and whose output is an absent value of that type. \n\
Copy link
Member

Choose a reason for hiding this comment

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

The “output is an absent value of that type” formulation is a bit confusing.

whose output is an `Optional` of that type

\ \n\
\For example: \n\
\ \n\
\ \n\
\ ┌─────────────────────────────────┐ This is a function whose result is an \n\
Copy link
Member

Choose a reason for hiding this comment

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

`None` is a function

\ │ None : ∀(a : Type) → Optional a │ ❰Optional❱ value, but the function \n\
\ └─────────────────────────────────┘ itself is not a valid ❰Optional❱ value \n\
\ \n\
\ \n\
\ ┌─────────────────────────────────┐ \n\
\ │ None Natural : Optional Natural │ This is a valid ❰Optional❱ value (an \n\
Copy link
Member

Choose a reason for hiding this comment

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

`None Natural` is a valid

\ └─────────────────────────────────┘ absent ❰Natural❱ number in this case) \n\
\ \n\
\ \n\
\The conversion to JSON/YAML only translates the latter form to ❰null❱. "
Copy link
Member

Choose a reason for hiding this comment

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

only translates the fully applied form (e.g. `None Natural`)

show (Unsupported e) =
Data.Text.unpack $
"" <> _ERROR <> ": Cannot translate to JSON \n\
\ \n\
\Explanation: Only primitive values, records, unions, ❰List❱s, and ❰Optional❱ \n\
\values can be translated from Dhall to JSON \n\
\ \n\
\The following Dhall expression could not be translated to JSON: \n\
\ \n\
\↳ " <> txt <> " "
_ERROR <> ": Cannot translate to JSON \n\
\ \n\
\Explanation: Only primitive values, records, unions, ❰List❱s, and ❰Optional❱ \n\
\values can be translated from Dhall to JSON \n\
\ \n\
\The following Dhall expression could not be translated to JSON: \n\
\ \n\
\↳ " <> txt <> " "
where
txt = Dhall.Core.pretty e

Expand Down Expand Up @@ -257,6 +279,11 @@ dhallToJSON e0 = loop (Dhall.Core.normalize e0)
return (toJSON a')
Dhall.Core.App Dhall.Core.None _ -> do
return Data.Aeson.Null
-- Provide a nicer error message for a common user mistake.
--
-- See: https://github.com/dhall-lang/dhall-lang/issues/492
Dhall.Core.None -> do
Left BareNone
Dhall.Core.RecordLit a ->
case toOrderedList a of
[ ( "contents"
Expand Down