@@ -2,19 +2,15 @@ module Data.Foreign.Generic where
22
33import Prelude
44
5- import Control.Bind ((>=>))
65import Control.Monad.Eff.Exception.Unsafe (unsafeThrow )
76import Data.Array (zipWith , zipWithA , sortBy )
8- import Data.Either (Either (..))
97import 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 )
1311import Data.Foreign.Index (prop , (!))
1412import 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 )
1814import Data.Maybe (Maybe (..))
1915import Data.Nullable (toNullable )
2016import 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
0 commit comments