@@ -35,11 +35,11 @@ import Prelude
35
35
36
36
import Control.Alt ((<|>))
37
37
import Control.Apply (lift2 )
38
- import Control.Monad.Except (ExceptT (..), except , runExcept , runExceptT , throwError , withExcept )
38
+ import Control.Monad.Except (ExceptT (..), except , runExcept , throwError , withExcept )
39
39
import Data.Array as Array
40
40
import Data.Array.NonEmpty (NonEmptyArray , fromArray , toArray )
41
41
import Data.Bifunctor (lmap )
42
- import Data.Either (Either (..) , hush , note )
42
+ import Data.Either (Either , hush , note )
43
43
import Data.Identity (Identity (..))
44
44
import Data.List.NonEmpty (singleton )
45
45
import Data.Maybe (Maybe (..), fromMaybe , maybe )
@@ -272,37 +272,20 @@ instance
272
272
, Row.Cons name ty from' to
273
273
) ⇒
274
274
ReadForeignFields (Cons name ty tail) from to where
275
- getFields _ obj = (compose <$> first) `exceptTApply` rest
275
+ getFields _ obj = do
276
+ compose <$> first <*> rest
276
277
where
277
- first = do
278
- value ← withExcept' (readImpl =<< readProp name obj)
279
- pure $ Builder .insert nameP value
278
+ value = withExcept' (readImpl =<< readProp name obj)
279
+ first = Builder .insert nameP <$> value
280
280
rest = getFields tailP obj
281
281
nameP = Proxy ∷ Proxy name
282
282
tailP = Proxy ∷ Proxy tail
283
283
name = reflectSymbol nameP
284
- withExcept' = withExcept <<< map $ ErrorAtProperty name
284
+ withExcept' = ( withExcept <<< map) ( ErrorAtProperty name)
285
285
286
286
readAtIdx ∷ ∀ a . ReadForeign a ⇒ Int → Foreign → F a
287
287
readAtIdx i f = withExcept (map (ErrorAtIndex i)) (readImpl f)
288
288
289
- exceptTApply ∷
290
- ∀ a b e m .
291
- Semigroup e ⇒
292
- Applicative m ⇒
293
- ExceptT e m (a → b ) →
294
- ExceptT e m a →
295
- ExceptT e m b
296
- exceptTApply fun a = ExceptT $ applyEither
297
- <$> runExceptT fun
298
- <*> runExceptT a
299
-
300
- applyEither ∷ ∀ e a b . Semigroup e ⇒ Either e (a → b ) → Either e a → Either e b
301
- applyEither (Left e) (Right _) = Left e
302
- applyEither (Left e1) (Left e2) = Left (e1 <> e2)
303
- applyEither (Right _) (Left e) = Left e
304
- applyEither (Right fun) (Right a) = Right (fun a)
305
-
306
289
instance
307
290
ReadForeignFields Nil () () where
308
291
getFields _ _ =
@@ -323,31 +306,25 @@ class
323
306
Foreign →
324
307
F (Variant row )
325
308
326
- instance
327
- ReadForeignVariant Nil trash where
328
- readVariantImpl _ _ = fail $ ForeignError
329
- " Unable to match any variant member."
330
-
331
309
instance
332
310
( IsSymbol name
333
311
, ReadForeign ty
334
312
, Row.Cons name ty trash row
335
313
, ReadForeignVariant tail row
336
314
) ⇒
337
315
ReadForeignVariant (Cons name ty tail) row where
338
- readVariantImpl _ o =
339
- do
340
- obj ∷ { type ∷ String , value ∷ Foreign } ← readImpl o
341
- if obj.type == name then do
342
- value ∷ ty ← readImpl obj .value
343
- pure $ inj namep value
344
- else
345
- (fail <<< ForeignError $ " Did not match variant tag " <> name)
346
- <|> readVariantImpl (Proxy ∷ Proxy tail ) o
316
+ readVariantImpl _ o = readVariantImpl (Proxy :: Proxy tail ) o <|> ado
317
+ value :: ty <- readProp name o >>= readImpl
318
+ in inj namep value
347
319
where
348
320
namep = Proxy ∷ Proxy name
349
321
name = reflectSymbol namep
350
322
323
+ instance
324
+ ReadForeignVariant Nil trash where
325
+ readVariantImpl _ _ = fail $ ForeignError
326
+ " Unable to match any variant member."
327
+
351
328
-- -- | A class for writing a value into JSON
352
329
-- -- | need to do this intelligently using Foreign probably, because of null and undefined whatever
353
330
class WriteForeign a where
@@ -449,7 +426,7 @@ instance
449
426
WriteForeignVariant Nil () where
450
427
writeVariantImpl _ _ =
451
428
-- a PureScript-defined variant cannot reach this path, but a JavaScript FFI one could.
452
- unsafeCrashWith " Variant was not able to be writen row WriteForeign ."
429
+ unsafeCrashWith " Attempted to write empty variant ."
453
430
454
431
instance
455
432
( IsSymbol name
@@ -466,10 +443,8 @@ instance
466
443
variant
467
444
where
468
445
namep = Proxy ∷ Proxy name
469
- writeVariant value = unsafeToForeign
470
- { type: reflectSymbol namep
471
- , value: writeImpl value
472
- }
446
+ name = reflectSymbol namep
447
+ writeVariant value = writeImpl $ Object .singleton name (writeImpl value)
473
448
474
449
instance ReadForeign a ⇒ ReadForeign (NonEmptyArray a ) where
475
450
readImpl f = do
@@ -480,4 +455,4 @@ instance ReadForeign a ⇒ ReadForeign (NonEmptyArray a) where
480
455
$ fromArray raw
481
456
482
457
instance writeForeignNEArray ∷ WriteForeign a ⇒ WriteForeign (NonEmptyArray a ) where
483
- writeImpl a = writeImpl <<< toArray $ a
458
+ writeImpl a = writeImpl <<< toArray $ a
0 commit comments