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 1 commit
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
1 change: 1 addition & 0 deletions docs/Data/Foreign/Class.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ instance booleanIsForeign :: IsForeign Boolean
instance numberIsForeign :: IsForeign Number
instance intIsForeign :: IsForeign Int
instance arrayIsForeign :: (IsForeign a) => IsForeign (Array a)
instance eitherIsForeign :: (IsForeign l, IsForeign r) => IsForeign (Either l r)
instance nullIsForeign :: (IsForeign a) => IsForeign (Null a)
instance undefinedIsForeign :: (IsForeign a) => IsForeign (Undefined a)
instance nullOrUndefinedIsForeign :: (IsForeign a) => IsForeign (NullOrUndefined a)
Expand Down
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
9 changes: 9 additions & 0 deletions src/Data/Foreign/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ instance arrayIsForeign :: (IsForeign a) => IsForeign (Array a) where
readElement :: forall a. (IsForeign a) => Int -> Foreign -> F (Array a)
readElement i value = readWith (ErrorAtIndex i) value

instance eitherIsForeign :: (IsForeign l, IsForeign r) => IsForeign (Either l r) where
read value =
case (read value) :: F r of
Right rval -> pure (Right rval)
Copy link
Contributor

Choose a reason for hiding this comment

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

So this prefers the Right parse then? I think Left would be more common (aeson etc.)

Also, you can probably shorten this using <|>.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup! My thinking is: Attempt to parse the thing we actually want, and if that fails, then attempt to parse the error case. If you have Either String SomeType then String would always succeed even if SomeType could have been parsed.

Left e ->
case (read value) :: F l of
Right lval -> pure (Left lval)
Left e' -> Left e'

instance nullIsForeign :: (IsForeign a) => IsForeign (Null a) where
read = readNull read

Expand Down