From 9e684170a17696f7273225e9f1bfb73b26309024 Mon Sep 17 00:00:00 2001 From: Nikita Volkov Date: Tue, 10 Nov 2015 17:53:14 +0300 Subject: [PATCH] Enum --- hasql.cabal | 2 +- library/Hasql/Deserialization.hs | 8 ++++++++ library/Hasql/Serialization.hs | 10 ++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/hasql.cabal b/hasql.cabal index 94e69ed..423e3ec 100644 --- a/hasql.cabal +++ b/hasql.cabal @@ -68,7 +68,7 @@ library -- parsing: attoparsec >= 0.10 && < 0.14, -- database: - postgresql-binary >= 0.7.2 && < 0.8, + postgresql-binary >= 0.7.3 && < 0.8, postgresql-libpq == 0.9.*, -- data: dlist >= 0.7 && < 0.8, diff --git a/library/Hasql/Deserialization.hs b/library/Hasql/Deserialization.hs index 4be5c64..9de2b2d 100644 --- a/library/Hasql/Deserialization.hs +++ b/library/Hasql/Deserialization.hs @@ -45,6 +45,7 @@ module Hasql.Deserialization array, composite, hstore, + enum, -- * Array Array, arrayDimension, @@ -525,6 +526,13 @@ hstore :: (forall m. Monad m => Int -> m (Text, Maybe Text) -> m a) -> Value a hstore replicateM = Value (Value.decoder (const (Decoder.hstore replicateM Decoder.text_strict Decoder.text_strict))) +-- | +-- Given a partial mapping from text to value, +-- produces a deserializer of that value. +enum :: (Text -> Maybe a) -> Value a +enum mapping = + Value (Value.decoder (const (Decoder.enum mapping))) + -- ** Instances ------------------------- diff --git a/library/Hasql/Serialization.hs b/library/Hasql/Serialization.hs index 48bab63..b61cfcc 100644 --- a/library/Hasql/Serialization.hs +++ b/library/Hasql/Serialization.hs @@ -25,6 +25,7 @@ module Hasql.Serialization uuid, json, array, + enum, -- * Array Array, arrayValue, @@ -220,6 +221,15 @@ array (Array imp) = Array.run imp & \(arrayOID, encoder') -> Value (Value.Value arrayOID arrayOID encoder') +-- | +-- Given a function, +-- which maps the value into the textual enum label from the DB side, +-- produces a serializer of that value. +{-# INLINABLE enum #-} +enum :: (a -> Text) -> Value a +enum mapping = + Value (Value.unsafePTI PTI.text (const (Encoder.enum mapping))) + -- ** Instances -------------------------