2021-11-10 14:12:01 +03:00
|
|
|
module Hasura.Generator.Common
|
|
|
|
( genHashMap,
|
|
|
|
genNonEmptyText,
|
|
|
|
genArbitraryUnicodeText,
|
|
|
|
genArbitraryAlphaNumText,
|
|
|
|
genFieldName,
|
|
|
|
genGName,
|
|
|
|
genDescription,
|
2022-04-06 01:08:43 +03:00
|
|
|
defaultRange,
|
2021-11-10 14:12:01 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.HashMap.Strict qualified as HM
|
|
|
|
import Data.Text.NonEmpty (NonEmptyText, mkNonEmptyText)
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types.Common (FieldName (FieldName))
|
|
|
|
import Hedgehog
|
2022-04-06 01:08:43 +03:00
|
|
|
import Hedgehog.Gen qualified as Gen
|
|
|
|
import Hedgehog.Range qualified as Range
|
2021-11-10 14:12:01 +03:00
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
|
|
|
|
genHashMap ::
|
|
|
|
MonadGen m =>
|
|
|
|
Eq a =>
|
|
|
|
Hashable a =>
|
|
|
|
m a ->
|
|
|
|
m b ->
|
|
|
|
Range Int ->
|
|
|
|
m (HashMap a b)
|
2022-04-06 01:08:43 +03:00
|
|
|
genHashMap genA genB range = fmap HM.fromList . Gen.list range $ (,) <$> genA <*> genB
|
2021-11-10 14:12:01 +03:00
|
|
|
|
|
|
|
genNonEmptyText :: MonadGen m => Range Int -> m NonEmptyText
|
2022-04-06 01:08:43 +03:00
|
|
|
genNonEmptyText range = mkNonEmptyText `Gen.mapMaybeT` genArbitraryUnicodeText range
|
2021-11-10 14:12:01 +03:00
|
|
|
|
|
|
|
genArbitraryUnicodeText :: MonadGen m => Range Int -> m Text
|
2022-04-06 01:08:43 +03:00
|
|
|
genArbitraryUnicodeText range = Gen.text range Gen.unicode
|
2021-11-10 14:12:01 +03:00
|
|
|
|
|
|
|
genArbitraryAlphaNumText :: MonadGen m => Range Int -> m Text
|
2022-04-06 01:08:43 +03:00
|
|
|
genArbitraryAlphaNumText range = Gen.text range Gen.alphaNum
|
2021-11-10 14:12:01 +03:00
|
|
|
|
|
|
|
genFieldName :: MonadGen m => Range Int -> m FieldName
|
|
|
|
genFieldName range = FieldName <$> genArbitraryUnicodeText range
|
|
|
|
|
|
|
|
genGName :: MonadGen m => Range Int -> m G.Name
|
2022-04-06 01:08:43 +03:00
|
|
|
genGName range = G.mkName `Gen.mapMaybeT` genArbitraryAlphaNumText range
|
2021-11-10 14:12:01 +03:00
|
|
|
|
|
|
|
genDescription :: MonadGen m => Range Int -> m G.Description
|
|
|
|
genDescription range = G.Description <$> genArbitraryUnicodeText range
|
2022-04-06 01:08:43 +03:00
|
|
|
|
|
|
|
-- | A reasonable range size to generate data on dev machines without
|
|
|
|
-- blowing up.
|
|
|
|
defaultRange :: Integral a => Range a
|
|
|
|
defaultRange = Range.linear 0 8
|