Skip to content

Commit ee3b269

Browse files
committed
Update for PureScript 0.11
1 parent affeeab commit ee3b269

File tree

8 files changed

+19
-180
lines changed

8 files changed

+19
-180
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: node_js
22
dist: trusty
33
sudo: required
4-
node_js: 6
4+
node_js: stable
55
install:
66
- npm install -g bower
77
- npm install

bower.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
},
2323
"license": "MIT",
2424
"dependencies": {
25-
"purescript-argonaut-core": "^2.0.0",
26-
"purescript-generics": "^3.0.0",
27-
"purescript-integers": "^2.0.0"
25+
"purescript-argonaut-core": "^3.0.0",
26+
"purescript-generics": "^4.0.0",
27+
"purescript-integers": "^3.0.0"
2828
},
2929
"devDependencies": {
30-
"purescript-strongcheck": "^2.0.0"
30+
"purescript-strongcheck": "^3.1.0"
3131
}
3232
}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"test": "pulp test"
77
},
88
"devDependencies": {
9-
"pulp": "^10.0.0",
10-
"purescript-psa": "^0.4.0",
11-
"purescript": "^0.10.7",
12-
"rimraf": "^2.5.4"
9+
"pulp": "^11.0.0",
10+
"purescript-psa": "^0.5.0",
11+
"purescript": "^0.11.1",
12+
"rimraf": "^2.6.1"
1313
}
1414
}

src/Data/Argonaut/Decode.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ module Data.Argonaut.Decode
33
, module Data.Argonaut.Decode.Combinators
44
) where
55

6-
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson, gDecodeJson, gDecodeJson')
6+
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
77
import Data.Argonaut.Decode.Combinators (getField, (.?))

src/Data/Argonaut/Decode/Class.purs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module Data.Argonaut.Decode.Class
22
( class DecodeJson
33
, decodeJson
4-
, gDecodeJson
5-
, gDecodeJson'
64
) where
75

86
import Prelude
@@ -12,7 +10,6 @@ import Data.Array (zipWithA)
1210
import Data.Bifunctor (lmap)
1311
import Data.Either (Either(..))
1412
import Data.Foldable (find)
15-
import Data.Generic (class Generic, GenericSpine(..), GenericSignature(..), fromSpine, toSignature)
1613
import Data.Int (fromNumber)
1714
import Data.List (List(..), (:), fromFoldable)
1815
import Data.Map as M
@@ -27,46 +24,6 @@ import Type.Proxy (Proxy(..))
2724
class DecodeJson a where
2825
decodeJson :: Json -> Either String a
2926

30-
-- | Decode `Json` representation of a value which has a `Generic` type.
31-
gDecodeJson :: forall a. Generic a => Json -> Either String a
32-
gDecodeJson
33-
= maybe (Left "fromSpine failed") Right
34-
<<< fromSpine
35-
<=< gDecodeJson' (toSignature (Proxy :: Proxy a))
36-
37-
-- | Decode `Json` representation of a `GenericSpine`.
38-
gDecodeJson' :: GenericSignature -> Json -> Either String GenericSpine
39-
gDecodeJson' signature json = case signature of
40-
SigNumber -> SNumber <$> mFail "Expected a number" (toNumber json)
41-
SigInt -> SInt <$> mFail "Expected an integer number" (fromNumber =<< toNumber json)
42-
SigString -> SString <$> mFail "Expected a string" (toString json)
43-
SigChar -> SChar <$> mFail "Expected a char" (toChar =<< toString json)
44-
SigBoolean -> SBoolean <$> mFail "Expected a boolean" (toBoolean json)
45-
SigUnit -> pure SUnit
46-
SigArray thunk -> do
47-
jArr <- mFail "Expected an array" $ toArray json
48-
SArray <$> traverse (map const <<< gDecodeJson' (thunk unit)) jArr
49-
SigRecord props -> do
50-
jObj <- mFail "Expected an object" $ toObject json
51-
SRecord <$> for props \({recLabel: lbl, recValue: val}) -> do
52-
pf <- mFail ("'" <> lbl <> "' property missing") (SM.lookup lbl jObj)
53-
sp <- gDecodeJson' (val unit) pf
54-
pure { recLabel: lbl, recValue: const sp }
55-
SigProd typeConstr alts -> do
56-
let decodingErr msg = "When decoding a " <> typeConstr <> ": " <> msg
57-
jObj <- mFail (decodingErr "expected an object") (toObject json)
58-
tagJson <- mFail (decodingErr "'tag' property is missing") (SM.lookup "tag" jObj)
59-
tag <- mFail (decodingErr "'tag' property is not a string") (toString tagJson)
60-
case find ((tag == _) <<< _.sigConstructor) alts of
61-
Nothing -> Left (decodingErr ("'" <> tag <> "' isn't a valid constructor"))
62-
Just { sigValues: sigValues } -> do
63-
vals <- mFail (decodingErr "'values' array is missing") (toArray =<< SM.lookup "values" jObj)
64-
sps <- zipWithA (\k -> gDecodeJson' (k unit)) sigValues vals
65-
pure (SProd tag (const <$> sps))
66-
where
67-
mFail :: forall a. String -> Maybe a -> Either String a
68-
mFail msg = maybe (Left msg) Right
69-
7027
instance decodeJsonMaybe :: DecodeJson a => DecodeJson (Maybe a) where
7128
decodeJson j =
7229
case decode j of

src/Data/Argonaut/Encode.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ module Data.Argonaut.Encode
33
, module Data.Argonaut.Encode.Combinators
44
) where
55

6-
import Data.Argonaut.Encode.Class (class EncodeJson, encodeJson, gEncodeJson, gEncodeJson')
6+
import Data.Argonaut.Encode.Class (class EncodeJson, encodeJson)
77
import Data.Argonaut.Encode.Combinators (assoc, extend, (:=), (~>))

src/Data/Argonaut/Encode/Class.purs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Prelude
55
import Data.Argonaut.Core (Json(), jsonNull, fromBoolean, fromNumber, fromString, fromArray, fromObject, jsonEmptyObject, jsonSingletonObject)
66
import Data.Either (Either(), either)
77
import Data.Foldable (foldr)
8-
import Data.Generic (class Generic, GenericSpine(..), toSpine)
98
import Data.Int (toNumber)
109
import Data.List (List(..), (:), toUnfoldable)
1110
import Data.Map as M
@@ -17,30 +16,6 @@ import Data.Tuple (Tuple(..))
1716
class EncodeJson a where
1817
encodeJson :: a -> Json
1918

20-
-- | Encode any `Generic` data structure into `Json`.
21-
gEncodeJson :: forall a. Generic a => a -> Json
22-
gEncodeJson = gEncodeJson' <<< toSpine
23-
24-
-- | Encode `GenericSpine` into `Json`.
25-
gEncodeJson' :: GenericSpine -> Json
26-
gEncodeJson' = case _ of
27-
SInt x -> fromNumber $ toNumber x
28-
SString x -> fromString x
29-
SChar x -> fromString $ singleton x
30-
SNumber x -> fromNumber x
31-
SBoolean x -> fromBoolean x
32-
SArray thunks -> fromArray (gEncodeJson' <<< (unit # _) <$> thunks)
33-
SUnit -> jsonNull
34-
SProd constr args ->
35-
fromObject
36-
$ SM.insert "tag" (encodeJson constr)
37-
$ SM.singleton "values" (encodeJson (gEncodeJson' <<< (unit # _) <$> args))
38-
SRecord fields ->
39-
fromObject $ foldr addField SM.empty fields
40-
where
41-
addField field =
42-
SM.insert field.recLabel (gEncodeJson' $ field.recValue unit)
43-
4419
instance encodeJsonMaybe :: EncodeJson a => EncodeJson (Maybe a) where
4520
encodeJson Nothing = jsonEmptyObject
4621
encodeJson (Just a) = jsonSingletonObject "just" (encodeJson a)
@@ -87,7 +62,7 @@ instance encodeStrMap :: EncodeJson a => EncodeJson (SM.StrMap a) where
8762
encodeJson = fromObject <<< map encodeJson
8863

8964
instance encodeMap :: (Ord a, EncodeJson a, EncodeJson b) => EncodeJson (M.Map a b) where
90-
encodeJson = encodeJson <<< M.toList
65+
encodeJson = encodeJson <<< (M.toUnfoldable :: M.Map a b -> List (Tuple a b))
9166

9267
instance encodeVoid :: EncodeJson Void where
9368
encodeJson = absurd

test/Test/Main.purs

Lines changed: 7 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import Prelude
44

55
import Control.Monad.Eff.Console (log, logShow)
66

7-
import Data.Argonaut.Core (JObject, Json, toObject, fromObject, fromArray, fromString, fromNumber, fromBoolean, jsonNull)
7+
import Data.Argonaut.Core (JObject, Json, isObject, toObject, fromObject, fromArray, fromString, fromNumber, fromBoolean, jsonNull)
88
import Data.Argonaut.Decode (decodeJson)
9-
import Data.Argonaut.Encode (encodeJson, gEncodeJson, (:=), (~>))
9+
import Data.Argonaut.Encode (encodeJson, (:=), (~>))
10+
import Data.Argonaut.Gen (genJson)
1011
import Data.Array (zipWith, nubBy, length)
1112
import Data.Either (Either(..))
1213
import Data.Foldable (foldl)
1314
import Data.Function (on)
14-
import Data.Generic (class Generic)
1515
import Data.List (fromFoldable)
1616
import Data.Maybe (Maybe(..), maybe, isJust)
1717
import Data.StrMap as SM
@@ -20,67 +20,26 @@ import Data.Tuple (Tuple(..), fst)
2020
import Test.StrongCheck (SC, quickCheck, quickCheck', (<?>))
2121
import Test.StrongCheck.Arbitrary (class Arbitrary, arbitrary)
2222
import Test.StrongCheck.Data.AlphaNumString (AlphaNumString(..))
23-
import Test.StrongCheck.Gen (Gen, Size, showSample, sized, frequency, oneOf, vectorOf)
23+
import Test.StrongCheck.Gen (Gen, Size, showSample, sized, frequency, oneOf, vectorOf, suchThat, resize)
2424

2525
main :: SC () Unit
2626
main = do
2727
eitherCheck
2828
encodeDecodeCheck
2929
combinatorsCheck
30-
genericsCheck
31-
32-
genJNull :: Gen Json
33-
genJNull = pure jsonNull
34-
35-
genJBool :: Gen Json
36-
genJBool = fromBoolean <$> arbitrary
37-
38-
genJNumber :: Gen Json
39-
genJNumber = fromNumber <$> arbitrary
40-
41-
genJString :: Gen Json
42-
genJString = fromString <$> arbitrary
43-
44-
genJArray :: Size -> Gen Json
45-
genJArray sz = fromArray <$> vectorOf sz (genJson $ sz - 1)
46-
47-
genJObject :: Size -> Gen Json
48-
genJObject sz = do
49-
v <- vectorOf sz (genJson $ sz - 1)
50-
k <- vectorOf (length v) (arbitrary :: Gen AlphaNumString)
51-
pure
52-
let
53-
f (AlphaNumString s) = s <> "x"
54-
k' = f <$> k
55-
in
56-
fromObject <<< SM.fromFoldable <<< nubBy (eq `on` fst) $ zipWith Tuple k' v
57-
58-
genJson :: Size -> Gen Json
59-
genJson 0 = oneOf genJNull [genJBool, genJNumber, genJString]
60-
genJson n = frequency (Tuple 1.0 genJNull) rest where
61-
rest = fromFoldable
62-
[ Tuple 2.0 genJBool
63-
, Tuple 2.0 genJNumber
64-
, Tuple 3.0 genJString
65-
, Tuple 1.0 (genJArray n)
66-
, Tuple 1.0 (genJObject n)
67-
]
6830

6931
newtype TestJson = TestJson Json
7032

7133
instance arbitraryTestJson :: Arbitrary TestJson where
72-
arbitrary = TestJson <$> sized genJson
34+
arbitrary = TestJson <$> (resize 5 genJson)
7335

7436
encodeDecodeCheck :: SC () Unit
7537
encodeDecodeCheck = do
76-
log "Showing small sample of JSON"
77-
showSample (genJson 10)
78-
7938
log "Testing that any JSON can be encoded and then decoded"
8039
quickCheck' 20 prop_encode_then_decode
8140

8241
log "Testing that any JSON can be decoded and then encoded"
83-
quickCheck' 20 prop_decode_then_encode
42+
quickCheck' 20 (prop_decode_then_encode)
8443

8544
where
8645

@@ -98,7 +57,7 @@ unObj :: Obj -> Json
9857
unObj (Obj j) = j
9958

10059
instance arbitraryObj :: Arbitrary Obj where
101-
arbitrary = Obj <$> genJObject 5
60+
arbitrary = Obj <$> suchThat (resize 5 genJson) isObject
10261

10362
combinatorsCheck :: SC () Unit
10463
combinatorsCheck = do
@@ -133,58 +92,6 @@ combinatorsCheck = do
13392
let keys = SM.keys obj
13493
in foldl (\ok key -> ok && isJust (SM.lookup key obj)) true keys
13594

136-
newtype MyRecord = MyRecord { foo :: String, bar :: Int }
137-
138-
derive instance genericMyRecord :: Generic MyRecord
139-
140-
data User
141-
= Anonymous
142-
| Guest String
143-
| Registered
144-
{ name :: String
145-
, bio :: Maybe String
146-
, age :: Int
147-
, balance :: Number
148-
, banned :: Boolean
149-
, tweets :: Array String
150-
, followers :: Array User
151-
}
152-
153-
derive instance genericUser :: Generic User
154-
155-
genericsCheck :: SC () Unit
156-
genericsCheck = do
157-
log "Print samples of values encoded with gEncodeJson"
158-
logShow $ gEncodeJson 5
159-
logShow $ gEncodeJson [1, 2, 3, 5]
160-
logShow $ gEncodeJson (Just "foo")
161-
logShow $ gEncodeJson (Right "foo" :: Either String String)
162-
logShow $ gEncodeJson $ MyRecord { foo: "foo", bar: 2}
163-
logShow $ gEncodeJson "foo"
164-
logShow $ gEncodeJson Anonymous
165-
logShow $ gEncodeJson $ Guest "guest's handle"
166-
logShow $ gEncodeJson $ Registered
167-
{ name: "user1"
168-
, bio: Just "Ordinary User"
169-
, age: 5
170-
, balance: 26.6
171-
, banned: false
172-
, tweets: ["Hello", "What's up"]
173-
, followers:
174-
[ Anonymous
175-
, Guest "someGuest"
176-
, Registered
177-
{ name: "user2"
178-
, bio: Nothing
179-
, age: 6
180-
, balance: 32.1
181-
, banned: false
182-
, tweets: ["Hi"]
183-
, followers: []
184-
}
185-
]
186-
}
187-
18895
eitherCheck :: SC () Unit
18996
eitherCheck = do
19097
log "Test EncodeJson/DecodeJson Either instance"

0 commit comments

Comments
 (0)