Skip to content

Commit

Permalink
Merge pull request #22 from justinwoo/nullorundefined-instances
Browse files Browse the repository at this point in the history
add decode/encode instances for NullOrUndefined
  • Loading branch information
paf31 authored Apr 13, 2017
2 parents ad0d657 + f54bca6 commit f43b4af
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Data/Foreign/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Control.Monad.Except (mapExcept)
import Data.Array ((..), zipWith, length)
import Data.Bifunctor (lmap)
import Data.Foreign (F, Foreign, ForeignError(ErrorAtIndex), readArray, readBoolean, readChar, readInt, readNumber, readString, toForeign)
import Data.Foreign.NullOrUndefined (NullOrUndefined(..), readNullOrUndefined, undefined)
import Data.Maybe (maybe)
import Data.Traversable (sequence)

-- | The `Decode` class is used to generate decoding functions
Expand Down Expand Up @@ -89,3 +91,9 @@ instance intEncode :: Encode Int where

instance arrayEncode :: Encode a => Encode (Array a) where
encode = toForeign <<< map encode

instance decodeNullOrUndefined :: Decode a => Decode (NullOrUndefined a) where
decode = readNullOrUndefined decode

instance encodeNullOrUndefined :: Encode a => Encode (NullOrUndefined a) where
encode (NullOrUndefined a) = maybe undefined encode a
1 change: 1 addition & 0 deletions src/Data/Foreign/NullOrUndefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports['undefined'] = undefined;
2 changes: 2 additions & 0 deletions src/Data/Foreign/NullOrUndefined.purs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ unNullOrUndefined (NullOrUndefined m) = m
readNullOrUndefined :: forall a. (Foreign -> F a) -> Foreign -> F (NullOrUndefined a)
readNullOrUndefined _ value | isNull value || isUndefined value = pure (NullOrUndefined Nothing)
readNullOrUndefined f value = NullOrUndefined <<< Just <$> f value

foreign import undefined :: Foreign
8 changes: 7 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import Data.Bifunctor (bimap)
import Data.Either (Either(..))
import Data.Foreign.Class (class Encode, class Decode)
import Data.Foreign.Generic (decodeJSON, encodeJSON)
import Data.Foreign.NullOrUndefined (NullOrUndefined(..))
import Data.Maybe (Maybe(..))
import Data.Tuple (Tuple(..))
import Test.Assert (assert, assert', ASSERT)
import Test.Types (IntList(..), RecordTest(..), Tree(..), TupleArray(..))
import Test.Types (IntList(..), RecordTest(..), Tree(..), TupleArray(..), UndefinedTest(..))

buildTree :: forall a. (a -> TupleArray a a) -> Int -> a -> Tree a
buildTree _ 0 a = Leaf a
Expand Down Expand Up @@ -44,5 +46,9 @@ main :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
main = do
testRoundTrip (RecordTest { foo: 1, bar: "test", baz: 'a' })
testRoundTrip (Cons 1 (Cons 2 (Cons 3 Nil)))
testRoundTrip (UndefinedTest {a: NullOrUndefined (Just "test")})
testRoundTrip (UndefinedTest {a: NullOrUndefined Nothing})
testRoundTrip [NullOrUndefined (Just "test")]
testRoundTrip [NullOrUndefined (Nothing :: Maybe String)]
testRoundTrip (makeTree 0)
testRoundTrip (makeTree 5)
13 changes: 13 additions & 0 deletions test/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Data.Bifunctor (class Bifunctor)
import Data.Foreign (ForeignError(ForeignError), fail, readArray, toForeign)
import Data.Foreign.Class (class Encode, class Decode, encode, decode)
import Data.Foreign.Generic (defaultOptions, genericDecode, genericEncode)
import Data.Foreign.NullOrUndefined (NullOrUndefined)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
import Data.Generic.Rep.Show (genericShow)
Expand Down Expand Up @@ -86,3 +87,15 @@ instance decodeTree :: Decode a => Decode (Tree a) where

instance encodeTree :: Encode a => Encode (Tree a) where
encode x = genericEncode defaultOptions x

newtype UndefinedTest = UndefinedTest
{ a :: NullOrUndefined String
}

derive instance eqUT :: Eq UndefinedTest
derive instance geUT :: Generic UndefinedTest _

instance dUT :: Decode UndefinedTest where
decode = genericDecode $ defaultOptions
instance eUT :: Encode UndefinedTest where
encode = genericEncode $ defaultOptions

0 comments on commit f43b4af

Please sign in to comment.