Skip to content

Commit 4ca5fec

Browse files
authored
Update package set from 0.13.6-20200507 to 0.13.8-20201223 (#267)
* Update package set from 0.13.6-20200507 to 0.13.8-20201223 * Ch10 - fix code broken by update * Ch8 - update fixes
1 parent de1a68f commit 4ca5fec

File tree

22 files changed

+50
-45
lines changed

22 files changed

+50
-45
lines changed

exercises/chapter10/packages.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let upstream =
2-
https://github.com/purescript/package-sets/releases/download/psc-0.13.6-20200507/packages.dhall sha256:9c1e8951e721b79de1de551f31ecb5a339e82bbd43300eb5ccfb1bf8cf7bbd62
2+
https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20201223/packages.dhall sha256:a1a8b096175f841c4fef64c9b605fb0d691229241fd2233f6cf46e213de8a185
33

44
let overrides =
55
{ test-unit =

exercises/chapter10/spago.dhall

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ You can edit this file as you like.
1111
, "effect"
1212
, "pairs"
1313
, "psci-support"
14+
, "react-basic-dom"
1415
, "react-basic-hooks"
1516
, "test-unit"
1617
, "validation"

exercises/chapter10/src/Main.purs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module Main where
22

33
import Prelude
4+
45
import Data.AddressBook (PhoneNumber, Person, examplePerson)
56
import Data.AddressBook.Validation (Errors, validatePerson')
6-
import Data.Argonaut (Json, decodeJson, encodeJson, jsonParser, stringify)
7+
import Data.Argonaut (Json, decodeJson, encodeJson, jsonParser, printJsonDecodeError, stringify)
78
import Data.Array (length, mapWithIndex, updateAt)
89
import Data.Bifunctor (lmap)
910
import Data.Either (Either(..))
@@ -156,9 +157,9 @@ mkAddressBookApp =
156157

157158
processItem :: Json -> Either String Person
158159
processItem item = do
159-
jsonString <- lmap ("No string in local storage: " <> _) $ decodeJson item
160-
j <- lmap ("Cannot parse JSON string: " <> _) $ jsonParser jsonString
161-
lmap ("Cannot decode Person: " <> _) $ decodeJson j
160+
jsonString <- lmap (\e -> "No string in local storage: " <> printJsonDecodeError e) $ decodeJson item
161+
j <- lmap ( "Cannot parse JSON string: " <> _) $ jsonParser jsonString
162+
lmap (\e -> "Cannot decode Person: " <> printJsonDecodeError e) $ decodeJson j
162163

163164
main :: Effect Unit
164165
main = do

exercises/chapter10/test/Examples.purs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Test.Examples where
33
import Prelude
44

55
import Control.Promise (Promise, toAffE)
6-
import Data.Argonaut (Json, decodeJson, encodeJson)
6+
import Data.Argonaut (Json, JsonDecodeError, decodeJson, encodeJson)
77
import Data.Either (Either)
88
import Data.Function.Uncurried (Fn2, mkFn2)
99
import Data.Map (Map)
@@ -90,17 +90,17 @@ foreign import addComplexBroken :: Complex -> Complex -> Complex
9090

9191
foreign import cumulativeSumsJson :: Array Int -> Json
9292

93-
cumulativeSumsDecoded :: Array Int -> Either String (Array Int)
93+
cumulativeSumsDecoded :: Array Int -> Either JsonDecodeError (Array Int)
9494
cumulativeSumsDecoded arr = decodeJson $ cumulativeSumsJson arr
9595

9696
foreign import addComplexJson :: Complex -> Complex -> Json
9797

98-
addComplexDecoded :: Complex -> Complex -> Either String Complex
98+
addComplexDecoded :: Complex -> Complex -> Either JsonDecodeError Complex
9999
addComplexDecoded a b = decodeJson $ addComplexJson a b
100100

101101
foreign import mapSetFooJson :: Json -> Json
102102

103-
mapSetFoo :: Map String Int -> Either String (Map String Int)
103+
mapSetFoo :: Map String Int -> Either JsonDecodeError (Map String Int)
104104
mapSetFoo = encodeJson >>> mapSetFooJson >>> decodeJson
105105

106106
{-
@@ -111,20 +111,20 @@ between versions by the reader.
111111
-}
112112
foreign import cumulativeSumsJsonBroken :: Array Int -> Json
113113

114-
cumulativeSumsDecodedBroken :: Array Int -> Either String (Array Int)
114+
cumulativeSumsDecodedBroken :: Array Int -> Either JsonDecodeError (Array Int)
115115
cumulativeSumsDecodedBroken = cumulativeSumsJsonBroken >>> decodeJson
116116

117117
foreign import addComplexJsonBroken :: Complex -> Complex -> Json
118118

119-
addComplexDecodedBroken :: Complex -> Complex -> Either String Complex
119+
addComplexDecodedBroken :: Complex -> Complex -> Either JsonDecodeError Complex
120120
addComplexDecodedBroken a b = decodeJson $ addComplexJsonBroken a b
121121

122122
foreign import cumulativeSumsJsonWorking :: Array Int -> Json
123123

124-
cumulativeSumsDecodedWorking :: Array Int -> Either String (Array Int)
124+
cumulativeSumsDecodedWorking :: Array Int -> Either JsonDecodeError (Array Int)
125125
cumulativeSumsDecodedWorking = cumulativeSumsJsonWorking >>> decodeJson
126126

127127
foreign import addComplexJsonWorking :: Complex -> Complex -> Json
128128

129-
addComplexDecodedWorking :: Complex -> Complex -> Either String Complex
129+
addComplexDecodedWorking :: Complex -> Complex -> Either JsonDecodeError Complex
130130
addComplexDecodedWorking a b = decodeJson $ addComplexJsonWorking a b

exercises/chapter10/test/Main.purs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Test.Examples
55
import Test.MySolutions
66

77
import Control.Monad.Free (Free)
8-
import Data.Argonaut (decodeJson, encodeJson)
8+
import Data.Argonaut (JsonDecodeError(..), decodeJson, encodeJson)
99
import Data.Either (Either(..), isLeft)
1010
import Data.Function.Uncurried (runFn2, runFn3)
1111
import Data.Map as Map
@@ -144,11 +144,11 @@ main =
144144
{ a: 3.0, b: -6.0, c: 3.0 }
145145
{ real: 1.0, imag: 0.0 }
146146
{ real: 1.0, imag: 0.0 }
147-
test "Exercise - decodeArray2D" do
147+
test "Exercise - parseAndDecodeArray2D" do
148148
let
149149
arr = [ [ 1, 2, 3 ], [ 4, 5 ], [ 6 ] ]
150150
Assert.equal (Right arr)
151-
$ decodeArray2D
151+
$ parseAndDecodeArray2D
152152
$ show arr -- the correct JSON string happens to also be produced by show
153153
test "Exercise - encode decode Tree" do
154154
let
@@ -284,14 +284,14 @@ runChapterExamples =
284284
Assert.equal 5.0 result
285285
suite "cumulativeSums Json" do
286286
test "broken" do
287-
Assert.equal (Left "Couldn't decode Array (Failed at index 3): Value is not a Number")
287+
Assert.equal (Left $ Named "Array" $ AtIndex 3 $ TypeMismatch "Number")
288288
$ cumulativeSumsDecodedBroken [ 1, 2, 3 ]
289289
test "working" do
290290
Assert.equal (Right [ 1, 3, 6 ])
291291
$ cumulativeSumsDecodedWorking [ 1, 2, 3 ]
292292
suite "addComplex Json" do
293293
test "broken" do
294-
Assert.equal (Left "JSON was missing expected field: imag")
294+
Assert.equal (Left $ AtKey "imag" MissingValue)
295295
$ addComplexDecodedBroken { real: 1.0, imag: 2.0 } { real: 3.0, imag: 4.0 }
296296
test "working" do
297297
Assert.equal (Right { imag: 6.0, real: 4.0 })

exercises/chapter10/test/MySolutions.purs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
module Test.MySolutions where
22

33
import Prelude
4+
45
import Control.Alt (alt)
56
import Control.Apply (lift2)
6-
import Data.Argonaut (class DecodeJson, class EncodeJson, Json, decodeJson, encodeJson, jsonParser)
7+
import Data.Argonaut (class DecodeJson, class EncodeJson, Json, JsonDecodeError(..), decodeJson, encodeJson, jsonParser, printJsonDecodeError)
78
import Data.Argonaut.Decode.Generic.Rep (genericDecodeJson)
89
import Data.Argonaut.Encode.Generic.Rep (genericEncodeJson)
10+
import Data.Bifunctor (lmap)
911
import Data.Either (Either(..))
1012
import Data.Foldable (foldr)
1113
import Data.Function.Uncurried (Fn3)
@@ -30,7 +32,7 @@ quadraticRoots poly = quadraticRootsImpl Pair poly
3032

3133
foreign import valuesOfMapJson :: Json -> Json
3234

33-
valuesOfMap :: Map String Int -> Either String (Set Int)
35+
valuesOfMap :: Map String Int -> Either JsonDecodeError (Set Int)
3436
valuesOfMap = encodeJson >>> valuesOfMapJson >>> decodeJson
3537

3638
valuesOfMapGeneric ::
@@ -41,12 +43,12 @@ valuesOfMapGeneric ::
4143
Ord k =>
4244
Ord v =>
4345
Map k v ->
44-
Either String (Set v)
46+
Either JsonDecodeError (Set v)
4547
valuesOfMapGeneric = encodeJson >>> valuesOfMapJson >>> decodeJson
4648

4749
foreign import quadraticRootsSetJson :: Json -> Json
4850

49-
quadraticRootsSet :: Quadratic -> Either String (Set Complex)
51+
quadraticRootsSet :: Quadratic -> Either JsonDecodeError (Set Complex)
5052
quadraticRootsSet = encodeJson >>> quadraticRootsSetJson >>> decodeJson
5153

5254
foreign import quadraticRootsSafeJson :: Json -> Json
@@ -59,18 +61,20 @@ instance decodeJsonWrapPair :: DecodeJson a => DecodeJson (WrapPair a) where
5961
decoded <- decodeJson j
6062
case decoded of
6163
[ a, b ] -> map WrapPair $ lift2 Pair (decodeJson a) (decodeJson b)
62-
_ -> Left "Couldn't decode WrapPair"
64+
[ ] -> Left $ AtIndex 0 MissingValue
65+
[ a ] -> Left $ AtIndex 1 MissingValue
66+
_ -> Left $ AtIndex 2 $ UnexpectedValue j
6367

64-
quadraticRootsSafeWrap :: Quadratic -> Either String (WrapPair Complex)
68+
quadraticRootsSafeWrap :: Quadratic -> Either JsonDecodeError (WrapPair Complex)
6569
quadraticRootsSafeWrap = encodeJson >>> quadraticRootsSafeJson >>> decodeJson
6670

67-
quadraticRootsSafe :: Quadratic -> Either String (Pair Complex)
71+
quadraticRootsSafe :: Quadratic -> Either JsonDecodeError (Pair Complex)
6872
quadraticRootsSafe = quadraticRootsSafeWrap >>> map (\(WrapPair p) -> p)
6973

70-
decodeArray2D :: String -> Either String (Array (Array Int))
71-
decodeArray2D str = do
74+
parseAndDecodeArray2D :: String -> Either String (Array (Array Int))
75+
parseAndDecodeArray2D str = do
7276
j <- jsonParser str
73-
decodeJson j
77+
lmap printJsonDecodeError $ decodeJson j
7478

7579
data Tree a
7680
= Leaf a
@@ -100,7 +104,7 @@ instance encodeJsonIntOrString :: EncodeJson IntOrString where
100104

101105
instance decodeJsonIntOrString :: DecodeJson IntOrString where
102106
decodeJson j =
103-
foldr alt (Left "Could not decode IntOrString")
107+
foldr alt (Left $ TypeMismatch "Not Int or String")
104108
[ map IntOrString_Int $ decodeJson j
105109
, map IntOrString_String $ decodeJson j
106110
]

exercises/chapter11/packages.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let upstream =
2-
https://github.com/purescript/package-sets/releases/download/psc-0.13.6-20200507/packages.dhall sha256:9c1e8951e721b79de1de551f31ecb5a339e82bbd43300eb5ccfb1bf8cf7bbd62
2+
https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20201223/packages.dhall sha256:a1a8b096175f841c4fef64c9b605fb0d691229241fd2233f6cf46e213de8a185
33

44
let overrides =
55
{ test-unit =

exercises/chapter12/packages.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let upstream =
2-
https://github.com/purescript/package-sets/releases/download/psc-0.13.6-20200507/packages.dhall sha256:9c1e8951e721b79de1de551f31ecb5a339e82bbd43300eb5ccfb1bf8cf7bbd62
2+
https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20201223/packages.dhall sha256:a1a8b096175f841c4fef64c9b605fb0d691229241fd2233f6cf46e213de8a185
33

44
let overrides =
55
{ test-unit =

exercises/chapter13/packages.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let upstream =
2-
https://github.com/purescript/package-sets/releases/download/psc-0.13.6-20200507/packages.dhall sha256:9c1e8951e721b79de1de551f31ecb5a339e82bbd43300eb5ccfb1bf8cf7bbd62
2+
https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20201223/packages.dhall sha256:a1a8b096175f841c4fef64c9b605fb0d691229241fd2233f6cf46e213de8a185
33

44
let overrides =
55
{ test-unit =

exercises/chapter14/packages.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let upstream =
2-
https://github.com/purescript/package-sets/releases/download/psc-0.13.6-20200507/packages.dhall sha256:9c1e8951e721b79de1de551f31ecb5a339e82bbd43300eb5ccfb1bf8cf7bbd62
2+
https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20201223/packages.dhall sha256:a1a8b096175f841c4fef64c9b605fb0d691229241fd2233f6cf46e213de8a185
33

44
let overrides =
55
{ test-unit =

0 commit comments

Comments
 (0)