License | BSD3 |
---|---|
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
Param serialization and deserialization. ToParam
and EncodeParam
are responsible for serialization part.
EncodeParam
converts the value into a wire format. ToParam
is responsible for creating (nested) key value pairs, which can be then used to deserialize to original type. For example
encodeParam 5 == "5" data Foo = Foo { foo :: Int } deriving (Show, Eq, Generic) data Bar = Bar { bar :: Foo } deriving (Show, Eq, Generic) instance ToParam Foo 'FormParam instance ToParam Bar 'FormParam toParam (Proxy :: Proxy 'FormParam) "" (Bar (Foo 5)) == [("bar.foo","5")]
Deserialization works analogously, FromParam
and DecodeParam
are counterparts to ToParam
and EncodeParam
respectively. Generic instances are provided for all of them. This means that the user only need to derive Generic in their type, and provide instance with an empty body. Note that for headers FromHeader
and ToHeader
is being used in place of FromParam
and ToParam
. Nesting is not supported for headers.
- class ToParam parK a where
- toParam :: Proxy (parK :: ParamK) -> ByteString -> a -> [SerializedData parK]
- class EncodeParam t where
- encodeParam :: t -> ByteString
- class ToHeader a where
- toHeader :: a -> [Header]
- type family SerializedData par
- toQueryParam :: ToParam QueryParam a => a -> Query
- toFormParam :: ToParam FormParam a => a -> [(ByteString, ByteString)]
- toFileParam :: ToParam FileParam a => a -> [(ByteString, FileInfo FilePath)]
- toPathParam :: ToParam PathParam a => a -> [ByteString]
- toCookie :: ToParam Cookie a => a -> [(ByteString, CookieInfo ByteString)]
- toNonNestedParam :: ToParam parK (NonNested a) => Proxy (parK :: ParamK) -> ByteString -> a -> [SerializedData parK]
- class FromParam parK a where
- fromParam :: Proxy (parK :: ParamK) -> ByteString -> Trie (DeSerializedData parK) -> Validation [ParamErr] a
- class DecodeParam t where
- decodeParam :: ByteString -> Maybe t
- class FromHeader a where
- fromHeader :: [Header] -> Validation [ParamErr] a
- newtype Validation e a = Validation {
- getValidation :: Either e a
- data ParamErr
- = NotFound ByteString
- | ParseErr ByteString Text
- class ParamErrToApiErr apiErr where
- type family DeSerializedData par
- fromQueryParam :: FromParam QueryParam a => Query -> Validation [ParamErr] a
- fromFormParam :: FromParam FormParam a => [(ByteString, ByteString)] -> Validation [ParamErr] a
- fromFileParam :: FromParam FileParam a => [(ByteString, FileInfo FilePath)] -> Validation [ParamErr] a
- fromCookie :: FromParam Cookie a => [(ByteString, ByteString)] -> Validation [ParamErr] a
- lookupParam :: Proxy (parK :: ParamK) -> ByteString -> Trie (DeSerializedData parK) -> Maybe (DeSerializedData parK)
- fromNonNestedParam :: FromParam parK (NonNested a) => Proxy (parK :: ParamK) -> ByteString -> Trie (DeSerializedData parK) -> Validation [ParamErr] a
- data CookieInfo a = CookieInfo {}
- newtype JsonOf a = JsonOf {
- getValue :: a
- newtype OptValue a = OptValue {}
- newtype FileInfo = FileInfo {}
- newtype NonNested a = NonNested {
- getNonNestedParam :: a
- data ParamK
- data ParamSettings
- fieldModifier :: ParamSettings -> ByteString -> ByteString
- filePath :: FileInfo -> FilePath
- nest :: ByteString -> ByteString -> ByteString
- defaultParamSettings :: ParamSettings
- genericToQueryParam :: (Generic a, GToParam (Rep a) QueryParam) => ParamSettings -> ByteString -> a -> [QueryItem]
- genericFromQueryParam :: (Generic a, GFromParam (Rep a) QueryParam) => ParamSettings -> ByteString -> Trie (Maybe ByteString) -> Validation [ParamErr] a
- genericToFormParam :: (Generic a, GToParam (Rep a) FormParam) => ParamSettings -> ByteString -> a -> [(ByteString, ByteString)]
- genericFromFormParam :: (Generic a, GFromParam (Rep a) FormParam) => ParamSettings -> ByteString -> Trie ByteString -> Validation [ParamErr] a
- genericToFileParam :: (Generic a, GToParam (Rep a) FileParam) => ParamSettings -> ByteString -> a -> [(ByteString, FileInfo FilePath)]
- genericFromFileParam :: (Generic a, GFromParam (Rep a) FileParam) => ParamSettings -> ByteString -> Trie (FileInfo FilePath) -> Validation [ParamErr] a
- genericToPathParam :: (Generic a, GToParam (Rep a) PathParam) => ParamSettings -> ByteString -> a -> [ByteString]
- genericFromPathParam :: (Generic a, GFromParam (Rep a) PathParam) => ParamSettings -> ByteString -> Trie ByteString -> Validation [ParamErr] a
- genericToCookie :: (Generic a, GToParam (Rep a) Cookie) => ParamSettings -> ByteString -> a -> [(ByteString, CookieInfo ByteString)]
- genericFromCookie :: (Generic a, GFromParam (Rep a) Cookie) => ParamSettings -> ByteString -> Trie ByteString -> Validation [ParamErr] a
Serialization
class ToParam parK a where Source
Serialize a type to a given type of kind ParamK
.
Nothing
toParam :: Proxy (parK :: ParamK) -> ByteString -> a -> [SerializedData parK] Source
class EncodeParam t where Source
Serialize a type to ByteString
.
Nothing
encodeParam :: t -> ByteString Source
Serialize a type to the header params
Nothing
type family SerializedData par Source
Define result of serialization of a type of kind ParamK
.
SerializedData QueryParam = QueryItem | |
SerializedData FormParam = (ByteString, ByteString) | |
SerializedData FileParam = (ByteString, FileInfo FilePath) | |
SerializedData PathParam = ByteString | |
SerializedData Cookie = (ByteString, CookieInfo ByteString) |
toQueryParam :: ToParam QueryParam a => a -> Query Source
Serialize a type into query params.
toFormParam :: ToParam FormParam a => a -> [(ByteString, ByteString)] Source
Serialize a type into form params.
toFileParam :: ToParam FileParam a => a -> [(ByteString, FileInfo FilePath)] Source
Serialize a type into file params.
toPathParam :: ToParam PathParam a => a -> [ByteString] Source
Serialize a type into path params.
toCookie :: ToParam Cookie a => a -> [(ByteString, CookieInfo ByteString)] Source
Serialize a type into cookie.
toNonNestedParam :: ToParam parK (NonNested a) => Proxy (parK :: ParamK) -> ByteString -> a -> [SerializedData parK] Source
Serialize a type without nesting.
Deserialization
class FromParam parK a where Source
(Try to) Deserialize a type from a given type of kind ParamK
.
Nothing
fromParam :: Proxy (parK :: ParamK) -> ByteString -> Trie (DeSerializedData parK) -> Validation [ParamErr] a Source
class DecodeParam t where Source
(Try to) Deserialize a type from ByteString
.
Nothing
decodeParam :: ByteString -> Maybe t Source
class FromHeader a where Source
(Try to) Deserialize a type from the header params
Nothing
fromHeader :: [Header] -> Validation [ParamErr] a Source
FromHeader () Source |
newtype Validation e a Source
Datatype representing the parsed result of params.
Validation | |
|
Functor (Validation e) Source | |
Monoid e => Applicative (Validation e) Source | |
(Eq e, Eq a) => Eq (Validation e a) Source | |
(Show e, Show a) => Show (Validation e a) Source |
Errors that occured during deserialization.
NotFound ByteString | The key was not found. |
ParseErr ByteString Text | A parse error occured while deserializing the type. |
class ParamErrToApiErr apiErr where Source
Convert the ParamErr
that occured during deserialization into ApiErr
type which can then be put in Response
.
ParamErrToApiErr () Source | |
ParamErrToApiErr Text Source | |
ParamErrToApiErr Value Source |
type family DeSerializedData par Source
Define result of deserialization of a type of kind ParamK
.
fromQueryParam :: FromParam QueryParam a => Query -> Validation [ParamErr] a Source
(Try to) Deserialize a type from query params.
fromFormParam :: FromParam FormParam a => [(ByteString, ByteString)] -> Validation [ParamErr] a Source
(Try to) Deserialize a type from form params.
fromFileParam :: FromParam FileParam a => [(ByteString, FileInfo FilePath)] -> Validation [ParamErr] a Source
(Try to) Deserialize a type from file params.
fromCookie :: FromParam Cookie a => [(ByteString, ByteString)] -> Validation [ParamErr] a Source
(Try to) Deserialize a type from cookie.
lookupParam :: Proxy (parK :: ParamK) -> ByteString -> Trie (DeSerializedData parK) -> Maybe (DeSerializedData parK) Source
Lookup a value from the Trie
using the given key.
fromNonNestedParam :: FromParam parK (NonNested a) => Proxy (parK :: ParamK) -> ByteString -> Trie (DeSerializedData parK) -> Validation [ParamErr] a Source
(Try to) Deserialize a type without nesting.
Wrappers
data CookieInfo a Source
CookieInfo | |
|
Serializing JsonOf
will produce a JSON representation of the value contained within. This is useful if params has to be sent as JSON.
ToJSON a => ToParam QueryParam (JsonOf a) Source | |
ToJSON a => ToParam FormParam (JsonOf a) Source | |
ToJSON a => ToParam PathParam (JsonOf a) Source | |
ToJSON a => ToParam Cookie (JsonOf a) Source | |
Eq a => Eq (JsonOf a) Source | |
Ord a => Ord (JsonOf a) Source | |
Read a => Read (JsonOf a) Source | |
Show a => Show (JsonOf a) Source | |
ToJSON a => ToJSON (JsonOf a) Source | |
FromJSON a => FromJSON (JsonOf a) Source | |
FromJSON a => DecodeParam (JsonOf a) Source | |
ToJSON a => EncodeParam (JsonOf a) Source |
Use this type if a key is required but the value is optional.
DecodeParam a => FromParam QueryParam (OptValue a) Source | |
DecodeParam a => FromParam FormParam (OptValue a) Source | |
DecodeParam a => FromParam Cookie (OptValue a) Source | |
EncodeParam a => ToParam QueryParam (OptValue a) Source | |
EncodeParam a => ToParam FormParam (OptValue a) Source | |
EncodeParam a => ToParam Cookie (OptValue a) Source | |
Eq a => Eq (OptValue a) Source | |
Ord a => Ord (OptValue a) Source | |
Read a => Read (OptValue a) Source | |
Show a => Show (OptValue a) Source |
A type for holding a file.
Use this type if for serialization / deserialization nesting is not required. The type contained within most likely requires EncodeParam
/ DecodeParam
.
(DecodeParam a, Typeable * a) => FromParam QueryParam (NonNested a) Source | |
(DecodeParam a, Typeable * a) => FromParam FormParam (NonNested a) Source | |
(DecodeParam a, Typeable * a) => FromParam Cookie (NonNested a) Source | |
EncodeParam a => ToParam QueryParam (NonNested a) Source | |
EncodeParam a => ToParam FormParam (NonNested a) Source | |
EncodeParam a => ToParam Cookie (NonNested a) Source | |
Eq a => Eq (NonNested a) Source | |
Read a => Read (NonNested a) Source | |
Show a => Show (NonNested a) Source |
Helpers
(Kind) Describes the various types of Param.
data ParamSettings Source
fieldModifier :: ParamSettings -> ByteString -> ByteString Source
nest :: ByteString -> ByteString -> ByteString Source
Nest the key with a prefix.
nest "pfx" "key" == "pfx.key" nest "" "key" == "key"
Generic (De)Serialization fn
genericToQueryParam :: (Generic a, GToParam (Rep a) QueryParam) => ParamSettings -> ByteString -> a -> [QueryItem] Source
genericFromQueryParam :: (Generic a, GFromParam (Rep a) QueryParam) => ParamSettings -> ByteString -> Trie (Maybe ByteString) -> Validation [ParamErr] a Source
genericToFormParam :: (Generic a, GToParam (Rep a) FormParam) => ParamSettings -> ByteString -> a -> [(ByteString, ByteString)] Source
genericFromFormParam :: (Generic a, GFromParam (Rep a) FormParam) => ParamSettings -> ByteString -> Trie ByteString -> Validation [ParamErr] a Source
genericToFileParam :: (Generic a, GToParam (Rep a) FileParam) => ParamSettings -> ByteString -> a -> [(ByteString, FileInfo FilePath)] Source
genericFromFileParam :: (Generic a, GFromParam (Rep a) FileParam) => ParamSettings -> ByteString -> Trie (FileInfo FilePath) -> Validation [ParamErr] a Source
genericToPathParam :: (Generic a, GToParam (Rep a) PathParam) => ParamSettings -> ByteString -> a -> [ByteString] Source
genericFromPathParam :: (Generic a, GFromParam (Rep a) PathParam) => ParamSettings -> ByteString -> Trie ByteString -> Validation [ParamErr] a Source
genericToCookie :: (Generic a, GToParam (Rep a) Cookie) => ParamSettings -> ByteString -> a -> [(ByteString, CookieInfo ByteString)] Source
genericFromCookie :: (Generic a, GFromParam (Rep a) Cookie) => ParamSettings -> ByteString -> Trie ByteString -> Validation [ParamErr] a Source