Skip to content

Commit 714c409

Browse files
authored
Merge pull request #8 from purescript-contrib/0.9-updates
Updates for PureScript 0.9.1
2 parents 4082d22 + 38d41b6 commit 714c409

File tree

8 files changed

+127
-170
lines changed

8 files changed

+127
-170
lines changed

.travis.yml

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
language: node_js
2-
sudo: false
3-
node_js:
4-
- 5
2+
dist: trusty
3+
sudo: required
4+
node_js: 6
55
install:
66
- npm install -g bower
77
- npm install
88
script:
9-
- npm test
9+
- bower install --production
10+
- npm run -s build
11+
- bower install
12+
- npm -s test
1013
after_success:
1114
- >-
1215
test $TRAVIS_TAG &&
13-
node_modules/.bin/psc-publish > .pursuit.json &&
14-
curl -X POST http://pursuit.purescript.org/packages \
15-
-d @.pursuit.json \
16-
-H 'Accept: application/json' \
17-
-H "Authorization: token ${GITHUB_TOKEN}"
16+
echo $GITHUB_TOKEN | pulp login &&
17+
echo y | pulp publish --no-push

bower.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
},
2222
"license": "MIT",
2323
"dependencies": {
24-
"purescript-argonaut-codecs": "^0.6.0",
25-
"purescript-profunctor-lenses": "^0.3.3"
24+
"purescript-argonaut-codecs": "^1.0.0",
25+
"purescript-profunctor-lenses": "^1.0.0"
2626
},
2727
"devDependencies": {
28-
"purescript-strongcheck": "^0.14.3"
28+
"purescript-strongcheck": "^1.1.1"
2929
}
3030
}

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"private": true,
33
"scripts": {
4-
"postinstall": "pulp dep install",
54
"clean": "rimraf output && rimraf .pulp-cache",
6-
"build": "pulp build",
5+
"build": "pulp build --censor-lib --strict",
76
"test": "pulp test"
87
},
98
"devDependencies": {
10-
"pulp": "^7.0.0",
11-
"purescript": "^0.7.6",
9+
"pulp": "^9.0.1",
10+
"purescript": "^0.9.1",
11+
"purescript-psa": "^0.3.8",
1212
"rimraf": "^2.4.4"
1313
}
1414
}

src/Data/Argonaut/JCursor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// module Data.Argonaut.JCursor
1+
"use strict";
22

33
exports.exactNull = null;

src/Data/Argonaut/JCursor.purs

+96-143
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,72 @@ module Data.Argonaut.JCursor where
22

33
import Prelude
44

5-
import Data.Argonaut.Core
6-
import Data.Argonaut.Decode
7-
import Data.Argonaut.Encode
5+
import Data.Argonaut.Core as J
6+
import Data.Argonaut.Decode (class DecodeJson, decodeJson)
7+
import Data.Argonaut.Encode (class EncodeJson, encodeJson)
8+
import Data.Array as A
89
import Data.Either (Either(..))
10+
import Data.Unfoldable (replicate)
911
import Data.Foldable (foldl)
10-
import Data.List (List(), zipWith, range, head, singleton, toList)
12+
import Data.Int as I
13+
import Data.List (List(), zipWith, range, head, singleton, fromFoldable)
1114
import Data.Maybe (Maybe(..), fromMaybe, maybe)
12-
import Data.Monoid (Monoid)
15+
import Data.Monoid (class Monoid)
16+
import Data.StrMap as M
1317
import Data.Tuple (Tuple(..), fst, snd)
1418

15-
import qualified Data.Array as A
16-
import qualified Data.Int as I
17-
import qualified Data.Maybe.Unsafe as MU
18-
import qualified Data.StrMap as M
19-
2019
data JCursor
2120
= JCursorTop
2221
| JField String JCursor
2322
| JIndex Int JCursor
2423

25-
newtype JsonPrim
26-
= JsonPrim (forall a.
27-
(JNull -> a) ->
28-
(JBoolean -> a) ->
29-
(JNumber -> a) ->
30-
(JString -> a) ->
31-
a)
24+
derive instance eqJCursor :: Eq JCursor
25+
derive instance ordJCursor :: Ord JCursor
26+
27+
instance showJCursor :: Show JCursor where
28+
show JCursorTop = ""
29+
show (JField i c) = "." <> i <> show c
30+
show (JIndex i c) = "[" <> show i <> "]" <> show c
31+
32+
instance semigroupJCursor :: Semigroup JCursor where
33+
append a JCursorTop = a
34+
append JCursorTop b = b
35+
append (JField i a) b = JField i (a <> b)
36+
append (JIndex i a) b = JIndex i (a <> b)
37+
38+
instance monoidJCursor :: Monoid JCursor where
39+
mempty = JCursorTop
40+
41+
instance encodeJsonJCursor :: EncodeJson JCursor where
42+
encodeJson = encodeJson <<< loop where
43+
loop JCursorTop = []
44+
loop (JField i c) = [encodeJson i] <> loop c
45+
loop (JIndex i c) = [encodeJson i] <> loop c
46+
47+
newtype JsonPrim = JsonPrim (forall a. (J.JNull -> a) -> (J.JBoolean -> a) -> (J.JNumber -> a) -> (J.JString -> a) -> a)
3248

33-
runJsonPrim :: JsonPrim -> (forall a. (JNull -> a) -> (JBoolean -> a) -> (JNumber -> a) -> (JString -> a) -> a)
49+
runJsonPrim :: JsonPrim -> (forall a. (J.JNull -> a) -> (J.JBoolean -> a) -> (J.JNumber -> a) -> (J.JString -> a) -> a)
3450
runJsonPrim (JsonPrim p) = p
3551

36-
foreign import exactNull :: JNull
52+
instance showJsonPrim :: Show JsonPrim where
53+
show p = runJsonPrim p show show show show
54+
55+
foreign import exactNull :: J.JNull
3756

3857
primNull :: JsonPrim
3958
primNull = JsonPrim (\f _ _ _ -> f exactNull)
4059

41-
primBool :: JBoolean -> JsonPrim
60+
primBool :: J.JBoolean -> JsonPrim
4261
primBool v = JsonPrim (\_ f _ _ -> f v)
4362

44-
primNum :: JNumber -> JsonPrim
63+
primNum :: J.JNumber -> JsonPrim
4564
primNum v = JsonPrim (\_ _ f _ -> f v)
4665

47-
primStr :: JString -> JsonPrim
66+
primStr :: J.JString -> JsonPrim
4867
primStr v = JsonPrim (\_ _ _ f -> f v)
4968

50-
primToJson :: JsonPrim -> Json
51-
primToJson p = runJsonPrim p fromNull fromBoolean fromNumber fromString
69+
primToJson :: JsonPrim -> J.Json
70+
primToJson p = runJsonPrim p J.fromNull J.fromBoolean J.fromNumber J.fromString
5271

5372
insideOut :: JCursor -> JCursor
5473
insideOut JCursorTop = JCursorTop
@@ -67,157 +86,91 @@ downIndex i = downIndex' where
6786
downIndex' (JField i' c) = JField i' (downIndex' c)
6887
downIndex' (JIndex i' c) = JIndex i' (downIndex' c)
6988

70-
cursorGet :: JCursor -> Json -> Maybe Json
89+
cursorGet :: JCursor -> J.Json -> Maybe J.Json
7190
cursorGet JCursorTop = Just
72-
cursorGet (JField i c) = foldJsonObject Nothing g where
73-
g m = M.lookup i m >>= cursorGet c
74-
cursorGet (JIndex i c) = foldJsonArray Nothing g where
75-
g a = a A.!! i >>= cursorGet c
91+
cursorGet (JField i c) = J.foldJsonObject Nothing (cursorGet c <=< M.lookup i)
92+
cursorGet (JIndex i c) = J.foldJsonArray Nothing (cursorGet c <=< (_ A.!! i))
7693

77-
inferEmpty :: JCursor -> Json
78-
inferEmpty JCursorTop = jsonNull
79-
inferEmpty (JField _ _) = jsonEmptyObject
80-
inferEmpty (JIndex _ _) = jsonEmptyArray
94+
inferEmpty :: JCursor -> J.Json
95+
inferEmpty JCursorTop = J.jsonNull
96+
inferEmpty (JField _ _) = J.jsonEmptyObject
97+
inferEmpty (JIndex _ _) = J.jsonEmptyArray
8198

82-
cursorSet :: JCursor -> Json -> Json -> Maybe Json
99+
cursorSet :: JCursor -> J.Json -> J.Json -> Maybe J.Json
83100
cursorSet JCursorTop v = pure <<< const v
84-
cursorSet (JField i c) v = foldJsonObject defaultObj mergeObjs
101+
cursorSet (JField i c) v = J.foldJsonObject defaultObj mergeObjs
85102
where
86-
defaultObj :: Maybe Json
87-
defaultObj = fromObject <<< M.singleton i <$> cursorSet c v (inferEmpty c)
88-
89-
mergeObjs :: JObject -> Maybe Json
90-
mergeObjs m =
91-
fromObject <<< flip (M.insert i) m <$>
92-
(cursorSet c v $ fromMaybe (inferEmpty c) (M.lookup i m))
93-
cursorSet (JIndex i c) v = foldJsonArray defaultArr mergeArrs
103+
defaultObj :: Maybe J.Json
104+
defaultObj = J.fromObject <<< M.singleton i <$> cursorSet c v (inferEmpty c)
105+
mergeObjs :: J.JObject -> Maybe J.Json
106+
mergeObjs m
107+
= J.fromObject
108+
<<< flip (M.insert i) m
109+
<$> cursorSet c v (fromMaybe (inferEmpty c) (M.lookup i m))
110+
cursorSet (JIndex i c) v = J.foldJsonArray defaultArr mergeArrs
94111
where
95-
defaultArr :: Maybe Json
96-
defaultArr = fromArray <<< MU.fromJust <<<
97-
flip (A.updateAt i) (A.replicate (i + 1) jsonNull) <$>
98-
cursorSet c v (inferEmpty c)
99-
100-
mergeArrs :: JArray -> Maybe Json
101-
mergeArrs a = (cursorSet c v $ fromMaybe (inferEmpty c) (a A.!! i)) >>= setArr a i
102-
103-
setArr :: JArray -> Int -> Json -> Maybe Json
112+
defaultArr :: Maybe J.Json
113+
defaultArr
114+
= J.fromArray
115+
<$> (flip (A.updateAt i) (replicate (i + 1) J.jsonNull) =<< cursorSet c v (inferEmpty c))
116+
mergeArrs :: J.JArray -> Maybe J.Json
117+
mergeArrs a =
118+
setArr a i =<< cursorSet c v (fromMaybe (inferEmpty c) (a A.!! i))
119+
setArr :: J.JArray -> Int -> J.Json -> Maybe J.Json
104120
setArr xs i v =
105121
let len = A.length xs
106122
in if i < 0
107123
then Nothing
108124
else if i >= len
109-
then setArr (xs <> (A.replicate (i - len + 1) jsonNull)) i v
110-
else Just <<< fromArray <<< MU.fromJust $ A.updateAt i v xs
111-
125+
then setArr (xs <> (replicate (i - len + 1) J.jsonNull)) i v
126+
else J.fromArray <$> A.updateAt i v xs
112127

113-
toPrims :: Json -> List (Tuple JCursor JsonPrim)
114-
toPrims = foldJson nullFn boolFn numFn strFn arrFn objFn
128+
toPrims :: J.Json -> List (Tuple JCursor JsonPrim)
129+
toPrims = J.foldJson nullFn boolFn numFn strFn arrFn objFn
115130
where
116131
mkTop :: JsonPrim -> List (Tuple JCursor JsonPrim)
117132
mkTop p = singleton $ Tuple JCursorTop p
118-
119-
nullFn :: JNull -> List (Tuple JCursor JsonPrim)
133+
nullFn :: J.JNull -> List (Tuple JCursor JsonPrim)
120134
nullFn _ = mkTop primNull
121-
122-
boolFn :: JBoolean -> List (Tuple JCursor JsonPrim)
135+
boolFn :: J.JBoolean -> List (Tuple JCursor JsonPrim)
123136
boolFn b = mkTop $ primBool b
124-
125-
numFn :: JNumber -> List (Tuple JCursor JsonPrim)
137+
numFn :: J.JNumber -> List (Tuple JCursor JsonPrim)
126138
numFn n = mkTop $ primNum n
127-
128-
strFn :: JString -> List (Tuple JCursor JsonPrim)
139+
strFn :: J.JString -> List (Tuple JCursor JsonPrim)
129140
strFn s = mkTop $ primStr s
130-
131-
arrFn :: JArray -> List (Tuple JCursor JsonPrim)
141+
arrFn :: J.JArray -> List (Tuple JCursor JsonPrim)
132142
arrFn arr =
133-
let zipped :: List (Tuple Int Json)
134-
zipped = zipWith Tuple (range 0 (A.length arr - 1)) (toList arr)
135-
143+
let zipped :: List (Tuple Int J.Json)
144+
zipped = zipWith Tuple (range 0 (A.length arr - 1)) (fromFoldable arr)
136145
in zipped >>= arrFn'
137-
138-
arrFn' :: Tuple Int Json -> List (Tuple JCursor JsonPrim)
139-
arrFn' (Tuple i j) = toList ((\t -> Tuple (JIndex i (fst t)) (snd t))
140-
<$> toPrims j)
141-
142-
143-
objFn :: JObject -> List (Tuple JCursor JsonPrim)
146+
arrFn' :: Tuple Int J.Json -> List (Tuple JCursor JsonPrim)
147+
arrFn' (Tuple i j) =
148+
fromFoldable ((\t -> Tuple (JIndex i (fst t)) (snd t)) <$> toPrims j)
149+
objFn :: J.JObject -> List (Tuple JCursor JsonPrim)
144150
objFn obj =
145-
let f :: Tuple String Json -> List (Tuple JCursor JsonPrim)
146-
f (Tuple i j) = ((\t -> Tuple (JField i (fst t)) (snd t))
147-
<$> toPrims j)
151+
let f :: Tuple String J.Json -> List (Tuple JCursor JsonPrim)
152+
f (Tuple i j) = (\t -> Tuple (JField i (fst t)) (snd t)) <$> toPrims j
148153
in M.toList obj >>= f
149154

150-
151-
fromPrims :: List (Tuple JCursor JsonPrim) -> Maybe Json
155+
fromPrims :: List (Tuple JCursor JsonPrim) -> Maybe J.Json
152156
fromPrims lst = foldl f (inferEmpty <<< fst <$> head lst) lst
153157
where
154-
f :: Maybe Json -> Tuple JCursor JsonPrim -> Maybe Json
158+
f :: Maybe J.Json -> Tuple JCursor JsonPrim -> Maybe J.Json
155159
f j (Tuple c p) = j >>= cursorSet c (primToJson p)
156160

157-
instance showJCursor :: Show JCursor where
158-
show JCursorTop = ""
159-
show (JField i c) = "." <> i <> show c
160-
show (JIndex i c) = "[" <> show i <> "]" <> show c
161-
162-
instance showJsonPrim :: Show JsonPrim where
163-
show p = runJsonPrim p show show show show
164-
165-
instance eqJCursor :: Eq JCursor where
166-
eq JCursorTop JCursorTop = true
167-
eq (JField i1 c1) (JField i2 c2) = i1 == i2 && c1 == c2
168-
eq (JIndex i1 c1) (JIndex i2 c2) = i1 == i2 && c1 == c2
169-
eq _ _ = false
170-
171-
instance ordJCursor :: Ord JCursor where
172-
compare JCursorTop JCursorTop = EQ
173-
compare JCursorTop _ = LT
174-
compare _ JCursorTop = GT
175-
compare (JField _ _) (JIndex _ _) = LT
176-
compare (JIndex _ _) (JField _ _) = GT
177-
compare (JField i1 c1) (JField i2 c2) = case compare i1 i2 of
178-
EQ -> compare c1 c2
179-
x -> x
180-
compare (JIndex i1 c1) (JIndex i2 c2) = case compare i1 i2 of
181-
EQ -> compare c1 c2
182-
x -> x
183-
184-
instance semigroupJCursor :: Semigroup JCursor where
185-
append a JCursorTop = a
186-
append JCursorTop b = b
187-
append (JField i a) b = JField i (a <> b)
188-
append (JIndex i a) b = JIndex i (a <> b)
189-
190-
instance monoidJCursor :: Monoid JCursor where
191-
mempty = JCursorTop
192-
193-
instance encodeJsonJCursor :: EncodeJson JCursor where
194-
encodeJson = encodeJson <<< loop where
195-
loop JCursorTop = []
196-
loop (JField i c) = [encodeJson i] <> loop c
197-
loop (JIndex i c) = [encodeJson i] <> loop c
198-
199-
fail :: forall a b. (Show a) => a -> Either String b
200-
fail x = Left $ "Expected String or Number but found: " ++ show x
161+
fail :: forall a b. Show a => a -> Either String b
162+
fail x = Left $ "Expected String or Number but found: " <> show x
201163

202164
instance decodeJsonJCursor :: DecodeJson JCursor where
203165
decodeJson j = decodeJson j >>= loop
204166
where
205-
loop :: Array Json -> Either String JCursor
167+
loop :: Array J.Json -> Either String JCursor
206168
loop arr =
207-
maybe (Right JCursorTop) goLoop do
208-
x <- A.head arr
209-
xs <- A.tail arr
210-
pure $ Tuple x xs
211-
212-
goLoop :: Tuple Json (Array Json) -> Either String JCursor
169+
maybe (Right JCursorTop) goLoop $ Tuple <$> A.head arr <*> A.tail arr
170+
goLoop :: Tuple J.Json (Array J.Json) -> Either String JCursor
213171
goLoop (Tuple x xs) = do
214172
c <- loop xs
215-
foldJson fail fail (goNum c) (Right <<< (flip JField c)) fail fail x
216-
217-
goNum :: JCursor -> JNumber -> Either String JCursor
218-
goNum c num =
219-
maybe (Left "Not an Int") (Right <<< (flip JIndex c)) $ I.fromNumber num
220-
221-
222-
223-
173+
J.foldJson fail fail (goNum c) (Right <<< flip JField c) fail fail x
174+
goNum :: JCursor -> J.JNumber -> Either String JCursor
175+
goNum c =
176+
maybe (Left "Not an Int") (Right <<< flip JIndex c) <<< I.fromNumber

src/Data/Argonaut/Prisms.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Data.Argonaut.Prisms where
22

33
import Data.Argonaut.Core
4-
import Data.Lens (PrismP(), prism')
4+
import Data.Lens (PrismP, prism')
55

66
_Null :: PrismP Json JNull
77
_Null = prism' fromNull toNull

src/Data/Argonaut/Traversals.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module Data.Argonaut.Traversals where
22

3-
import Data.Argonaut.Core
4-
import Data.Lens (TraversalP(), filtered)
53
import Prelude ((<<<), id)
4+
import Data.Argonaut.Core
5+
import Data.Lens (TraversalP, filtered)
66

77
_JsonNull :: TraversalP Json Json
88
_JsonNull = id <<< filtered isNull

0 commit comments

Comments
 (0)