mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 17:31:56 +03:00
49f2e0f4a3
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4387 GitOrigin-RevId: 890d07ebda838a4c6dc4221908bacba2ee75a0a2
39 lines
1.3 KiB
Haskell
39 lines
1.3 KiB
Haskell
-- | Utility functions related to yaml
|
|
module Harness.Yaml
|
|
( combinationsObject,
|
|
fromObject,
|
|
combinationsObjectUsingValue,
|
|
)
|
|
where
|
|
|
|
import Data.Aeson
|
|
( Object,
|
|
Value (..),
|
|
)
|
|
import Data.List (permutations)
|
|
import Data.Vector qualified as V
|
|
import Hasura.Prelude
|
|
|
|
fromObject :: Value -> Object
|
|
fromObject (Object x) = x
|
|
fromObject v = error $ "fromObject: Expected object, received" <> show v
|
|
|
|
-- | Compute all variations of an object and construct a list of
|
|
-- 'Value' based on the higher order function that is passed to it. A
|
|
-- single variation of 'Object' is constructed as an 'Array' before
|
|
-- it's transformed by the passed function.
|
|
--
|
|
-- Typical usecase of this function is to use it with
|
|
-- 'shouldReturnOneOfYaml' function.
|
|
combinationsObject :: (Value -> Value) -> [Object] -> [Value]
|
|
combinationsObject fn variants =
|
|
let toArray :: [Value]
|
|
toArray = map ((Array . V.fromList) . (map Object)) (permutations variants)
|
|
in map fn toArray
|
|
|
|
-- | Same as 'combinationsObject' but the second parameter is a list
|
|
-- of 'Value`. We assume that 'Value' internally has only 'Object', if
|
|
-- not it will throw exception.
|
|
combinationsObjectUsingValue :: (Value -> Value) -> [Value] -> [Value]
|
|
combinationsObjectUsingValue fn variants = combinationsObject fn (map fromObject variants)
|