Skip to content

Commit

Permalink
Add function to join arguments/array into a string
Browse files Browse the repository at this point in the history
Example usage:

    mkjson "x=join('', join(replicate(randomInt(40, 48), randomInt(1, 9))), '.', join(replicate(2, randomInt(1, 9))))"
  • Loading branch information
mfussenegger committed Aug 13, 2024
1 parent 9bc26d1 commit 326ad04
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ You may also want to read this [introduction and sample use case][1].
- object(key, value [, ...])
- fromFile(fileName)
- fromRegex(pattern)
- join(sep, val1, [, ...])
- join(array)


## Installation
Expand Down
8 changes: 7 additions & 1 deletion src/Aeson.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE OverloadedStrings #-}

module Aeson (
asInt,
Expand Down Expand Up @@ -65,7 +66,12 @@ asArray o = Left $ "Expected an array, but received: " <> show o
--
-- >>> asText (Number 10.3)
-- Right "10.3"
--
-- >>> asText (Number 4)
-- Right "4"
asText :: Value -> Either String T.Text
asText (String t) = Right t
asText (Number n) = Right . T.pack $ show n
asText (Number n) = Right $ case (S.toBoundedInteger n :: Maybe Int) of
Nothing -> T.pack $ show n
Just int -> T.pack $ show int
asText o = Left $ "Expected a string, but received: " <> show o
40 changes: 40 additions & 0 deletions src/Fake.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import qualified Text.Regex.TDFA.ReadRegex as R
import Prelude hiding (lines, replicate)
import Data.Aeson.Key (fromText)
import Data.Aeson.Types (Pair)
import qualified Data.Either as V


-- $setup
Expand Down Expand Up @@ -154,6 +155,44 @@ oneOfArray arr = do
pure $ arr' V.! idx


-- | Join argument expressions into a string.
--
-- If there is more than one argument the first first argument is used as separator.
-- If there is a single argument and it is an array, the array is concattenated.
--
-- >>> exec "join(['Hello', 'World'])"
-- String "HelloWorld"
--
-- >>> exec "join('-', 'Hello', 'World')"
-- String "Hello-World"
--
-- >>> exec "join('foo')"
-- String "foo"
--
-- >>> exec "join([1, 2, 3])"
-- String "123"
--
-- >>> exec "join()"
-- String ""
joinArgs :: [Expr] -> Fake Value
joinArgs [] = pure $ String ""
joinArgs [arg] = do
val <- eval arg
pure $ case val of
(String _) -> val
(Array arr') -> do
let strings = V.rights . V.toList $ fmap A.asText arr'
text = T.intercalate "" strings
String text
_ -> error $ "Expected a string or array, but received: " <> show val
joinArgs (x:xs) = do
sep <- Except.liftEither . A.asText =<< eval x
values <- mapM eval xs
let strings = V.rights $ fmap A.asText values
text = T.intercalate sep strings
pure $ String text


-- | Select one random argument
--
-- >>> exec "oneOf(37, 42, 21)"
Expand Down Expand Up @@ -443,6 +482,7 @@ eval (Fn "randomDateTime" [lower, upper]) = do
upper' <- rightToMaybe . A.asText <$> eval upper
randomDateTime lower' upper'
eval (Fn "array" args) = Array . V.fromList <$> mapM eval args
eval (Fn "join" args) = joinArgs args
eval (Fn "oneOf" [arg]) = oneOfArray arg
eval (Fn "oneOf" args) = oneOfArgs args
eval (Fn "replicate" [num, expr]) = replicate num expr
Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
resolver: lts-22.13
resolver: lts-22.29

packages:
- .
Expand Down
8 changes: 4 additions & 4 deletions stack.yaml.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
packages: []
snapshots:
- completed:
sha256: 6f0bea3ba5b07360f25bc886e8cff8d847767557a492a6f7f6dcb06e3cc79ee9
size: 712905
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/13.yaml
original: lts-22.13
sha256: 98fe98dae6f42f9b4405e5467f62f57df2896c57b742a09772688c900c722510
size: 719579
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/29.yaml
original: lts-22.29

0 comments on commit 326ad04

Please sign in to comment.