summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabrielGonzalez <>2019-03-07 04:56:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2019-03-07 04:56:00 (GMT)
commitd1bfef4b31850bacd2e27e7e4730036b75e5cddb (patch)
tree6bdaf6be130131836e4dbfcb1b5df8f961f07a34
parent915f9fb4de4f71677c518db68060069b3439a8af (diff)
version 1.2.7HEAD1.2.7master
-rw-r--r--CHANGELOG.md7
-rw-r--r--dhall-json.cabal7
-rw-r--r--dhall-to-json/Main.hs30
-rw-r--r--dhall-to-yaml/Main.hs28
-rw-r--r--src/Dhall/JSON.hs56
5 files changed, 76 insertions, 52 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 85beec4..4ace5ef 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,10 @@
+1.2.7
+
+* Build against `dhall-1.21.0`
+* Support GHC 7.10.3
+ * See: https://github.com/dhall-lang/dhall-haskell/pull/814
+* Add new `--omitEmpty` flag for omitting nulls and empty records
+
1.2.6
* Add `--version` flag
diff --git a/dhall-json.cabal b/dhall-json.cabal
index 8d14f7c..f2855e6 100644
--- a/dhall-json.cabal
+++ b/dhall-json.cabal
@@ -1,8 +1,8 @@
Name: dhall-json
-Version: 1.2.6
+Version: 1.2.7
Cabal-Version: >=1.8.0.2
Build-Type: Simple
-Tested-With: GHC == 7.10.2, GHC == 8.0.1
+Tested-With: GHC == 7.10.3, GHC == 8.4.3, GHC == 8.6.1
License: BSD3
License-File: LICENSE
Copyright: 2017 Gabriel Gonzalez
@@ -35,7 +35,7 @@ Library
Build-Depends:
base >= 4.8.0.0 && < 5 ,
aeson >= 1.0.0.0 && < 1.5 ,
- dhall >= 1.19.0 && < 1.21,
+ dhall >= 1.19.0 && < 1.22,
optparse-applicative >= 0.14.0.0 && < 0.15,
text >= 0.11.1.0 && < 1.3 ,
unordered-containers < 0.3
@@ -63,6 +63,7 @@ Executable dhall-to-yaml
Main-Is: Main.hs
Build-Depends:
base ,
+ aeson ,
bytestring < 0.11,
dhall ,
dhall-json ,
diff --git a/dhall-to-json/Main.hs b/dhall-to-json/Main.hs
index d58130f..8670f01 100644
--- a/dhall-to-json/Main.hs
+++ b/dhall-to-json/Main.hs
@@ -1,4 +1,3 @@
-{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
@@ -7,6 +6,7 @@ module Main where
import Control.Applicative ((<|>))
import Control.Exception (SomeException)
import Control.Monad (when)
+import Data.Aeson (Value)
import Data.Monoid ((<>))
import Data.Version (showVersion)
import Dhall.JSON (Conversion)
@@ -29,19 +29,19 @@ import qualified System.IO
data Options = Options
{ explain :: Bool
, pretty :: Bool
- , omitNull :: Bool
+ , omission :: Value -> Value
, version :: Bool
, conversion :: Conversion
}
parseOptions :: Parser Options
-parseOptions = Options.Applicative.helper <*> do
- explain <- parseExplain
- pretty <- parsePretty
- omitNull <- parseOmitNull
- version <- parseVersion
- conversion <- Dhall.JSON.parseConversion
- return (Options {..})
+parseOptions =
+ Options
+ <$> parseExplain
+ <*> parsePretty
+ <*> Dhall.JSON.parseOmission
+ <*> parseVersion
+ <*> Dhall.JSON.parseConversion
where
parseExplain =
Options.Applicative.switch
@@ -69,12 +69,6 @@ parseOptions = Options.Applicative.helper <*> do
defaultBehavior =
pure False
- parseOmitNull =
- Options.Applicative.switch
- ( Options.Applicative.long "omitNull"
- <> Options.Applicative.help "Omit record fields that are null"
- )
-
parseVersion =
Options.Applicative.switch
( Options.Applicative.long "version"
@@ -84,7 +78,7 @@ parseOptions = Options.Applicative.helper <*> do
parserInfo :: ParserInfo Options
parserInfo =
Options.Applicative.info
- parseOptions
+ (Options.Applicative.helper <*> parseOptions)
( Options.Applicative.fullDesc
<> Options.Applicative.progDesc "Compile Dhall to JSON"
)
@@ -112,11 +106,9 @@ main = do
let explaining = if explain then Dhall.detailed else id
- let omittingNull = if omitNull then Dhall.JSON.omitNull else id
-
stdin <- Data.Text.IO.getContents
- json <- omittingNull <$> explaining (Dhall.JSON.codeToValue conversion "(stdin)" stdin)
+ json <- omission <$> explaining (Dhall.JSON.codeToValue conversion "(stdin)" stdin)
Data.ByteString.Char8.putStrLn $ Data.ByteString.Lazy.toStrict $ encode json
diff --git a/dhall-to-yaml/Main.hs b/dhall-to-yaml/Main.hs
index cec56be..fd516df 100644
--- a/dhall-to-yaml/Main.hs
+++ b/dhall-to-yaml/Main.hs
@@ -1,10 +1,10 @@
-{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Main where
import Control.Exception (SomeException)
+import Data.Aeson (Value)
import Data.Monoid ((<>))
import Dhall.JSON (Conversion)
import Options.Applicative (Parser, ParserInfo)
@@ -23,18 +23,18 @@ import qualified System.IO
data Options = Options
{ explain :: Bool
- , omitNull :: Bool
+ , omission :: Value -> Value
, documents :: Bool
, conversion :: Conversion
}
parseOptions :: Parser Options
-parseOptions = Options.Applicative.helper <*> do
- explain <- parseExplain
- omitNull <- parseOmitNull
- documents <- parseDocuments
- conversion <- Dhall.JSON.parseConversion
- return (Options {..})
+parseOptions =
+ Options
+ <$> parseExplain
+ <*> Dhall.JSON.parseOmission
+ <*> parseDocuments
+ <*> Dhall.JSON.parseConversion
where
parseExplain =
Options.Applicative.switch
@@ -42,12 +42,6 @@ parseOptions = Options.Applicative.helper <*> do
<> Options.Applicative.help "Explain error messages in detail"
)
- parseOmitNull =
- Options.Applicative.switch
- ( Options.Applicative.long "omitNull"
- <> Options.Applicative.help "Omit record fields that are null"
- )
-
parseDocuments =
Options.Applicative.switch
( Options.Applicative.long "documents"
@@ -57,7 +51,7 @@ parseOptions = Options.Applicative.helper <*> do
parserInfo :: ParserInfo Options
parserInfo =
Options.Applicative.info
- parseOptions
+ (Options.Applicative.helper <*> parseOptions)
( Options.Applicative.fullDesc
<> Options.Applicative.progDesc "Compile Dhall to YAML"
)
@@ -71,11 +65,9 @@ main = do
handle $ do
let explaining = if explain then Dhall.detailed else id
- let omittingNull = if omitNull then Dhall.JSON.omitNull else id
-
stdin <- Data.Text.IO.getContents
- json <- omittingNull <$> explaining (Dhall.JSON.codeToValue conversion "(stdin)" stdin)
+ json <- omission <$> explaining (Dhall.JSON.codeToValue conversion "(stdin)" stdin)
let yaml = case (documents, json) of
(True, Data.Yaml.Array elems)
diff --git a/src/Dhall/JSON.hs b/src/Dhall/JSON.hs
index 8400c17..10bb746 100644
--- a/src/Dhall/JSON.hs
+++ b/src/Dhall/JSON.hs
@@ -1,4 +1,3 @@
-{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
@@ -162,6 +161,8 @@ module Dhall.JSON (
-- * Dhall to JSON
dhallToJSON
, omitNull
+ , omitEmpty
+ , parseOmission
, Conversion(..)
, convertToHomogeneousMaps
, parseConversion
@@ -330,8 +331,9 @@ toOrderedList =
-- | Omit record fields that are @null@
omitNull :: Value -> Value
-omitNull (Object object) =
- Object (fmap omitNull (Data.HashMap.Strict.filter (/= Null) object))
+omitNull (Object object) = Object fields
+ where
+ fields =Data.HashMap.Strict.filter (/= Null) (fmap omitNull object)
omitNull (Array array) =
Array (fmap omitNull array)
omitNull (String string) =
@@ -343,6 +345,40 @@ omitNull (Bool bool) =
omitNull Null =
Null
+{-| Omit record fields that are @null@ or records whose transitive fields are
+ all null
+-}
+omitEmpty :: Value -> Value
+omitEmpty (Object object) =
+ if null fields then Null else Object fields
+ where
+ fields = Data.HashMap.Strict.filter (/= Null) (fmap omitEmpty object)
+omitEmpty (Array array) =
+ Array (fmap omitEmpty array)
+omitEmpty (String string) =
+ String string
+omitEmpty (Number number) =
+ Number number
+omitEmpty (Bool bool) =
+ Bool bool
+omitEmpty Null =
+ Null
+
+-- | Parser for command-line options related to omitting fields
+parseOmission :: Parser (Value -> Value)
+parseOmission =
+ Options.Applicative.flag'
+ omitNull
+ ( Options.Applicative.long "omitNull"
+ <> Options.Applicative.help "Omit record fields that are null"
+ )
+ <|> Options.Applicative.flag'
+ omitEmpty
+ ( Options.Applicative.long "omitEmpty"
+ <> Options.Applicative.help "Omit record fields that are null or empty records"
+ )
+ <|> pure id
+
{-| Specify whether or not to convert association lists of type
@List { mapKey: Text, mapValue : v }@ to records
-}
@@ -518,6 +554,9 @@ convertToHomogeneousMaps (Conversion {..}) e0 = loop (Dhall.Core.normalize e0)
a' = loop a
b' = loop b
+ Dhall.Core.TextShow ->
+ Dhall.Core.TextShow
+
Dhall.Core.List ->
Dhall.Core.List
@@ -665,11 +704,6 @@ convertToHomogeneousMaps (Conversion {..}) e0 = loop (Dhall.Core.normalize e0)
b' = loop b
c' = fmap loop c
- Dhall.Core.Constructors a ->
- Dhall.Core.Constructors a'
- where
- a' = loop a
-
Dhall.Core.Field a b ->
Dhall.Core.Field a' b
where
@@ -694,15 +728,13 @@ convertToHomogeneousMaps (Conversion {..}) e0 = loop (Dhall.Core.normalize e0)
Dhall.Core.Embed a ->
Dhall.Core.Embed a
+-- | Parser for command-line options related to homogeneous map support
parseConversion :: Parser Conversion
parseConversion =
conversion
<|> noConversion
where
- conversion = do
- mapKey <- parseKeyField
- mapValue <- parseValueField
- return (Conversion {..})
+ conversion = Conversion <$> parseKeyField <*> parseValueField
where
parseKeyField =
Options.Applicative.strOption