Skip to content
This repository was archived by the owner on Jul 25, 2022. It is now read-only.

Commit 6043b43

Browse files
committed
Add --array
1 parent 7824e34 commit 6043b43

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# jl [![Build Status](https://travis-ci.org/chrisdone/jl.svg)](https://travis-ci.org/chrisdone/jl)
1+
# jl [![Build Status](https://travis-ci.org/chrisdone/jl.svg)](https://travis-ci.org/chrisdone/jl)
22
jl ("JSON lambda") is a tiny functional language for querying and
33
manipulating JSON.
44

@@ -81,6 +81,36 @@ programming:
8181
jl 'id'
8282
```
8383
84+
A sequence of JSON strings will be read in and processed individually:
85+
86+
E.g.
87+
88+
``` haskell
89+
$ cat x.json | jl id
90+
{"a":1}
91+
{"a":2}
92+
{"a":3}
93+
{"a":4}
94+
```
95+
96+
If you want to read the input in as an array, use `--array`:
97+
98+
``` haskell
99+
$ cat x.json | jl --array 'map _.a'
100+
[1,2,3,4]
101+
```
102+
103+
After processing, sometimes you want to print each element of the
104+
array out line by line, for that use `--lines`:
105+
106+
``` haskell
107+
$ cat x.json | jl --array --lines 'map _.a'
108+
1
109+
2
110+
3
111+
4
112+
```
113+
84114
Taking the first element of something, using syntax that looks like
85115
regular array access. The `_` is a short-hand so that you don't need a
86116
lambda:

app/Main.hs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,22 @@ main :: IO ()
3232
main = do
3333
do hSetBuffering stdout LineBuffering
3434
hSetBuffering stdin LineBuffering
35-
((inp, file, aslines, browse, markdown, pretty), ()) <-
35+
((inp, file, inarray, aslines, browse, markdown, pretty), ()) <-
3636
simpleOptions
3737
"0.0.0"
3838
"jl - JSON Lambda calculus"
3939
"Command-line language for querying and outputting JSON."
40-
((,,,,,) <$>
40+
((,,,,,,) <$>
4141
strArgument
4242
(metavar "CODE" <>
4343
help "JL code; supports completion of function names" <>
4444
completeWith (map (\(Variable v) -> T.unpack v) (M.keys context))) <*>
4545
optional (strArgument (metavar "FILE")) <*>
46+
flag
47+
False
48+
True
49+
(short 'a' <> long "array" <>
50+
help "Read each line of input as a single array") <*>
4651
flag
4752
False
4853
True
@@ -101,14 +106,21 @@ main = do
101106
case Aeson.decode bytes of
102107
Nothing -> hPutStr stderr "invalid input JSON"
103108
Just j -> handleJson pretty expr0 aslines j
104-
Nothing -> process pretty expr0 aslines
109+
Nothing -> process pretty expr0 inarray aslines
105110
where
106-
process pretty expr0 aslines =
111+
process pretty expr0 inarray aslines =
107112
CB.sourceHandle stdin $= CB.lines $= conduitParserEither Aeson.value $=
108-
CL.mapM_
109-
(either
110-
(hPutStrLn stderr . errorMessage)
111-
(handleJson pretty expr0 aslines . snd)) $$
113+
(if inarray
114+
then do
115+
es <-
116+
CL.mapM (either (error . errorMessage) (return . snd)) $=
117+
CL.consume
118+
liftIO
119+
(handleJson pretty expr0 aslines (Aeson.Array (V.fromList es)))
120+
else CL.mapM_
121+
(either
122+
(hPutStrLn stderr . errorMessage)
123+
(handleJson pretty expr0 aslines . snd))) $$
112124
CL.sinkNull
113125

114126
-- | Handle a JSON input, printing out one to many JSON values.

0 commit comments

Comments
 (0)