Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Either instance for IsForeign #32

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions examples/Either.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Examples.Either where

import Prelude
import Data.Either

import Data.Foreign
import Data.Foreign.Class

import Control.Monad.Eff.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 = do
print $
readJSON """{ "x":1, "y": 2, "z": 3}""" :: F Response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this typecheck?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't, and neither psc-ide nor pulp -w build compile the examples directory so I forgot to change it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, thought I was missing something 😄

print $
readJSON """["Invalid parse", "Not a valid y point"]""" :: F Response
11 changes: 11 additions & 0 deletions src/Data/Foreign/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Data.Foreign.Class

import Prelude

import Control.Alt ((<|>))
import Data.Array (range, zipWith, length)
import Data.Either (Either(..), either)
import Data.Foreign (F, Foreign, ForeignError(..), parseJSON, readArray, readInt, readNumber, readBoolean, readChar, readString)
Expand Down Expand Up @@ -75,3 +76,13 @@ readWith f value = either (Left <<< f) Right (read value)
-- | Attempt to read a property of a foreign value at the specified index
readProp :: forall a i. (IsForeign a, Index i) => i -> Foreign -> F a
readProp prop value = value ! prop >>= readWith (errorAt prop)

-- | Attempt to read a value that can be either one thing or another. This
-- | implementation is right biased.
readEitherR :: forall l r. (IsForeign l, IsForeign r) => Foreign -> F (Either l r)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

readEitherR value = Right <$> read value <|> Left <$> read value

-- | Attempt to read a value that can be either one thing or another. This
-- | implementation is left biased.
readEitherL :: forall l r. (IsForeign l, IsForeign r) => Foreign -> F (Either l r)
readEitherL value = Left <$> read value <|> Right <$> read value