Skip to content

Commit

Permalink
Add a --num option and default to 1 record
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Mar 12, 2019
1 parent 8f3d767 commit f46541b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
26 changes: 21 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,34 @@

[![Build status](https://dev.azure.com/mfussenegger/fake-json/_apis/build/status/fake-json-CI?branchName=master)](https://dev.azure.com/mfussenegger/fake-json/_build/latest?definitionId=2)

`fake-json` is a CLI to generate random JSON records. If invoked it will
generate and print 1 JSON object per line until cancelled:
`fake-json` is a CLI to generate JSON records.


Static JSON:

```
↪ fake-json x=10 y=20 foo=bar
{"foo":"bar","x":10,"y":20}
```

Generated random data:

```
↪ fake-json id=uuid4() x="randomInt(0, 10)" | head -n 3
↪ fake-json --num 3 id="uuid4()" x="randomInt(0, 10)"
{"id":"2c6ce42f-5f7d-4e65-a1a1-8b39f6cfce19","x":6}
{"id":"d4c1af69-3cdd-417d-98e0-d3774f5fa1be","x":8}
{"id":"8fc064c9-57ba-4d78-b205-57899776e757","x":6}
```

It takes `fieldName=valueProvider()` pairs as argument. Each pair will result in
a field within the JSON object.
It takes `fieldName=value` or `fieldName=provider()` pairs as
argument. Each pair will result in a field within the JSON object.

To generate infinite records, use:

```
↪ fake-json --num Inf
```


### Available providers

Expand Down
6 changes: 4 additions & 2 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Main where

import qualified Cli
import Control.Monad (forever)
import Control.Monad (forever, replicateM)
import Control.Monad.IO.Class (liftIO)
import Data.Aeson (encode, object)
import qualified Data.ByteString.Lazy.Char8 as BL
Expand Down Expand Up @@ -41,13 +41,15 @@ main = do
if null errored
then
let
printRecords = forever $
printRecords = loop (Cli.num parsedArgs) $
mapM runProvider providers >>= liftIO . BL.putStrLn . encode . object
in
runFakeT (Cli.seed parsedArgs) printRecords >> pure ()
else
mapM_ print errored
where
loop Cli.Infinite = forever
loop (Cli.Const n) = replicateM n
unpackRight (x, Right y) = (x, y)
unpackRight _ = error "Tuple must only contain Right eithers"
runProvider (column, provider) = do
Expand Down
29 changes: 26 additions & 3 deletions src/Cli.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@

module Cli
( Args(..)
, Amount(..)
, parseArgs
) where


import Options.Applicative
import Options.Applicative hiding (Const)
import Text.Read (readMaybe)


data Args = Args
{ seed :: !(Maybe Int)
, num :: !Amount }

data Amount = Const Int
| Infinite
deriving (Show)


parseNum :: ReadM Amount
parseNum = eitherReader parseNum'
where
parseNum' "Inf" = Right Infinite
parseNum' s = case readMaybe s of
(Just n) -> Right (Const n)
Nothing -> Left "Expected a number or `Inf` for infinite records"

newtype Args = Args
{ seed :: Maybe Int }

args :: Parser Args
args = Args
Expand All @@ -17,6 +35,11 @@ args = Args
( long "seed"
<> help "A seed for the random data generator"
<> metavar "INT" ))
<*> option parseNum
( long "num"
<> value (Const 1)
<> help "Number of records to generate. Use `Inf` for infinite records" )


parseArgs :: [String] -> IO Args
parseArgs cliArgs = handleParseResult $ execParserPure defaultPrefs opts cliArgs
Expand Down

0 comments on commit f46541b

Please sign in to comment.