Skip to content

Commit b3cf6c7

Browse files
committed
updates for 0.10
1 parent b744681 commit b3cf6c7

File tree

5 files changed

+47
-33
lines changed

5 files changed

+47
-33
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
bower_components
2-
output
31
.psci*
2+
bower_components/
3+
output/
4+
.psc-package
5+
.psc-ide-port

bower.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
"url": "git://github.com/paf31/purescript-foreign-generic.git"
1616
},
1717
"dependencies": {
18-
"purescript-console": "^1.0.0",
19-
"purescript-eff": "^1.0.0",
20-
"purescript-exceptions": "^1.0.0",
21-
"purescript-foreign": "^1.0.0",
22-
"purescript-generics": "^1.0.0",
23-
"purescript-globals": "^1.0.0",
24-
"purescript-maps": "^1.0.0",
25-
"purescript-nullable": "^1.0.0"
18+
"purescript-console": "^2.0.0",
19+
"purescript-eff": "^2.0.0",
20+
"purescript-exceptions": "^2.0.0",
21+
"purescript-foreign": "^3.0.0",
22+
"purescript-generics": "^3.0.0",
23+
"purescript-globals": "^2.0.0",
24+
"purescript-maps": "^2.0.0",
25+
"purescript-nullable": "^2.0.0",
26+
"purescript-symbols": "^1.0.1"
2627
},
2728
"devDependencies": {
28-
"purescript-assert": "^1.0.0"
29+
"purescript-assert": "^2.0.0"
2930
}
3031
}

psc-package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "foreign-generic",
3+
"source": "https://github.com/purescript/package-sets.git",
4+
"set": "psc-0.10.1",
5+
"depends": [
6+
"console",
7+
"eff",
8+
"exceptions",
9+
"foreign",
10+
"generics",
11+
"globals",
12+
"maps",
13+
"nullable"
14+
]
15+
}

src/Data/Foreign/Generic.purs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@ module Data.Foreign.Generic where
22

33
import Prelude
44

5-
import Control.Bind ((>=>))
65
import Control.Monad.Eff.Exception.Unsafe (unsafeThrow)
76
import Data.Array (zipWith, zipWithA, sortBy)
8-
import Data.Either (Either(..))
97
import Data.Foldable (find)
10-
import Data.Foreign (F, Foreign, ForeignError(..), parseJSON, toForeign, readArray,
11-
readString, isUndefined, isNull, readBoolean, readChar, readInt,
12-
readNumber)
8+
import Data.Foreign (F, Foreign, ForeignError(..), fail, parseJSON, toForeign,
9+
readArray, readString, isUndefined, isNull, readBoolean,
10+
readChar, readInt, readNumber)
1311
import Data.Foreign.Index (prop, (!))
1412
import Data.Function (on)
15-
import Data.Generic (class Generic, GenericSignature(..), GenericSpine(..), toSpine,
16-
toSignature, fromSpine)
17-
import Data.List as L
13+
import Data.Generic (class Generic, GenericSignature(..), GenericSpine(..), toSpine, toSignature, fromSpine)
1814
import Data.Maybe (Maybe(..))
1915
import Data.Nullable (toNullable)
2016
import Data.StrMap as S
@@ -98,13 +94,13 @@ readGeneric { sumEncoding
9894
x <- go (_1 unit) a
9995
y <- go (_2 unit) b
10096
pure $ SProd "Data.Tuple.Tuple" [\_ -> x, \_ -> y]
101-
_ -> Left (TypeMismatch "array of length 2" "array")
97+
_ -> fail (TypeMismatch "array of length 2" "array")
10298
go (SigProd _ alts) f =
10399
case sumEncoding of
104100
TaggedObject { tagFieldName, contentsFieldName } -> do
105101
tag <- prop tagFieldName f >>= readString
106102
case find (\alt -> alt.sigConstructor == tag) alts of
107-
Nothing -> Left (TypeMismatch ("one of " <> show (map _.sigConstructor alts)) tag)
103+
Nothing -> fail (TypeMismatch ("one of " <> show (map _.sigConstructor alts)) tag)
108104
Just { sigValues: [] } -> pure (SProd tag [])
109105
Just { sigValues: [sig] } | unwrapSingleArgumentConstructors -> do
110106
val <- prop contentsFieldName f
@@ -131,7 +127,7 @@ toForeignGeneric { sumEncoding
131127
go _ (SString s) = toForeign s
132128
go _ (SBoolean b) = toForeign b
133129
go (SigArray sig) (SArray arr) = toForeign (map (go (sig unit) <<< (_ $ unit)) arr)
134-
go (SigRecord sigs) (SRecord sps) = toForeign (S.fromList (L.fromFoldable pairs))
130+
go (SigRecord sigs) (SRecord sps) = toForeign (S.fromFoldable pairs)
135131
where
136132
pairs :: Array (Tuple String Foreign)
137133
pairs = zipWith pair (sortBy (compare `on` _.recLabel) sigs)
@@ -151,14 +147,14 @@ toForeignGeneric { sumEncoding
151147
Nothing -> unsafeThrow ("No signature for data constructor " <> tag)
152148
Just { sigValues } ->
153149
case zipWith (\sig sp -> go (sig unit) (sp unit)) sigValues sps of
154-
[] -> toForeign (S.fromList (L.singleton (Tuple tagFieldName (toForeign tag))))
150+
[] -> toForeign (S.singleton tagFieldName (toForeign tag))
155151
[f] | unwrapSingleArgumentConstructors ->
156-
toForeign (S.fromList (L.fromFoldable [ Tuple tagFieldName (toForeign tag)
157-
, Tuple contentsFieldName f
158-
]))
159-
fs -> toForeign (S.fromList (L.fromFoldable [ Tuple tagFieldName (toForeign tag)
160-
, Tuple contentsFieldName (toForeign fs)
161-
]))
152+
toForeign (S.fromFoldable [ Tuple tagFieldName (toForeign tag)
153+
, Tuple contentsFieldName f
154+
])
155+
fs -> toForeign (S.fromFoldable [ Tuple tagFieldName (toForeign tag)
156+
, Tuple contentsFieldName (toForeign fs)
157+
])
162158
go _ _ = unsafeThrow "Invalid spine for signature"
163159

164160
-- | Read a value which has a `Generic` type from a JSON String

test/Main.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
module Test.Main where
22

33
import Prelude
4-
54
import Control.Monad.Eff (Eff)
65
import Control.Monad.Eff.Console (CONSOLE, log)
6+
import Control.Monad.Except (runExcept)
77
import Data.Bifunctor (bimap)
88
import Data.Either (Either(..))
99
import Data.Foreign (F)
1010
import Data.Foreign.Generic (Options, defaultOptions, readJSONGeneric, toJSONGeneric)
1111
import Data.Generic (class Generic, gEq, gShow)
1212
import Data.Tuple (Tuple(..))
13-
import Test.Assert (assert, assert', ASSERT())
13+
import Test.Assert (assert, assert', ASSERT)
1414

1515
-- | Balanced binary leaf trees
1616
data Tree a = Leaf a | Branch (Tree (Tuple a a))
@@ -71,7 +71,7 @@ testTree
7171
testTree = do
7272
let json = writeTree tree
7373
log json
74-
case readTree json of
74+
case runExcept (readTree json) of
7575
Right tree1 -> do
7676
log (gShow tree1)
7777
assert (gEq tree tree1)
@@ -93,7 +93,7 @@ test thing = do
9393
log ""
9494
let json = toJSONGeneric defaultOptions thing
9595
log json
96-
case readJSONGeneric defaultOptions json :: F a of
96+
case runExcept (readJSONGeneric defaultOptions json :: F a) of
9797
Right thing1 -> do
9898
log ("result: " <> gShow thing1)
9999
assert (gEq thing thing1)

0 commit comments

Comments
 (0)