Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit b794b95

Browse files
committed
Make remaining functions in Aeson pure
1 parent 6b67df4 commit b794b95

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

src/Aeson.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ asDouble (String s) =
5353
asDouble o = Left $ "Expected a double, but received: " <> show o
5454

5555

56-
asArray :: Value -> V.Vector Value
57-
asArray (Array x) = x
58-
asArray o = error $ "Expected an array, but received: " <> show o
56+
asArray :: Value -> Either String (V.Vector Value)
57+
asArray (Array x) = Right x
58+
asArray o = Left $ "Expected an array, but received: " <> show o
5959

6060

6161
-- | Try to extract a Text from Value
6262
--
6363
-- >>> asText (String "foo")
64-
-- "foo"
64+
-- Right "foo"
6565
--
6666
-- >>> asText (Number 10.3)
67-
-- "10.3"
68-
asText :: Value -> T.Text
69-
asText (String t) = t
70-
asText (Number n) = T.pack $ show n
71-
asText o = error $ "Expected a string, but received: " <> show o
67+
-- Right "10.3"
68+
asText :: Value -> Either String T.Text
69+
asText (String t) = Right t
70+
asText (Number n) = Right . T.pack $ show n
71+
asText o = Left $ "Expected a string, but received: " <> show o

src/Fake.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Control.Monad.State.Strict (StateT)
2020
import qualified Control.Monad.State.Strict as State
2121
import Data.Aeson (Value (..), object)
2222
import qualified Data.ByteString.Char8 as BS
23+
import Data.Functor ((<&>))
2324
import qualified Data.HashMap.Strict as M
2425
import Data.Maybe (fromMaybe)
2526
import qualified Data.Scientific as S
@@ -123,7 +124,7 @@ randomBool = Bool <$> State.state random
123124
-- Number 21.0
124125
oneOfArray :: Expr -> Fake Value
125126
oneOfArray 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

242243
fromFile :: Expr -> Fake Value
243244
fromFile 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
289290
eval (FunctionCall "replicate" [num, expr]) = replicate num expr
290291
eval (FunctionCall "object" args) = objectFromArgs args
291292
eval (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
293299
eval (FunctionCall name _) = error $ "No random generator for " <> T.unpack name

0 commit comments

Comments
 (0)