Skip to content

Commit

Permalink
Merge pull request #35 from justinwoo/update
Browse files Browse the repository at this point in the history
update dependencies, remove NullOrUndefined
  • Loading branch information
justinwoo authored Apr 24, 2018
2 parents 94a799f + bf15c4c commit 50fde46
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 26 deletions.
6 changes: 3 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"output"
],
"dependencies": {
"purescript-prelude": "^3.1.0",
"purescript-foreign-generic": "^5.0.0",
"purescript-typelevel-prelude": "^2.6.0",
"purescript-prelude": "^3.3.0",
"purescript-foreign-generic": "^6.0.0",
"purescript-typelevel-prelude": "^2.7.0",
"purescript-record": "^0.2.6",
"purescript-nullable": "^3.0.0",
"purescript-variant": "^4.1.0"
Expand Down
17 changes: 5 additions & 12 deletions src/Simple/JSON.purs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import Data.Foreign (F, Foreign, ForeignError(..), MultipleErrors, fail, readArr
import Data.Foreign.Index (readProp)
import Data.Foreign.Internal (readStrMap)
import Data.Foreign.JSON (parseJSON)
import Data.Foreign.NullOrUndefined (NullOrUndefined(NullOrUndefined), readNullOrUndefined, unNullOrUndefined, undefined)
import Data.Maybe (Maybe(..), maybe)
import Data.Foreign.NullOrUndefined (readNullOrUndefined, undefined)
import Data.Maybe (Maybe(Nothing), maybe)
import Data.Nullable (Nullable, toMaybe, toNullable)
import Data.Record (get)
import Data.Record.Builder (Builder)
Expand Down Expand Up @@ -110,11 +110,8 @@ instance readBoolean :: ReadForeign Boolean where
instance readArray :: ReadForeign a => ReadForeign (Array a) where
readImpl = traverse readImpl <=< readArray

instance readNullOrUndefined :: ReadForeign a => ReadForeign (NullOrUndefined a) where
readImpl = readNullOrUndefined readImpl

instance readMaybe :: ReadForeign a => ReadForeign (Maybe a) where
readImpl = map unNullOrUndefined <<< readImpl
readImpl = readNullOrUndefined readImpl

instance readNullable :: ReadForeign a => ReadForeign (Nullable a) where
readImpl o = withExcept (map reformat) $
Expand Down Expand Up @@ -230,15 +227,11 @@ instance writeForeignBoolean :: WriteForeign Boolean where
instance writeForeignArray :: WriteForeign a => WriteForeign (Array a) where
writeImpl xs = toForeign $ writeImpl <$> xs

instance writeForeignNullOrUndefined :: WriteForeign a => WriteForeign (NullOrUndefined a) where
writeImpl (NullOrUndefined a) = maybe undefined writeImpl a

instance writeForeignMaybe :: WriteForeign a => WriteForeign (Maybe a) where
writeImpl (Just a) = writeImpl a
writeImpl Nothing = toForeign $ toNullable Nothing
writeImpl = maybe undefined writeImpl

instance writeForeignNullable :: WriteForeign a => WriteForeign (Nullable a) where
writeImpl = writeImpl <<< toMaybe
writeImpl = maybe (toForeign $ toNullable Nothing) writeImpl <<< toMaybe

instance writeForeignStrMap :: WriteForeign a => WriteForeign (StrMap.StrMap a) where
writeImpl = toForeign <<< StrMap.mapWithKey (const writeImpl)
Expand Down
15 changes: 4 additions & 11 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Data.Argonaut.Core (Json)
import Data.Argonaut.Parser (jsonParser)
import Data.Either (Either(..), either, fromLeft, isRight)
import Data.Foreign (ForeignError(..), MultipleErrors)
import Data.Foreign.NullOrUndefined (NullOrUndefined)
import Data.List (List(..))
import Data.List.NonEmpty (NonEmptyList(..))
import Data.Maybe (Maybe)
Expand Down Expand Up @@ -38,7 +37,7 @@ type MyTestNull =
, b :: String
, c :: Boolean
, d :: Array String
, e :: NullOrUndefined (Array String)
, e :: Maybe (Array String)
}

type MyTestStrMap =
Expand Down Expand Up @@ -102,7 +101,7 @@ main = run [consoleReporter] do

it "works with missing Maybe fields by setting them to Nothing" do
let result = readJSON "{}"
(writeJSON <$> (result :: E MyTestMaybe)) `shouldEqual` (Right """{"a":null}""")
(writeJSON <$> (result :: E MyTestMaybe)) `shouldEqual` (Right """{}""")

it "fails with undefined for null with correct error message" do
let result = readJSON """
Expand All @@ -116,10 +115,10 @@ main = run [consoleReporter] do
it "works with proper JSON" $ roundtrips (Proxy :: Proxy MyTest) """
{ "a": 1, "b": "asdf", "c": true, "d": ["A", "B"]}
"""
it "works with JSON lacking NullOrUndefined field" $ roundtrips (Proxy :: Proxy MyTestNull) """
it "works with JSON lacking Maybe field" $ roundtrips (Proxy :: Proxy MyTestNull) """
{ "a": 1, "b": "asdf", "c": true, "d": ["A", "B"]}
"""
it "works with JSON containing NullOrUndefined field" $ roundtrips (Proxy :: Proxy MyTestNull) """
it "works with JSON containing Maybe field" $ roundtrips (Proxy :: Proxy MyTestNull) """
{ "a": 1, "b": "asdf", "c": true, "d": ["A", "B"], "e": ["C", "D"]}
"""
it "works with JSON containing StrMap field" $ roundtrips (Proxy :: Proxy MyTestStrMap) """
Expand All @@ -128,12 +127,6 @@ main = run [consoleReporter] do
it "works with Maybe field and existing value" $ roundtrips (Proxy :: Proxy MyTestMaybe) """
{ "a": "foo" }
"""
it "works with Maybe field and null value" $ roundtrips (Proxy :: Proxy MyTestMaybe) """
{ "a": null }
"""
it "works with many Maybe fields" $ roundtrips (Proxy :: Proxy MyTestManyMaybe) """
{ "a": "str", "aNull": null, "b":1, "bNull": null, "c":true, "cNull":null, "d":1.1, "dNull":null, "e":["str1", "str2", null], "eNull": null }
"""
it "works with Nullable" $ roundtrips (Proxy :: Proxy MyTestNullable) """
{ "a": null, "b": "a" }
"""
Expand Down

0 comments on commit 50fde46

Please sign in to comment.