Skip to content

Commit 1a27cad

Browse files
Gabriella439mergify[bot]
authored andcommitted
Add --file option to dhall-json executables (#1107)
* Add `--file` option to `dhall-json` executables Fixes #1096 * s/JSON/expression/ ... as caught by @sjakobi Co-Authored-By: Simon Jakobi <[email protected]> * s/expression/YAML expression/ ... as caught by @sjakobi Co-Authored-By: Simon Jakobi <[email protected]> * Change the program descriptions for `{json,yaml}-to-dhall` ... as suggested by @sjakobi
1 parent 40ec837 commit 1a27cad

File tree

5 files changed

+136
-72
lines changed

5 files changed

+136
-72
lines changed

dhall-json/dhall-to-json/Main.hs

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Main where
55

6-
import Control.Applicative ((<|>))
6+
import Control.Applicative ((<|>), optional)
77
import Control.Exception (SomeException)
88
import Control.Monad (when)
99
import Data.Aeson (Value)
@@ -17,12 +17,13 @@ import qualified Data.Aeson
1717
import qualified Data.Aeson.Encode.Pretty
1818
import qualified Data.ByteString.Char8
1919
import qualified Data.ByteString.Lazy
20-
import qualified Data.Text.IO
20+
import qualified Data.Text as Text
21+
import qualified Data.Text.IO as Text.IO
2122
import qualified Dhall
2223
import qualified Dhall.JSON
2324
import qualified GHC.IO.Encoding
24-
import qualified Options.Applicative
25-
import qualified Paths_dhall_json as Meta
25+
import qualified Options.Applicative as Options
26+
import qualified Paths_dhall_json as Meta
2627
import qualified System.Exit
2728
import qualified System.IO
2829

@@ -33,6 +34,7 @@ data Options = Options
3334
, version :: Bool
3435
, conversion :: Conversion
3536
, approximateSpecialDoubles :: Bool
37+
, file :: Maybe FilePath
3638
}
3739

3840
parseOptions :: Parser Options
@@ -44,58 +46,66 @@ parseOptions =
4446
<*> parseVersion
4547
<*> Dhall.JSON.parseConversion
4648
<*> parseApproximateSpecialDoubles
49+
<*> optional parseFile
4750
where
4851
parseExplain =
49-
Options.Applicative.switch
50-
( Options.Applicative.long "explain"
51-
<> Options.Applicative.help "Explain error messages in detail"
52+
Options.switch
53+
( Options.long "explain"
54+
<> Options.help "Explain error messages in detail"
5255
)
5356

5457
parsePretty =
5558
prettyFlag <|> compactFlag <|> defaultBehavior
5659
where
5760
prettyFlag =
58-
Options.Applicative.flag'
61+
Options.flag'
5962
True
60-
( Options.Applicative.long "pretty"
61-
<> Options.Applicative.help "Pretty print generated JSON"
63+
( Options.long "pretty"
64+
<> Options.help "Pretty print generated JSON"
6265
)
6366

6467
compactFlag =
65-
Options.Applicative.flag'
68+
Options.flag'
6669
False
67-
( Options.Applicative.long "compact"
68-
<> Options.Applicative.help "Render JSON on one line"
70+
( Options.long "compact"
71+
<> Options.help "Render JSON on one line"
6972
)
7073

7174
defaultBehavior =
7275
pure False
7376

7477
parseVersion =
75-
Options.Applicative.switch
76-
( Options.Applicative.long "version"
77-
<> Options.Applicative.help "Display version"
78+
Options.switch
79+
( Options.long "version"
80+
<> Options.help "Display version"
7881
)
7982

8083
parseApproximateSpecialDoubles =
81-
Options.Applicative.switch
82-
( Options.Applicative.long "approximate-special-doubles"
83-
<> Options.Applicative.help "Use approximate representation for NaN/±Infinity"
84+
Options.switch
85+
( Options.long "approximate-special-doubles"
86+
<> Options.help "Use approximate representation for NaN/±Infinity"
87+
)
88+
89+
parseFile =
90+
Options.strOption
91+
( Options.long "file"
92+
<> Options.help "Read expression from a file instead of standard input"
93+
<> Options.metavar "FILE"
8494
)
8595

8696
parserInfo :: ParserInfo Options
8797
parserInfo =
88-
Options.Applicative.info
89-
(Options.Applicative.helper <*> parseOptions)
90-
( Options.Applicative.fullDesc
91-
<> Options.Applicative.progDesc "Compile Dhall to JSON"
98+
Options.info
99+
(Options.helper <*> parseOptions)
100+
( Options.fullDesc
101+
<> Options.progDesc "Compile Dhall to JSON"
92102
)
93103

94104
main :: IO ()
95105
main = do
96106
GHC.IO.Encoding.setLocaleEncoding GHC.IO.Encoding.utf8
97107

98-
Options {..} <- Options.Applicative.execParser parserInfo
108+
Options {..} <- Options.execParser parserInfo
99109

100110
when version $ do
101111
putStrLn (showVersion Meta.version)
@@ -119,9 +129,15 @@ main = do
119129
then ApproximateWithinJSON
120130
else ForbidWithinJSON
121131

122-
stdin <- Data.Text.IO.getContents
132+
text <- case file of
133+
Nothing -> Text.IO.getContents
134+
Just path -> Text.IO.readFile path
135+
136+
let path = case file of
137+
Nothing -> "(stdin)"
138+
Just p -> Text.pack p
123139

124-
json <- omission <$> explaining (Dhall.JSON.codeToValue conversion specialDoubleMode "(stdin)" stdin)
140+
json <- omission <$> explaining (Dhall.JSON.codeToValue conversion specialDoubleMode path text)
125141

126142
Data.ByteString.Char8.putStrLn $ Data.ByteString.Lazy.toStrict $ encode json
127143

dhall-json/dhall-to-yaml/Main.hs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE RecordWildCards #-}
33
module Main where
44

5+
import Control.Applicative (optional)
56
import Control.Exception (SomeException)
67
import Data.Monoid ((<>))
78
import Dhall.JSON (parseOmission, parseConversion)
@@ -10,9 +11,10 @@ import Options.Applicative (Parser, ParserInfo)
1011

1112
import qualified Control.Exception
1213
import qualified Data.ByteString
13-
import qualified Data.Text.IO
14+
import qualified Data.Text as Text
15+
import qualified Data.Text.IO as Text.IO
1416
import qualified GHC.IO.Encoding
15-
import qualified Options.Applicative
17+
import qualified Options.Applicative as Options
1618
import qualified System.Exit
1719
import qualified System.IO
1820

@@ -24,32 +26,45 @@ parseOptions =
2426
<*> parseDocuments
2527
<*> parseQuoted
2628
<*> Dhall.JSON.parseConversion
29+
<*> optional parseFile
2730
where
2831
parseExplain =
29-
Options.Applicative.switch
30-
( Options.Applicative.long "explain"
31-
<> Options.Applicative.help "Explain error messages in detail"
32+
Options.switch
33+
( Options.long "explain"
34+
<> Options.help "Explain error messages in detail"
35+
)
36+
37+
parseFile =
38+
Options.strOption
39+
( Options.long "file"
40+
<> Options.help "Read expression from a file instead of standard input"
41+
<> Options.metavar "FILE"
3242
)
3343

3444
parserInfo :: ParserInfo Options
3545
parserInfo =
36-
Options.Applicative.info
37-
(Options.Applicative.helper <*> parseOptions)
38-
( Options.Applicative.fullDesc
39-
<> Options.Applicative.progDesc "Compile Dhall to YAML"
46+
Options.info
47+
(Options.helper <*> parseOptions)
48+
( Options.fullDesc
49+
<> Options.progDesc "Compile Dhall to YAML"
4050
)
4151

4252
main :: IO ()
4353
main = do
4454
GHC.IO.Encoding.setLocaleEncoding GHC.IO.Encoding.utf8
4555

46-
options <- Options.Applicative.execParser parserInfo
56+
options@Options {..} <- Options.execParser parserInfo
4757

4858
handle $ do
59+
contents <- case file of
60+
Nothing -> Text.IO.getContents
61+
Just path -> Text.IO.readFile path
4962

50-
stdin <- Data.Text.IO.getContents
63+
let path = case file of
64+
Nothing -> "(stdin)"
65+
Just p -> Text.pack p
5166

52-
Data.ByteString.putStr =<< dhallToYaml options "(stdin)" stdin
67+
Data.ByteString.putStr =<< dhallToYaml options path contents
5368

5469
handle :: IO a -> IO a
5570
handle = Control.Exception.handle handler

dhall-json/json-to-dhall/Main.hs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
module Main where
1010

11+
import Control.Applicative (optional)
1112
import qualified Control.Exception
1213
import Control.Exception (SomeException, throwIO)
1314
import Control.Monad (when)
@@ -18,7 +19,7 @@ import Data.Text (Text)
1819
import qualified Data.Text.IO as Text
1920
import Data.Version (showVersion)
2021
import qualified GHC.IO.Encoding
21-
import qualified Options.Applicative as O
22+
import qualified Options.Applicative as Options
2223
import Options.Applicative (Parser, ParserInfo)
2324
import qualified System.Exit
2425
import qualified System.IO
@@ -34,34 +35,46 @@ import qualified Paths_dhall_json as Meta
3435

3536
-- | Command info and description
3637
parserInfo :: ParserInfo Options
37-
parserInfo = O.info
38-
( O.helper <*> parseOptions)
39-
( O.fullDesc
40-
<> O.progDesc "Populate Dhall value given its Dhall type (schema) from a JSON expression"
38+
parserInfo = Options.info
39+
( Options.helper <*> parseOptions)
40+
( Options.fullDesc
41+
<> Options.progDesc "Convert a JSON expression to a Dhall expression, given the expected Dhall type"
4142
)
4243

4344
-- | All the command arguments and options
4445
data Options = Options
4546
{ version :: Bool
4647
, schema :: Text
4748
, conversion :: Conversion
49+
, file :: Maybe FilePath
4850
} deriving Show
4951

5052
-- | Parser for all the command arguments and options
5153
parseOptions :: Parser Options
5254
parseOptions = Options <$> parseVersion
5355
<*> parseSchema
5456
<*> parseConversion
57+
<*> optional parseFile
5558
where
56-
parseSchema = O.strArgument
57-
( O.metavar "SCHEMA"
58-
<> O.help "Dhall type expression (schema)"
59-
)
60-
parseVersion = O.switch
61-
( O.long "version"
62-
<> O.short 'V'
63-
<> O.help "Display version"
64-
)
59+
parseSchema =
60+
Options.strArgument
61+
( Options.metavar "SCHEMA"
62+
<> Options.help "Dhall type expression (schema)"
63+
)
64+
65+
parseVersion =
66+
Options.switch
67+
( Options.long "version"
68+
<> Options.short 'V'
69+
<> Options.help "Display version"
70+
)
71+
72+
parseFile =
73+
Options.strOption
74+
( Options.long "file"
75+
<> Options.help "Read JSON from a file instead of standard input"
76+
<> Options.metavar "FILE"
77+
)
6578

6679
-- ----------
6780
-- Main
@@ -71,15 +84,18 @@ main :: IO ()
7184
main = do
7285
GHC.IO.Encoding.setLocaleEncoding GHC.IO.Encoding.utf8
7386

74-
Options {..} <- O.execParser parserInfo
87+
Options {..} <- Options.execParser parserInfo
7588

7689
when version $ do
7790
putStrLn (showVersion Meta.version)
7891
System.Exit.exitSuccess
7992

8093
handle $ do
81-
stdin <- BSL8.getContents
82-
value :: A.Value <- case A.eitherDecode stdin of
94+
bytes <- case file of
95+
Nothing -> BSL8.getContents
96+
Just path -> BSL8.readFile path
97+
98+
value :: A.Value <- case A.eitherDecode bytes of
8399
Left err -> throwIO (userError err)
84100
Right v -> pure v
85101

dhall-json/src/Dhall/Yaml.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ data Options = Options
3737
, documents :: Bool
3838
, quoted :: Bool
3939
, conversion :: Conversion
40+
, file :: Maybe FilePath
4041
}
4142

4243
defaultOptions :: Options
@@ -46,6 +47,7 @@ defaultOptions =
4647
, documents = False
4748
, quoted = False
4849
, conversion = NoConversion
50+
, file = Nothing
4951
}
5052

5153
parseDocuments :: Parser Bool

0 commit comments

Comments
 (0)