Skip to content

Commit

Permalink
Add a ulid provider
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Feb 15, 2019
1 parent 8ec215b commit 5bfae80
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ a field within the JSON object.

- uuid1
- uuid4
- ulid
- null
- randomBool
- randomChar
Expand Down
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies:
- regex-tdfa
- optparse-applicative
- time
- ulid

library:
source-dirs: src
Expand Down
10 changes: 10 additions & 0 deletions src/Fake.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import Data.Time.Calendar (Day (..), showGregorian)
import Data.Time.Clock (UTCTime (..), secondsToDiffTime)
import Data.Time.Format (defaultTimeLocale, formatTime,
iso8601DateFormat, parseTimeM)
import qualified Data.ULID as ULID
import Data.ULID.Random (mkULIDRandom)
import Data.ULID.TimeStamp (getULIDTimeStamp)
import qualified Data.UUID as UUID
import qualified Data.UUID.V1 as UUID1
import qualified Data.Vector as V
Expand Down Expand Up @@ -333,6 +336,12 @@ dayAsValue :: Day -> Value
dayAsValue = String . T.pack . showGregorian


getUlid :: Fake Value
getUlid = ULID.ULID
<$> liftIO getULIDTimeStamp <*> State.state mkULIDRandom
<&> String . T.pack . show


-- | Create a value getter for an expression
--
-- >>> exec "uuid4"
Expand All @@ -347,6 +356,7 @@ eval (StringLiteral x) = pure $ String x
eval (DoubleLiteral x) = pure $ Number x
eval (FunctionCall "uuid4" []) = String . UUID.toText <$> State.state random
eval (FunctionCall "uuid1" []) = String . UUID.toText <$> liftIO uuid1
eval (FunctionCall "ulid" []) = getUlid
eval (FunctionCall "null" []) = pure Null
eval (FunctionCall "randomBool" []) = randomBool
eval (FunctionCall "randomChar" []) = randomChar
Expand Down
4 changes: 3 additions & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ resolver: lts-13.0
packages:
- .

extra-deps: []
extra-deps:
- ulid-0.2.0.0@sha256:ac415298b0272479909ca576f4444862ae942ed85c7849fa0675e3bf496be6f3
- crockford-0.2@sha256:6ec593711a20bd4b81a0fc13f3d850d2d890690941d777230fbf980c483e434a

0 comments on commit 5bfae80

Please sign in to comment.