@@ -20,6 +20,7 @@ import Control.Monad.State.Strict (StateT)
2020import qualified Control.Monad.State.Strict as State
2121import Data.Aeson (Value (.. ), object )
2222import qualified Data.ByteString.Char8 as BS
23+ import Data.Functor ((<&>) )
2324import qualified Data.HashMap.Strict as M
2425import Data.Maybe (fromMaybe )
2526import qualified Data.Scientific as S
@@ -123,7 +124,7 @@ randomBool = Bool <$> State.state random
123124-- Number 21.0
124125oneOfArray :: Expr -> Fake Value
125126oneOfArray arr = do
126- arr' <- A. asArray <$> eval arr
127+ arr' <- Except. liftEither =<< A. asArray <$> eval arr
127128 idx <- State. state $ randomR (0 , length arr' - 1 )
128129 pure $ arr' V. ! idx
129130
@@ -168,7 +169,7 @@ objectFromArgs args = do
168169 mkPairs [_] = error " Arguments to object must be a multiple of 2 (key + value pairs)"
169170 mkPairs (x : y : rest) = (x, y) : mkPairs rest
170171 pairs <- forM keyValuePairs (\ (key, val) -> do
171- key' <- A. asText <$> key
172+ key' <- Except. liftEither =<< A. asText <$> key
172173 val' <- val
173174 pure (key', val'))
174175 pure $ object pairs
@@ -241,7 +242,7 @@ fromRegex input =
241242
242243fromFile :: Expr -> Fake Value
243244fromFile fileName = do
244- fileName' <- A. asText <$> eval fileName
245+ fileName' <- Except. liftEither =<< A. asText <$> eval fileName
245246 e@ Env {.. } <- State. get
246247 case M. lookup fileName' envFileCache of
247248 (Just lines ) -> pure $ Array lines
@@ -289,5 +290,10 @@ eval (FunctionCall "oneOf" args) = oneOfArgs args
289290eval (FunctionCall " replicate" [num, expr]) = replicate num expr
290291eval (FunctionCall " object" args) = objectFromArgs args
291292eval (FunctionCall " fromFile" [fileName]) = fromFile fileName
292- eval (FunctionCall " fromRegex" [pattern ]) = String <$> (eval pattern >>= fromRegex . A. asText)
293+ eval (FunctionCall " fromRegex" [pattern ]) =
294+ eval pattern
295+ <&> A. asText
296+ >>= Except. liftEither
297+ >>= fromRegex
298+ <&> String
293299eval (FunctionCall name _) = error $ " No random generator for " <> T. unpack name
0 commit comments