-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from purescript/either-decoding
Either decoding for IsForeign
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Examples.Either where | ||
|
||
import Prelude | ||
import Control.Monad.Eff (Eff) | ||
import Data.Either (Either) | ||
|
||
import Data.Foreign (F, parseJSON) | ||
import Data.Foreign.Class (class IsForeign, readEitherR, readProp) | ||
|
||
import Control.Monad.Eff.Console (logShow, CONSOLE) | ||
|
||
data Point = Point Number Number Number | ||
|
||
instance showPoint :: Show Point where | ||
show (Point x y z) = "Point " <> show [x, y, z] | ||
|
||
instance pointIsForeign :: IsForeign Point where | ||
read value = Point <$> readProp "x" value | ||
<*> readProp "y" value | ||
<*> readProp "z" value | ||
|
||
type Response = Either (Array String) Point | ||
|
||
main :: forall eff. Eff (console :: CONSOLE | eff) Unit | ||
main = do | ||
logShow do | ||
json <- parseJSON """{ "x":1, "y": 2, "z": 3}""" | ||
readEitherR json :: F Response | ||
|
||
logShow do | ||
json <- parseJSON """["Invalid parse", "Not a valid y point"]""" | ||
readEitherR json :: F Response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters