Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Synopsis
- class MakesValue a where
- make :: HasCallStack => a -> App Value
- noValue :: Maybe Value
- (.=) :: ToJSON a => String -> a -> Pair
- (.=?) :: ToJSON a => String -> Maybe a -> Maybe Pair
- asOptional :: HasCallStack => MakesValue a => a -> App (Maybe Value)
- asString :: HasCallStack => MakesValue a => a -> App String
- asText :: HasCallStack => MakesValue a => a -> App Text
- asStringM :: HasCallStack => MakesValue a => a -> App (Maybe String)
- asByteString :: (HasCallStack, MakesValue a) => a -> App ByteString
- asObject :: HasCallStack => MakesValue a => a -> App Object
- asInt :: HasCallStack => MakesValue a => a -> App Int
- asIntegral :: (Integral i, HasCallStack) => MakesValue a => a -> App i
- asList :: HasCallStack => MakesValue a => a -> App [Value]
- asListOf :: HasCallStack => (Value -> App b) -> MakesValue a => a -> App [b]
- asSet :: HasCallStack => MakesValue a => a -> App (Set Value)
- asSetOf :: (HasCallStack, Ord b) => (Value -> App b) -> MakesValue a => a -> App (Set b)
- asBool :: HasCallStack => MakesValue a => a -> App Bool
- (%.) :: (HasCallStack, MakesValue a) => a -> String -> App Value
- isEqual :: (MakesValue a, MakesValue b, HasCallStack) => a -> b -> App Bool
- liftP2 :: (MakesValue a, MakesValue b, HasCallStack) => (Value -> Value -> c) -> a -> b -> App c
- fieldEquals :: (MakesValue a, MakesValue b) => a -> String -> b -> App Bool
- assertFieldMissing :: (HasCallStack, MakesValue a) => a -> String -> App ()
- assertField :: (HasCallStack, MakesValue a) => a -> String -> Maybe Value -> App Value
- renameField :: String -> String -> Value -> App Value
- lookupFieldM :: (HasCallStack, MakesValue a) => a -> String -> MaybeT App Value
- lookupField :: (HasCallStack, MakesValue a) => a -> String -> App (Maybe Value)
- setField :: forall a b. (HasCallStack, MakesValue a, ToJSON b) => String -> b -> a -> App Value
- mergeField :: forall a b. (HasCallStack, MakesValue a, ToJSON b) => String -> b -> a -> App Value
- member :: (HasCallStack, MakesValue a) => String -> a -> App Bool
- modifyField :: (HasCallStack, MakesValue a, ToJSON b) => String -> (Maybe Value -> App b) -> a -> App Value
- removeField :: (HasCallStack, MakesValue a) => String -> a -> App Value
- assertFailureWithJSON :: HasCallStack => MakesValue a => a -> String -> App b
- printJSON :: MakesValue a => a -> App ()
- traceJSON :: MakesValue a => a -> App a
- prettyJSON :: MakesValue a => a -> App String
- jsonType :: Value -> String
- typeWasExpectedButGot :: String -> Value -> String
- objId :: HasCallStack => MakesValue a => a -> App String
- objQid :: HasCallStack => MakesValue a => a -> App (String, String)
- objQidObject :: HasCallStack => MakesValue a => a -> App Value
- objDomain :: (HasCallStack, MakesValue a) => a -> App String
- objSubConv :: (HasCallStack, MakesValue a) => a -> App (Value, Maybe String)
- objConvId :: (HasCallStack, MakesValue conv) => conv -> App ConvId
Documentation
class MakesValue a where Source #
All library functions should use this typeclass for all untyped value arguments wherever possible. This design choice has advantages:
No need convert value between different representations. E.g. if a function needs a user id as a string, all these input types become valid input:
- String
- Text
- Value
- App Text
- App String
- App Value
Internally the function calls asString
to convert to App String
Since (App a) are treated as first-class values values this means we can compose operations that might fail without giving up nice error messages:
callMe (response.json %. "user" & "foo.bar.baz" %.= 2)
This can fail if 1. the response is not application/json 2. has no "user" field 3. the nested update fails
Instances
MakesValue FedConn Source # | |
MakesValue CreateConv Source # | |
Defined in API.Galley | |
MakesValue PushToken Source # | |
MakesValue Domain Source # | |
MakesValue ClientIdentity Source # | |
Defined in Testlib.JSON | |
MakesValue CredentialType Source # | |
Defined in Testlib.JSON | |
MakesValue AnyFedDomain Source # | |
Defined in Testlib.VersionedFed | |
MakesValue StaticDomain Source # | |
Defined in Testlib.VersionedFed | |
ToJSON a => MakesValue a Source # | |
ToJSON a => MakesValue (App a) Source # | |
(MakesValue a, MakesValue b) => MakesValue (OneOf a b) Source # | |
MakesValue (FedDomain 0) Source # | |
MakesValue (FedDomain 1) Source # | |
asOptional :: HasCallStack => MakesValue a => a -> App (Maybe Value) Source #
Convert JSON null to Nothing.
asString :: HasCallStack => MakesValue a => a -> App String Source #
asText :: HasCallStack => MakesValue a => a -> App Text Source #
asStringM :: HasCallStack => MakesValue a => a -> App (Maybe String) Source #
asByteString :: (HasCallStack, MakesValue a) => a -> App ByteString Source #
asObject :: HasCallStack => MakesValue a => a -> App Object Source #
asInt :: HasCallStack => MakesValue a => a -> App Int Source #
asIntegral :: (Integral i, HasCallStack) => MakesValue a => a -> App i Source #
asList :: HasCallStack => MakesValue a => a -> App [Value] Source #
asListOf :: HasCallStack => (Value -> App b) -> MakesValue a => a -> App [b] Source #
asSet :: HasCallStack => MakesValue a => a -> App (Set Value) Source #
asSetOf :: (HasCallStack, Ord b) => (Value -> App b) -> MakesValue a => a -> App (Set b) Source #
asBool :: HasCallStack => MakesValue a => a -> App Bool Source #
:: (HasCallStack, MakesValue a) | |
=> a | |
-> String | A plain key, e.g. "id", or a nested key "user.profile.id" |
-> App Value |
Get a (nested) field of a JSON object
Raise an AssertionFailure if the field at the (nested) key is missing. See
lookupField
for details.
isEqual :: (MakesValue a, MakesValue b, HasCallStack) => a -> b -> App Bool Source #
liftP2 :: (MakesValue a, MakesValue b, HasCallStack) => (Value -> Value -> c) -> a -> b -> App c Source #
fieldEquals :: (MakesValue a, MakesValue b) => a -> String -> b -> App Bool Source #
assertFieldMissing :: (HasCallStack, MakesValue a) => a -> String -> App () Source #
assertField :: (HasCallStack, MakesValue a) => a -> String -> Maybe Value -> App Value Source #
:: (HasCallStack, MakesValue a) | |
=> a | |
-> String | A plain key, e.g. "id", or a nested key "user.profile.id" |
-> MaybeT App Value |
like lookupField
but wrapped in MaybeT
for convenience
:: (HasCallStack, MakesValue a) | |
=> a | |
-> String | A plain key, e.g. "id", or a nested key "user.profile.id" |
-> App (Maybe Value) |
Look up (nested) field of a JSON object
If the field key has no dots then returns Nothing if the key is missing from the object.
If the field key has dots (describes a nested lookuyp) then returns Nothing if the last component of the key field selector is missing from nested object. If any other component is missing this function raises an AssertionFailure.
Objects and arrays are supported. Array keys should be integers.
:: forall a b. (HasCallStack, MakesValue a, ToJSON b) | |
=> String | Selector, e.g. "id", "user.team.id" |
-> b | The value that should insert or replace the value at the selector |
-> a | |
-> App Value |
Update nested fields E.g. ob & "foo.bar.baz" %.= ("quux" :: String) The selector path will be created if non-existing.
mergeField :: forall a b. (HasCallStack, MakesValue a, ToJSON b) => String -> b -> a -> App Value Source #
Merges fields if the old and new are both Objects or Arrays. Otherwise new field overwrites the old completely
member :: (HasCallStack, MakesValue a) => String -> a -> App Bool Source #
modifyField :: (HasCallStack, MakesValue a, ToJSON b) => String -> (Maybe Value -> App b) -> a -> App Value Source #
Update nested fields, using the old value with a stateful action The selector path will be created if non-existing.
removeField :: (HasCallStack, MakesValue a) => String -> a -> App Value Source #
`removeField "a.b" {"a": {"b": 3}, "c": true} == {"a": {}, "c": true}`
assertFailureWithJSON :: HasCallStack => MakesValue a => a -> String -> App b Source #
printJSON :: MakesValue a => a -> App () Source #
Useful for debugging
traceJSON :: MakesValue a => a -> App a Source #
useful for debugging, same as printJSON
but returns input JSON
prettyJSON :: MakesValue a => a -> App String Source #
objId :: HasCallStack => MakesValue a => a -> App String Source #
objQid :: HasCallStack => MakesValue a => a -> App (String, String) Source #
objQidObject :: HasCallStack => MakesValue a => a -> App Value Source #
Get "qualified_id" field as {"id": _, "domain": _} object or - if already is a qualified id object - return that.
objDomain :: (HasCallStack, MakesValue a) => a -> App String Source #
objSubConv :: (HasCallStack, MakesValue a) => a -> App (Value, Maybe String) Source #
Get conversation ID and optional subconversation ID.
This accepts subconversation objects in the format:
{ "parent_qualified_id": {
"domain": "example.com",
"id": "7b6c21d1-322d-4be6-a923-85225691f398"
},
"subconv_id": "conference"
}
as well as conversation objects in the general format supported by objQid
.
Conversation objects can optionally contain a subconv_id
field. So, in
particular, a flat subconversation format, like
{ "domain": "example.com",
"id": "7b6c21d1-322d-4be6-a923-85225691f398",
"subconv_id": "conference"
}
is also supported.
objConvId :: (HasCallStack, MakesValue conv) => conv -> App ConvId Source #