Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

WIP main: factor the top-level as textToYAML #20

Merged
merged 1 commit into from
Mar 12, 2018

Conversation

deepfire
Copy link
Contributor

@Gabriel439, this is a suggested refactoring -- would you feel like including something like this?

If so, what improvements (besides the obvious lack of a docstring) would you suggest?

@Gabriella439
Copy link
Collaborator

Perhaps simplify the utility to only encompass this logic:

codeToValue :: Text -> Text -> IO Value
codeToValue name code = do
    expr <- case Dhall.Parser.exprFromText (Directed name 0 0 0 0) code of
              Left  err  -> Control.Exception.throwIO err
              Right expr -> return expr

    expr' <- Dhall.Import.load expr
    case Dhall.TypeCheck.typeOf expr' of
      Left  err -> Control.Exception.throwIO err
      Right _   -> return ()

    case Dhall.JSON.dhallToJSON expr' of
      Left  err  -> Control.Exception.throwIO err
      Right json -> return json

Then if the user wants detailed error messages they can wrap that in detailed and if they want to omit null then they wrap the result in omitNull. Also, returning Value has the advantage that you can generate both YAML and JSON from the same utility.

@deepfire deepfire force-pushed the master branch 2 times, most recently from 8ad6cb4 to d031a22 Compare March 12, 2018 18:09
@deepfire
Copy link
Contributor Author

@Gabriel439, does that one look better?

@@ -0,0 +1,20 @@
{-# LANGUAGE DeriveDataTypeable #-}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think you need this module any longer. All it does is re-export codeToValue from the Dhall.JSON module

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, removed it.

then Dhall.JSON.omitNull json
else json
Data.ByteString.putStr (Data.Yaml.encode filteredJSON) ))
Data.ByteString.putStr =<< (Data.Yaml.encode . omittingNull <$>) . explaining . (Dhall.JSON.codeToValue "(stdin)")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd prefer to use do notation here to split this up over multiple lines. This is difficult to read

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@deepfire deepfire force-pushed the master branch 5 times, most recently from 49072b5 to ad57699 Compare March 12, 2018 18:31
@deepfire
Copy link
Contributor Author

Added a provisional docstring to textToValue.

dhall-json.cabal Outdated
aeson >= 1.0.0.0 && < 1.4 ,
dhall >= 1.11.0 && < 1.12,
text >= 0.11.1.0 && < 1.3 ,
unordered-containers < 0.3
trifecta >= 1.6 && < 1.8 ,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you can now remove the trifecta dependency from the dhall-to-json and dhall-to-yaml executables

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

-}
codeToValue :: Data.Text.Text -> Data.Text.Text -> IO Value
codeToValue name code = do
let textBS = Data.ByteString.UTF8.fromString . Data.Text.unpack
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oops! Perhaps I should have suggested that the first argument have type ByteString that way we don't need to convert from Text using the utf8-string package

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

dhall-json.cabal Outdated
trifecta >= 1.6 && < 1.8 ,
unordered-containers < 0.3 ,
utf8-string ,
yaml >= 0.5.0 && < 0.9
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the library section doesn't need to depend on the yaml package any longer now that codeToValue returns a Value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

{-| Convert a piece of Text carrying a Dhall inscription to an equivalent JSON Value

>>> :set -XOverloadedStrings
>>> :set -XOverloadedLists
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this example doesn't require -XOverloadedLists

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

>>> Dhall.JSON.codeToValue "(stdin)" "{ a = 1 }"
>>> Object (fromList [("a",Number 1.0)])
-}
codeToValue :: Data.Text.Text -> Data.Text.Text -> IO Value
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't forget to document what each argument represents

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@deepfire deepfire force-pushed the master branch 5 times, most recently from e393ba3 to 212f2c6 Compare March 12, 2018 19:20
@deepfire
Copy link
Contributor Author

All done and CI passes : -)

@Gabriella439
Copy link
Collaborator

Looks great! Thanks for doing this :)

@Gabriella439 Gabriella439 merged commit d6adaa2 into dhall-lang:master Mar 12, 2018
@deepfire
Copy link
Contributor Author

Thank you for your feedback!

@Gabriella439
Copy link
Collaborator

You're welcome! :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants