{-# LANGUAGE DeriveDataTypeable  #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Database.CQL.Protocol.Response
    ( Response            (..)
    , warnings
    , traceId
    , unpack

      -- ** Ready
    , Ready               (..)
    , decodeReady

      -- ** Authenticate
    , Authenticate        (..)
    , AuthChallenge       (..)
    , AuthSuccess         (..)
    , decodeAuthenticate
    , decodeAuthChallenge
    , decodeAuthSuccess

      -- ** Result
    , Result              (..)
    , MetaData            (..)
    , ColumnSpec          (..)
    , decodeResult
    , decodeMetaData

      -- ** Supported
    , Supported           (..)
    , decodeSupported

      -- ** Event
    , Event               (..)
    , TopologyChange      (..)
    , SchemaChange        (..)
    , StatusChange        (..)
    , Change              (..)
    , decodeSchemaChange
    , decodeChange
    , decodeEvent
    , decodeTopologyChange
    , decodeStatusChange

      -- ** Error
    , Error               (..)
    , WriteType           (..)
    , decodeError
    , decodeWriteType
    ) where

import Control.Applicative
import Control.Exception (Exception)
import Control.Monad
import Data.Bits
import Data.ByteString (ByteString)
import Data.Int
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Data.Serialize hiding (Result)
import Data.Typeable
import Data.UUID (UUID)
import Data.Word
import Database.CQL.Protocol.Tuple
import Database.CQL.Protocol.Codec
import Database.CQL.Protocol.Types
import Database.CQL.Protocol.Header
import Network.Socket (SockAddr)
import Prelude

import qualified Data.ByteString.Lazy as LB

------------------------------------------------------------------------------
-- Response

-- | The type corresponding to the protocol response frame.
--
-- The type parameter 'k' denotes the kind of response. It is present to allow
-- distinguishing read operations from write operations. Use 'R' for read,
-- 'W' for write and 'S' for schema related operations.
--
-- 'a' represents the argument type and 'b' the return type of this
-- response.
data Response k a b
    = RsError         (Maybe UUID) [Text] !Error
    | RsReady         (Maybe UUID) [Text] !Ready
    | RsAuthenticate  (Maybe UUID) [Text] !Authenticate
    | RsAuthChallenge (Maybe UUID) [Text] !AuthChallenge
    | RsAuthSuccess   (Maybe UUID) [Text] !AuthSuccess
    | RsSupported     (Maybe UUID) [Text] !Supported
    | RsResult        (Maybe UUID) [Text] !(Result k a b)
    | RsEvent         (Maybe UUID) [Text] !Event
    deriving Int -> Response k a b -> ShowS
[Response k a b] -> ShowS
Response k a b -> [Char]
(Int -> Response k a b -> ShowS)
-> (Response k a b -> [Char])
-> ([Response k a b] -> ShowS)
-> Show (Response k a b)
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
forall k a b. Show b => Int -> Response k a b -> ShowS
forall k a b. Show b => [Response k a b] -> ShowS
forall k a b. Show b => Response k a b -> [Char]
$cshowsPrec :: forall k a b. Show b => Int -> Response k a b -> ShowS
showsPrec :: Int -> Response k a b -> ShowS
$cshow :: forall k a b. Show b => Response k a b -> [Char]
show :: Response k a b -> [Char]
$cshowList :: forall k a b. Show b => [Response k a b] -> ShowS
showList :: [Response k a b] -> ShowS
Show

-- | Get server warnings from response if any.
warnings :: Response k a b -> [Text]
warnings :: forall k a b. Response k a b -> [Text]
warnings (RsError         Maybe UUID
_ [Text]
w Error
_) = [Text]
w
warnings (RsReady         Maybe UUID
_ [Text]
w Ready
_) = [Text]
w
warnings (RsAuthenticate  Maybe UUID
_ [Text]
w Authenticate
_) = [Text]
w
warnings (RsAuthChallenge Maybe UUID
_ [Text]
w AuthChallenge
_) = [Text]
w
warnings (RsAuthSuccess   Maybe UUID
_ [Text]
w AuthSuccess
_) = [Text]
w
warnings (RsSupported     Maybe UUID
_ [Text]
w Supported
_) = [Text]
w
warnings (RsResult        Maybe UUID
_ [Text]
w Result k a b
_) = [Text]
w
warnings (RsEvent         Maybe UUID
_ [Text]
w Event
_) = [Text]
w

-- | Get server trace ID from response if any.
traceId :: Response k a b -> Maybe UUID
traceId :: forall k a b. Response k a b -> Maybe UUID
traceId (RsError         Maybe UUID
x [Text]
_ Error
_) = Maybe UUID
x
traceId (RsReady         Maybe UUID
x [Text]
_ Ready
_) = Maybe UUID
x
traceId (RsAuthenticate  Maybe UUID
x [Text]
_ Authenticate
_) = Maybe UUID
x
traceId (RsAuthChallenge Maybe UUID
x [Text]
_ AuthChallenge
_) = Maybe UUID
x
traceId (RsAuthSuccess   Maybe UUID
x [Text]
_ AuthSuccess
_) = Maybe UUID
x
traceId (RsSupported     Maybe UUID
x [Text]
_ Supported
_) = Maybe UUID
x
traceId (RsResult        Maybe UUID
x [Text]
_ Result k a b
_) = Maybe UUID
x
traceId (RsEvent         Maybe UUID
x [Text]
_ Event
_) = Maybe UUID
x

-- | Deserialise a 'Response' from the given 'ByteString'.
unpack :: (Tuple a, Tuple b)
       => Compression
       -> Header
       -> LB.ByteString
       -> Either String (Response k a b)
unpack :: forall a b k.
(Tuple a, Tuple b) =>
Compression
-> Header -> ByteString -> Either [Char] (Response k a b)
unpack Compression
c Header
h ByteString
b = do
    let f :: Flags
f = Header -> Flags
flags Header
h
    let v :: Version
v = Header -> Version
version Header
h
    ByteString
x <- if Flags
compress Flags -> Flags -> Bool
`isSet` Flags
f then Compression -> ByteString -> Either [Char] ByteString
deflate Compression
c ByteString
b else ByteString -> Either [Char] ByteString
forall a. a -> Either [Char] a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
b
    (Get (Response k a b)
 -> ByteString -> Either [Char] (Response k a b))
-> ByteString
-> Get (Response k a b)
-> Either [Char] (Response k a b)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Get (Response k a b)
-> ByteString -> Either [Char] (Response k a b)
forall a. Get a -> ByteString -> Either [Char] a
runGetLazy ByteString
x (Get (Response k a b) -> Either [Char] (Response k a b))
-> Get (Response k a b) -> Either [Char] (Response k a b)
forall a b. (a -> b) -> a -> b
$ do
        Maybe UUID
t <- if Flags
tracing Flags -> Flags -> Bool
`isSet` Flags
f then UUID -> Maybe UUID
forall a. a -> Maybe a
Just (UUID -> Maybe UUID) -> Get UUID -> Get (Maybe UUID)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get UUID
decodeUUID else Maybe UUID -> Get (Maybe UUID)
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe UUID
forall a. Maybe a
Nothing
        [Text]
w <- if Flags
warning Flags -> Flags -> Bool
`isSet` Flags
f then Get [Text]
decodeList else [Text] -> Get [Text]
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return []
        Version -> Maybe UUID -> [Text] -> OpCode -> Get (Response k a b)
forall {a} {b} {k}.
(Tuple a, Tuple b) =>
Version -> Maybe UUID -> [Text] -> OpCode -> Get (Response k a b)
message Version
v Maybe UUID
t [Text]
w (Header -> OpCode
opCode Header
h)
  where
    message :: Version -> Maybe UUID -> [Text] -> OpCode -> Get (Response k a b)
message Version
_ Maybe UUID
t [Text]
w OpCode
OcError         = Maybe UUID -> [Text] -> Error -> Response k a b
forall k a b. Maybe UUID -> [Text] -> Error -> Response k a b
RsError         Maybe UUID
t [Text]
w (Error -> Response k a b) -> Get Error -> Get (Response k a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Error
decodeError
    message Version
_ Maybe UUID
t [Text]
w OpCode
OcReady         = Maybe UUID -> [Text] -> Ready -> Response k a b
forall k a b. Maybe UUID -> [Text] -> Ready -> Response k a b
RsReady         Maybe UUID
t [Text]
w (Ready -> Response k a b) -> Get Ready -> Get (Response k a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Ready
decodeReady
    message Version
_ Maybe UUID
t [Text]
w OpCode
OcAuthenticate  = Maybe UUID -> [Text] -> Authenticate -> Response k a b
forall k a b.
Maybe UUID -> [Text] -> Authenticate -> Response k a b
RsAuthenticate  Maybe UUID
t [Text]
w (Authenticate -> Response k a b)
-> Get Authenticate -> Get (Response k a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Authenticate
decodeAuthenticate
    message Version
_ Maybe UUID
t [Text]
w OpCode
OcSupported     = Maybe UUID -> [Text] -> Supported -> Response k a b
forall k a b. Maybe UUID -> [Text] -> Supported -> Response k a b
RsSupported     Maybe UUID
t [Text]
w (Supported -> Response k a b)
-> Get Supported -> Get (Response k a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Supported
decodeSupported
    message Version
v Maybe UUID
t [Text]
w OpCode
OcResult        = Maybe UUID -> [Text] -> Result k a b -> Response k a b
forall k a b.
Maybe UUID -> [Text] -> Result k a b -> Response k a b
RsResult        Maybe UUID
t [Text]
w (Result k a b -> Response k a b)
-> Get (Result k a b) -> Get (Response k a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Version -> Get (Result k a b)
forall k a b. (Tuple a, Tuple b) => Version -> Get (Result k a b)
decodeResult Version
v
    message Version
v Maybe UUID
t [Text]
w OpCode
OcEvent         = Maybe UUID -> [Text] -> Event -> Response k a b
forall k a b. Maybe UUID -> [Text] -> Event -> Response k a b
RsEvent         Maybe UUID
t [Text]
w (Event -> Response k a b) -> Get Event -> Get (Response k a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Version -> Get Event
decodeEvent Version
v
    message Version
_ Maybe UUID
t [Text]
w OpCode
OcAuthChallenge = Maybe UUID -> [Text] -> AuthChallenge -> Response k a b
forall k a b.
Maybe UUID -> [Text] -> AuthChallenge -> Response k a b
RsAuthChallenge Maybe UUID
t [Text]
w (AuthChallenge -> Response k a b)
-> Get AuthChallenge -> Get (Response k a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get AuthChallenge
decodeAuthChallenge
    message Version
_ Maybe UUID
t [Text]
w OpCode
OcAuthSuccess   = Maybe UUID -> [Text] -> AuthSuccess -> Response k a b
forall k a b. Maybe UUID -> [Text] -> AuthSuccess -> Response k a b
RsAuthSuccess   Maybe UUID
t [Text]
w (AuthSuccess -> Response k a b)
-> Get AuthSuccess -> Get (Response k a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get AuthSuccess
decodeAuthSuccess
    message Version
_ Maybe UUID
_ [Text]
_ OpCode
other           = [Char] -> Get (Response k a b)
forall a. [Char] -> Get a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Get (Response k a b)) -> [Char] -> Get (Response k a b)
forall a b. (a -> b) -> a -> b
$ [Char]
"decode-response: unknown: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ OpCode -> [Char]
forall a. Show a => a -> [Char]
show OpCode
other

    deflate :: Compression -> ByteString -> Either [Char] ByteString
deflate Compression
f ByteString
x  = Either [Char] ByteString
-> (ByteString -> Either [Char] ByteString)
-> Maybe ByteString
-> Either [Char] ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Either [Char] ByteString
forall {b}. Either [Char] b
deflateError ByteString -> Either [Char] ByteString
forall a. a -> Either [Char] a
forall (m :: * -> *) a. Monad m => a -> m a
return (Compression -> ByteString -> Maybe ByteString
expand Compression
f ByteString
x)
    deflateError :: Either [Char] b
deflateError = [Char] -> Either [Char] b
forall a b. a -> Either a b
Left [Char]
"unpack: decompression failure"

------------------------------------------------------------------------------
-- AUTHENTICATE

-- | The server requires authentication.
newtype Authenticate = Authenticate Text deriving Int -> Authenticate -> ShowS
[Authenticate] -> ShowS
Authenticate -> [Char]
(Int -> Authenticate -> ShowS)
-> (Authenticate -> [Char])
-> ([Authenticate] -> ShowS)
-> Show Authenticate
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Authenticate -> ShowS
showsPrec :: Int -> Authenticate -> ShowS
$cshow :: Authenticate -> [Char]
show :: Authenticate -> [Char]
$cshowList :: [Authenticate] -> ShowS
showList :: [Authenticate] -> ShowS
Show

decodeAuthenticate :: Get Authenticate
decodeAuthenticate :: Get Authenticate
decodeAuthenticate = Text -> Authenticate
Authenticate (Text -> Authenticate) -> Get Text -> Get Authenticate
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Text
decodeString

------------------------------------------------------------------------------
-- AUTH_CHALLENGE

-- | A server-send authentication challenge.
newtype AuthChallenge = AuthChallenge (Maybe LB.ByteString) deriving Int -> AuthChallenge -> ShowS
[AuthChallenge] -> ShowS
AuthChallenge -> [Char]
(Int -> AuthChallenge -> ShowS)
-> (AuthChallenge -> [Char])
-> ([AuthChallenge] -> ShowS)
-> Show AuthChallenge
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AuthChallenge -> ShowS
showsPrec :: Int -> AuthChallenge -> ShowS
$cshow :: AuthChallenge -> [Char]
show :: AuthChallenge -> [Char]
$cshowList :: [AuthChallenge] -> ShowS
showList :: [AuthChallenge] -> ShowS
Show

decodeAuthChallenge :: Get AuthChallenge
decodeAuthChallenge :: Get AuthChallenge
decodeAuthChallenge = Maybe ByteString -> AuthChallenge
AuthChallenge (Maybe ByteString -> AuthChallenge)
-> Get (Maybe ByteString) -> Get AuthChallenge
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (Maybe ByteString)
decodeBytes

------------------------------------------------------------------------------
-- AUTH_SUCCESS

-- | Indicates the success of an authentication phase.
newtype AuthSuccess = AuthSuccess (Maybe LB.ByteString) deriving Int -> AuthSuccess -> ShowS
[AuthSuccess] -> ShowS
AuthSuccess -> [Char]
(Int -> AuthSuccess -> ShowS)
-> (AuthSuccess -> [Char])
-> ([AuthSuccess] -> ShowS)
-> Show AuthSuccess
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AuthSuccess -> ShowS
showsPrec :: Int -> AuthSuccess -> ShowS
$cshow :: AuthSuccess -> [Char]
show :: AuthSuccess -> [Char]
$cshowList :: [AuthSuccess] -> ShowS
showList :: [AuthSuccess] -> ShowS
Show

decodeAuthSuccess :: Get AuthSuccess
decodeAuthSuccess :: Get AuthSuccess
decodeAuthSuccess = Maybe ByteString -> AuthSuccess
AuthSuccess (Maybe ByteString -> AuthSuccess)
-> Get (Maybe ByteString) -> Get AuthSuccess
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (Maybe ByteString)
decodeBytes

------------------------------------------------------------------------------
-- READY

-- | The server is ready to process queries. Response of a 'Startup'
-- request.
data Ready = Ready deriving Int -> Ready -> ShowS
[Ready] -> ShowS
Ready -> [Char]
(Int -> Ready -> ShowS)
-> (Ready -> [Char]) -> ([Ready] -> ShowS) -> Show Ready
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Ready -> ShowS
showsPrec :: Int -> Ready -> ShowS
$cshow :: Ready -> [Char]
show :: Ready -> [Char]
$cshowList :: [Ready] -> ShowS
showList :: [Ready] -> ShowS
Show

decodeReady :: Get Ready
decodeReady :: Get Ready
decodeReady = Ready -> Get Ready
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return Ready
Ready

------------------------------------------------------------------------------
-- SUPPORTED

-- | The startup options supported by the server. Response of an 'Options'
-- request.
data Supported = Supported [CompressionAlgorithm] [CqlVersion] deriving Int -> Supported -> ShowS
[Supported] -> ShowS
Supported -> [Char]
(Int -> Supported -> ShowS)
-> (Supported -> [Char])
-> ([Supported] -> ShowS)
-> Show Supported
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Supported -> ShowS
showsPrec :: Int -> Supported -> ShowS
$cshow :: Supported -> [Char]
show :: Supported -> [Char]
$cshowList :: [Supported] -> ShowS
showList :: [Supported] -> ShowS
Show

decodeSupported :: Get Supported
decodeSupported :: Get Supported
decodeSupported = do
    [(Text, [Text])]
opt <- Get [(Text, [Text])]
decodeMultiMap
    [CompressionAlgorithm]
cmp <- (Text -> Get CompressionAlgorithm)
-> [Text] -> Get [CompressionAlgorithm]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Text -> Get CompressionAlgorithm
forall {a} {m :: * -> *}.
(Eq a, IsString a, MonadFail m, Show a) =>
a -> m CompressionAlgorithm
toCompression ([Text] -> Get [CompressionAlgorithm])
-> (Maybe [Text] -> [Text])
-> Maybe [Text]
-> Get [CompressionAlgorithm]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Maybe [Text] -> [Text]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [Text] -> Get [CompressionAlgorithm])
-> Maybe [Text] -> Get [CompressionAlgorithm]
forall a b. (a -> b) -> a -> b
$ Text -> [(Text, [Text])] -> Maybe [Text]
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"COMPRESSION" [(Text, [Text])]
opt
    let v :: [CqlVersion]
v = (Text -> CqlVersion) -> [Text] -> [CqlVersion]
forall a b. (a -> b) -> [a] -> [b]
map Text -> CqlVersion
toVersion ([Text] -> [CqlVersion])
-> (Maybe [Text] -> [Text]) -> Maybe [Text] -> [CqlVersion]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Maybe [Text] -> [Text]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [Text] -> [CqlVersion]) -> Maybe [Text] -> [CqlVersion]
forall a b. (a -> b) -> a -> b
$ Text -> [(Text, [Text])] -> Maybe [Text]
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"CQL_VERSION" [(Text, [Text])]
opt
    Supported -> Get Supported
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Supported -> Get Supported) -> Supported -> Get Supported
forall a b. (a -> b) -> a -> b
$ [CompressionAlgorithm] -> [CqlVersion] -> Supported
Supported [CompressionAlgorithm]
cmp [CqlVersion]
v
  where
    toCompression :: a -> m CompressionAlgorithm
toCompression a
"snappy" = CompressionAlgorithm -> m CompressionAlgorithm
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return CompressionAlgorithm
Snappy
    toCompression a
"lz4"    = CompressionAlgorithm -> m CompressionAlgorithm
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return CompressionAlgorithm
LZ4
    toCompression a
other    = [Char] -> m CompressionAlgorithm
forall a. [Char] -> m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> m CompressionAlgorithm)
-> [Char] -> m CompressionAlgorithm
forall a b. (a -> b) -> a -> b
$
        [Char]
"decode-supported: unknown compression: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
other

    toVersion :: Text -> CqlVersion
toVersion Text
"3.0.0" = CqlVersion
Cqlv300
    toVersion Text
other   = Text -> CqlVersion
CqlVersion Text
other

------------------------------------------------------------------------------
-- RESULT

-- | Query response.
data Result k a b
    = VoidResult
    | RowsResult         !MetaData [b]
    | SetKeyspaceResult  !Keyspace
    | PreparedResult     !(QueryId k a b) !MetaData !MetaData
    | SchemaChangeResult !SchemaChange
    deriving (Int -> Result k a b -> ShowS
[Result k a b] -> ShowS
Result k a b -> [Char]
(Int -> Result k a b -> ShowS)
-> (Result k a b -> [Char])
-> ([Result k a b] -> ShowS)
-> Show (Result k a b)
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
forall k a b. Show b => Int -> Result k a b -> ShowS
forall k a b. Show b => [Result k a b] -> ShowS
forall k a b. Show b => Result k a b -> [Char]
$cshowsPrec :: forall k a b. Show b => Int -> Result k a b -> ShowS
showsPrec :: Int -> Result k a b -> ShowS
$cshow :: forall k a b. Show b => Result k a b -> [Char]
show :: Result k a b -> [Char]
$cshowList :: forall k a b. Show b => [Result k a b] -> ShowS
showList :: [Result k a b] -> ShowS
Show)

-- | Part of a @RowsResult@. Describes the result set.
data MetaData = MetaData
    { MetaData -> Int32
columnCount       :: !Int32
    , MetaData -> Maybe PagingState
pagingState       :: Maybe PagingState
    , MetaData -> [ColumnSpec]
columnSpecs       :: [ColumnSpec]
    , MetaData -> [Int32]
primaryKeyIndices :: [Int32]
    } deriving (Int -> MetaData -> ShowS
[MetaData] -> ShowS
MetaData -> [Char]
(Int -> MetaData -> ShowS)
-> (MetaData -> [Char]) -> ([MetaData] -> ShowS) -> Show MetaData
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MetaData -> ShowS
showsPrec :: Int -> MetaData -> ShowS
$cshow :: MetaData -> [Char]
show :: MetaData -> [Char]
$cshowList :: [MetaData] -> ShowS
showList :: [MetaData] -> ShowS
Show)

decodeResult :: forall k a b. (Tuple a, Tuple b) => Version -> Get (Result k a b)
decodeResult :: forall k a b. (Tuple a, Tuple b) => Version -> Get (Result k a b)
decodeResult Version
v = Get Int32
decodeInt Get Int32 -> (Int32 -> Get (Result k a b)) -> Get (Result k a b)
forall a b. Get a -> (a -> Get b) -> Get b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int32 -> Get (Result k a b)
forall {a} {b} {k} {a}.
(Eq a, Num a, Show a, PrivateTuple b) =>
a -> Get (Result k a b)
go
  where
    go :: a -> Get (Result k a b)
go a
0x1 = Result k a b -> Get (Result k a b)
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return Result k a b
forall k a b. Result k a b
VoidResult
    go a
0x2 = do
        MetaData
m <- Get MetaData
decodeMetaData
        Int32
n <- Get Int32
decodeInt
        let c :: Int
c = Tagged b Int -> Int
forall a b. Tagged a b -> b
untag (Tagged b Int
forall a. PrivateTuple a => Tagged a Int
count :: Tagged b Int)
        Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== -Int
1 Bool -> Bool -> Bool
|| MetaData -> Int32
columnCount MetaData
m Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
c) (Get () -> Get ()) -> Get () -> Get ()
forall a b. (a -> b) -> a -> b
$
            [Char] -> Get ()
forall a. [Char] -> Get a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Get ()) -> [Char] -> Get ()
forall a b. (a -> b) -> a -> b
$ [Char]
"column count: "
                [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Int32 -> [Char]
forall a. Show a => a -> [Char]
show (MetaData -> Int32
columnCount MetaData
m)
                [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" =/= "
                [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
c
        let typecheck :: [ColumnType] -> [ColumnType]
typecheck = Tagged b ([ColumnType] -> [ColumnType])
-> [ColumnType] -> [ColumnType]
forall a b. Tagged a b -> b
untag (Tagged b ([ColumnType] -> [ColumnType])
forall a. PrivateTuple a => Tagged a ([ColumnType] -> [ColumnType])
check :: Tagged b ([ColumnType] -> [ColumnType]))
        let ctypes :: [ColumnType]
ctypes    = (ColumnSpec -> ColumnType) -> [ColumnSpec] -> [ColumnType]
forall a b. (a -> b) -> [a] -> [b]
map ColumnSpec -> ColumnType
columnType (MetaData -> [ColumnSpec]
columnSpecs MetaData
m)
        let expected :: [ColumnType]
expected  = [ColumnType] -> [ColumnType]
typecheck [ColumnType]
ctypes
        let message :: [Char]
message   = [Char]
"expected: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [ColumnType] -> [Char]
forall a. Show a => a -> [Char]
show [ColumnType]
expected [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
", but got " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [ColumnType] -> [Char]
forall a. Show a => a -> [Char]
show [ColumnType]
ctypes
        Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([ColumnType] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ColumnType]
expected) (Get () -> Get ()) -> Get () -> Get ()
forall a b. (a -> b) -> a -> b
$
            [Char] -> Get ()
forall a. [Char] -> Get a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Get ()) -> [Char] -> Get ()
forall a b. (a -> b) -> a -> b
$ [Char]
"column-type error: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
message
        MetaData -> [b] -> Result k a b
forall k a b. MetaData -> [b] -> Result k a b
RowsResult MetaData
m ([b] -> Result k a b) -> Get [b] -> Get (Result k a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Get b -> Get [b]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
n) (Version -> [ColumnSpec] -> Get b
forall a. PrivateTuple a => Version -> [ColumnSpec] -> Get a
tuple Version
v (MetaData -> [ColumnSpec]
columnSpecs MetaData
m))
    go a
0x3 = Keyspace -> Result k a b
forall k a b. Keyspace -> Result k a b
SetKeyspaceResult (Keyspace -> Result k a b) -> Get Keyspace -> Get (Result k a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Keyspace
decodeKeyspace
    go a
0x4 = if Version
v Version -> Version -> Bool
forall a. Eq a => a -> a -> Bool
== Version
V4
                then QueryId k a b -> MetaData -> MetaData -> Result k a b
forall k a b. QueryId k a b -> MetaData -> MetaData -> Result k a b
PreparedResult (QueryId k a b -> MetaData -> MetaData -> Result k a b)
-> Get (QueryId k a b)
-> Get (MetaData -> MetaData -> Result k a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (QueryId k a b)
forall k a b. Get (QueryId k a b)
decodeQueryId Get (MetaData -> MetaData -> Result k a b)
-> Get MetaData -> Get (MetaData -> Result k a b)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get MetaData
decodePreparedV4 Get (MetaData -> Result k a b)
-> Get MetaData -> Get (Result k a b)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get MetaData
decodeMetaData
                else QueryId k a b -> MetaData -> MetaData -> Result k a b
forall k a b. QueryId k a b -> MetaData -> MetaData -> Result k a b
PreparedResult (QueryId k a b -> MetaData -> MetaData -> Result k a b)
-> Get (QueryId k a b)
-> Get (MetaData -> MetaData -> Result k a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (QueryId k a b)
forall k a b. Get (QueryId k a b)
decodeQueryId Get (MetaData -> MetaData -> Result k a b)
-> Get MetaData -> Get (MetaData -> Result k a b)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get MetaData
decodeMetaData Get (MetaData -> Result k a b)
-> Get MetaData -> Get (Result k a b)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get MetaData
decodeMetaData
    go a
0x5 = SchemaChange -> Result k a b
forall k a b. SchemaChange -> Result k a b
SchemaChangeResult (SchemaChange -> Result k a b)
-> Get SchemaChange -> Get (Result k a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Version -> Get SchemaChange
decodeSchemaChange Version
v
    go a
int = [Char] -> Get (Result k a b)
forall a. [Char] -> Get a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Get (Result k a b)) -> [Char] -> Get (Result k a b)
forall a b. (a -> b) -> a -> b
$ [Char]
"decode-result: unknown: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
int

decodeMetaData :: Get MetaData
decodeMetaData :: Get MetaData
decodeMetaData = do
    Int32
f <- Get Int32
decodeInt
    Int32
n <- Get Int32
decodeInt
    Maybe PagingState
p <- if Int32 -> Bool
forall {a}. Bits a => a -> Bool
hasMorePages Int32
f then Get (Maybe PagingState)
decodePagingState else Maybe PagingState -> Get (Maybe PagingState)
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe PagingState
forall a. Maybe a
Nothing
    if Int32 -> Bool
forall {a}. Bits a => a -> Bool
hasNoMetaData Int32
f
        then MetaData -> Get MetaData
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (MetaData -> Get MetaData) -> MetaData -> Get MetaData
forall a b. (a -> b) -> a -> b
$ Int32 -> Maybe PagingState -> [ColumnSpec] -> [Int32] -> MetaData
MetaData Int32
n Maybe PagingState
p [] []
        else Int32 -> Maybe PagingState -> [ColumnSpec] -> [Int32] -> MetaData
MetaData Int32
n Maybe PagingState
p ([ColumnSpec] -> [Int32] -> MetaData)
-> Get [ColumnSpec] -> Get ([Int32] -> MetaData)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int32 -> Bool -> Get [ColumnSpec]
forall {a}. Integral a => a -> Bool -> Get [ColumnSpec]
decodeSpecs Int32
n (Int32 -> Bool
forall {a}. Bits a => a -> Bool
hasGlobalSpec Int32
f) Get ([Int32] -> MetaData) -> Get [Int32] -> Get MetaData
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Int32] -> Get [Int32]
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
  where
    hasGlobalSpec :: a -> Bool
hasGlobalSpec a
f = a
f a -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
`testBit` Int
0
    hasMorePages :: a -> Bool
hasMorePages  a
f = a
f a -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
`testBit` Int
1
    hasNoMetaData :: a -> Bool
hasNoMetaData a
f = a
f a -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
`testBit` Int
2

    decodeSpecs :: a -> Bool -> Get [ColumnSpec]
decodeSpecs a
n Bool
True = do
        Keyspace
k <- Get Keyspace
decodeKeyspace
        Table
t <- Get Table
decodeTable
        Int -> Get ColumnSpec -> Get [ColumnSpec]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n) (Get ColumnSpec -> Get [ColumnSpec])
-> Get ColumnSpec -> Get [ColumnSpec]
forall a b. (a -> b) -> a -> b
$ Keyspace -> Table -> Text -> ColumnType -> ColumnSpec
ColumnSpec Keyspace
k Table
t
            (Text -> ColumnType -> ColumnSpec)
-> Get Text -> Get (ColumnType -> ColumnSpec)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Text
decodeString
            Get (ColumnType -> ColumnSpec) -> Get ColumnType -> Get ColumnSpec
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get ColumnType
decodeColumnType

    decodeSpecs a
n Bool
False =
        Int -> Get ColumnSpec -> Get [ColumnSpec]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n) (Get ColumnSpec -> Get [ColumnSpec])
-> Get ColumnSpec -> Get [ColumnSpec]
forall a b. (a -> b) -> a -> b
$ Keyspace -> Table -> Text -> ColumnType -> ColumnSpec
ColumnSpec
            (Keyspace -> Table -> Text -> ColumnType -> ColumnSpec)
-> Get Keyspace -> Get (Table -> Text -> ColumnType -> ColumnSpec)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Keyspace
decodeKeyspace
            Get (Table -> Text -> ColumnType -> ColumnSpec)
-> Get Table -> Get (Text -> ColumnType -> ColumnSpec)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Table
decodeTable
            Get (Text -> ColumnType -> ColumnSpec)
-> Get Text -> Get (ColumnType -> ColumnSpec)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Text
decodeString
            Get (ColumnType -> ColumnSpec) -> Get ColumnType -> Get ColumnSpec
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get ColumnType
decodeColumnType

decodePreparedV4 :: Get MetaData
decodePreparedV4 :: Get MetaData
decodePreparedV4 = do
    Int32
f <- Get Int32
decodeInt
    Int32
n <- Get Int32
decodeInt
    Int32
pkCount <- Get Int32
decodeInt
    [Word16]
pkis <- Int -> Get Word16 -> Get [Word16]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
pkCount) Get Word16
decodeShort
    [ColumnSpec]
specs <- Int32 -> Bool -> Get [ColumnSpec]
forall {a}. Integral a => a -> Bool -> Get [ColumnSpec]
decodeSpecs Int32
n (Int32 -> Bool
forall {a}. Bits a => a -> Bool
hasGlobalSpec Int32
f)
    MetaData -> Get MetaData
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (MetaData -> Get MetaData) -> MetaData -> Get MetaData
forall a b. (a -> b) -> a -> b
$ Int32 -> Maybe PagingState -> [ColumnSpec] -> [Int32] -> MetaData
MetaData Int32
n Maybe PagingState
forall a. Maybe a
Nothing [ColumnSpec]
specs (Word16 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16 -> Int32) -> [Word16] -> [Int32]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Word16]
pkis)
  where
    hasGlobalSpec :: a -> Bool
hasGlobalSpec a
f = a
f a -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
`testBit` Int
0

    decodeSpecs :: a -> Bool -> Get [ColumnSpec]
decodeSpecs a
n Bool
True = do
        Keyspace
k <- Get Keyspace
decodeKeyspace
        Table
t <- Get Table
decodeTable
        Int -> Get ColumnSpec -> Get [ColumnSpec]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n) (Get ColumnSpec -> Get [ColumnSpec])
-> Get ColumnSpec -> Get [ColumnSpec]
forall a b. (a -> b) -> a -> b
$ Keyspace -> Table -> Text -> ColumnType -> ColumnSpec
ColumnSpec Keyspace
k Table
t
            (Text -> ColumnType -> ColumnSpec)
-> Get Text -> Get (ColumnType -> ColumnSpec)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Text
decodeString
            Get (ColumnType -> ColumnSpec) -> Get ColumnType -> Get ColumnSpec
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get ColumnType
decodeColumnType

    decodeSpecs a
n Bool
False =
        Int -> Get ColumnSpec -> Get [ColumnSpec]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n) (Get ColumnSpec -> Get [ColumnSpec])
-> Get ColumnSpec -> Get [ColumnSpec]
forall a b. (a -> b) -> a -> b
$ Keyspace -> Table -> Text -> ColumnType -> ColumnSpec
ColumnSpec
            (Keyspace -> Table -> Text -> ColumnType -> ColumnSpec)
-> Get Keyspace -> Get (Table -> Text -> ColumnType -> ColumnSpec)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Keyspace
decodeKeyspace
            Get (Table -> Text -> ColumnType -> ColumnSpec)
-> Get Table -> Get (Text -> ColumnType -> ColumnSpec)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Table
decodeTable
            Get (Text -> ColumnType -> ColumnSpec)
-> Get Text -> Get (ColumnType -> ColumnSpec)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Text
decodeString
            Get (ColumnType -> ColumnSpec) -> Get ColumnType -> Get ColumnSpec
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get ColumnType
decodeColumnType

------------------------------------------------------------------------------
-- SCHEMA_CHANGE

data SchemaChange
    = SchemaCreated !Change
    | SchemaUpdated !Change
    | SchemaDropped !Change
    deriving Int -> SchemaChange -> ShowS
[SchemaChange] -> ShowS
SchemaChange -> [Char]
(Int -> SchemaChange -> ShowS)
-> (SchemaChange -> [Char])
-> ([SchemaChange] -> ShowS)
-> Show SchemaChange
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SchemaChange -> ShowS
showsPrec :: Int -> SchemaChange -> ShowS
$cshow :: SchemaChange -> [Char]
show :: SchemaChange -> [Char]
$cshowList :: [SchemaChange] -> ShowS
showList :: [SchemaChange] -> ShowS
Show

data Change
    = KeyspaceChange  !Keyspace
    | TableChange     !Keyspace !Table
    | TypeChange      !Keyspace !Text
    | FunctionChange  !Keyspace !Text ![Text]
    | AggregateChange !Keyspace !Text ![Text]
    deriving Int -> Change -> ShowS
[Change] -> ShowS
Change -> [Char]
(Int -> Change -> ShowS)
-> (Change -> [Char]) -> ([Change] -> ShowS) -> Show Change
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Change -> ShowS
showsPrec :: Int -> Change -> ShowS
$cshow :: Change -> [Char]
show :: Change -> [Char]
$cshowList :: [Change] -> ShowS
showList :: [Change] -> ShowS
Show

decodeSchemaChange :: Version -> Get SchemaChange
decodeSchemaChange :: Version -> Get SchemaChange
decodeSchemaChange Version
v = Get Text
decodeString Get Text -> (Text -> Get SchemaChange) -> Get SchemaChange
forall a b. Get a -> (a -> Get b) -> Get b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Get SchemaChange
forall {a}. (Eq a, IsString a, Show a) => a -> Get SchemaChange
fromString
  where
    fromString :: a -> Get SchemaChange
fromString a
"CREATED" = Change -> SchemaChange
SchemaCreated (Change -> SchemaChange) -> Get Change -> Get SchemaChange
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Version -> Get Change
decodeChange Version
v
    fromString a
"UPDATED" = Change -> SchemaChange
SchemaUpdated (Change -> SchemaChange) -> Get Change -> Get SchemaChange
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Version -> Get Change
decodeChange Version
v
    fromString a
"DROPPED" = Change -> SchemaChange
SchemaDropped (Change -> SchemaChange) -> Get Change -> Get SchemaChange
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Version -> Get Change
decodeChange Version
v
    fromString a
other     = [Char] -> Get SchemaChange
forall a. [Char] -> Get a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Get SchemaChange) -> [Char] -> Get SchemaChange
forall a b. (a -> b) -> a -> b
$ [Char]
"decode-schema-change: unknown: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
other

decodeChange :: Version -> Get Change
decodeChange :: Version -> Get Change
decodeChange Version
V4 = Get Text
decodeString Get Text -> (Text -> Get Change) -> Get Change
forall a b. Get a -> (a -> Get b) -> Get b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Get Change
forall {a}. (Eq a, IsString a, Show a) => a -> Get Change
fromString
  where
    fromString :: a -> Get Change
fromString a
"KEYSPACE"  = Keyspace -> Change
KeyspaceChange  (Keyspace -> Change) -> Get Keyspace -> Get Change
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Keyspace
decodeKeyspace
    fromString a
"TABLE"     = Keyspace -> Table -> Change
TableChange     (Keyspace -> Table -> Change)
-> Get Keyspace -> Get (Table -> Change)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Keyspace
decodeKeyspace Get (Table -> Change) -> Get Table -> Get Change
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Table
decodeTable
    fromString a
"TYPE"      = Keyspace -> Text -> Change
TypeChange      (Keyspace -> Text -> Change)
-> Get Keyspace -> Get (Text -> Change)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Keyspace
decodeKeyspace Get (Text -> Change) -> Get Text -> Get Change
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Text
decodeString
    fromString a
"FUNCTION"  = Keyspace -> Text -> [Text] -> Change
FunctionChange  (Keyspace -> Text -> [Text] -> Change)
-> Get Keyspace -> Get (Text -> [Text] -> Change)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Keyspace
decodeKeyspace Get (Text -> [Text] -> Change)
-> Get Text -> Get ([Text] -> Change)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Text
decodeString Get ([Text] -> Change) -> Get [Text] -> Get Change
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get [Text]
decodeList
    fromString a
"AGGREGATE" = Keyspace -> Text -> [Text] -> Change
AggregateChange (Keyspace -> Text -> [Text] -> Change)
-> Get Keyspace -> Get (Text -> [Text] -> Change)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Keyspace
decodeKeyspace Get (Text -> [Text] -> Change)
-> Get Text -> Get ([Text] -> Change)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Text
decodeString Get ([Text] -> Change) -> Get [Text] -> Get Change
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get [Text]
decodeList
    fromString a
other      = [Char] -> Get Change
forall a. [Char] -> Get a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Get Change) -> [Char] -> Get Change
forall a b. (a -> b) -> a -> b
$ [Char]
"decode-change V4: unknown: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
other
decodeChange Version
V3 = Get Text
decodeString Get Text -> (Text -> Get Change) -> Get Change
forall a b. Get a -> (a -> Get b) -> Get b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Get Change
forall {a}. (Eq a, IsString a, Show a) => a -> Get Change
fromString
  where
    fromString :: a -> Get Change
fromString a
"KEYSPACE" = Keyspace -> Change
KeyspaceChange (Keyspace -> Change) -> Get Keyspace -> Get Change
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Keyspace
decodeKeyspace
    fromString a
"TABLE"    = Keyspace -> Table -> Change
TableChange    (Keyspace -> Table -> Change)
-> Get Keyspace -> Get (Table -> Change)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Keyspace
decodeKeyspace Get (Table -> Change) -> Get Table -> Get Change
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Table
decodeTable
    fromString a
"TYPE"     = Keyspace -> Text -> Change
TypeChange     (Keyspace -> Text -> Change)
-> Get Keyspace -> Get (Text -> Change)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Keyspace
decodeKeyspace Get (Text -> Change) -> Get Text -> Get Change
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Text
decodeString
    fromString a
other      = [Char] -> Get Change
forall a. [Char] -> Get a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Get Change) -> [Char] -> Get Change
forall a b. (a -> b) -> a -> b
$ [Char]
"decode-change V3: unknown: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
other

------------------------------------------------------------------------------
-- EVENT

-- | Messages send by the server without request, if the connection has
-- been 'Register'ed to receive such events.
data Event
    = TopologyEvent !TopologyChange !SockAddr
    | StatusEvent   !StatusChange   !SockAddr
    | SchemaEvent   !SchemaChange
    deriving Int -> Event -> ShowS
[Event] -> ShowS
Event -> [Char]
(Int -> Event -> ShowS)
-> (Event -> [Char]) -> ([Event] -> ShowS) -> Show Event
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Event -> ShowS
showsPrec :: Int -> Event -> ShowS
$cshow :: Event -> [Char]
show :: Event -> [Char]
$cshowList :: [Event] -> ShowS
showList :: [Event] -> ShowS
Show

data TopologyChange = NewNode | RemovedNode deriving (TopologyChange -> TopologyChange -> Bool
(TopologyChange -> TopologyChange -> Bool)
-> (TopologyChange -> TopologyChange -> Bool) -> Eq TopologyChange
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TopologyChange -> TopologyChange -> Bool
== :: TopologyChange -> TopologyChange -> Bool
$c/= :: TopologyChange -> TopologyChange -> Bool
/= :: TopologyChange -> TopologyChange -> Bool
Eq, Eq TopologyChange
Eq TopologyChange =>
(TopologyChange -> TopologyChange -> Ordering)
-> (TopologyChange -> TopologyChange -> Bool)
-> (TopologyChange -> TopologyChange -> Bool)
-> (TopologyChange -> TopologyChange -> Bool)
-> (TopologyChange -> TopologyChange -> Bool)
-> (TopologyChange -> TopologyChange -> TopologyChange)
-> (TopologyChange -> TopologyChange -> TopologyChange)
-> Ord TopologyChange
TopologyChange -> TopologyChange -> Bool
TopologyChange -> TopologyChange -> Ordering
TopologyChange -> TopologyChange -> TopologyChange
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: TopologyChange -> TopologyChange -> Ordering
compare :: TopologyChange -> TopologyChange -> Ordering
$c< :: TopologyChange -> TopologyChange -> Bool
< :: TopologyChange -> TopologyChange -> Bool
$c<= :: TopologyChange -> TopologyChange -> Bool
<= :: TopologyChange -> TopologyChange -> Bool
$c> :: TopologyChange -> TopologyChange -> Bool
> :: TopologyChange -> TopologyChange -> Bool
$c>= :: TopologyChange -> TopologyChange -> Bool
>= :: TopologyChange -> TopologyChange -> Bool
$cmax :: TopologyChange -> TopologyChange -> TopologyChange
max :: TopologyChange -> TopologyChange -> TopologyChange
$cmin :: TopologyChange -> TopologyChange -> TopologyChange
min :: TopologyChange -> TopologyChange -> TopologyChange
Ord, Int -> TopologyChange -> ShowS
[TopologyChange] -> ShowS
TopologyChange -> [Char]
(Int -> TopologyChange -> ShowS)
-> (TopologyChange -> [Char])
-> ([TopologyChange] -> ShowS)
-> Show TopologyChange
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TopologyChange -> ShowS
showsPrec :: Int -> TopologyChange -> ShowS
$cshow :: TopologyChange -> [Char]
show :: TopologyChange -> [Char]
$cshowList :: [TopologyChange] -> ShowS
showList :: [TopologyChange] -> ShowS
Show)
data StatusChange   = Up | Down deriving (StatusChange -> StatusChange -> Bool
(StatusChange -> StatusChange -> Bool)
-> (StatusChange -> StatusChange -> Bool) -> Eq StatusChange
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StatusChange -> StatusChange -> Bool
== :: StatusChange -> StatusChange -> Bool
$c/= :: StatusChange -> StatusChange -> Bool
/= :: StatusChange -> StatusChange -> Bool
Eq, Eq StatusChange
Eq StatusChange =>
(StatusChange -> StatusChange -> Ordering)
-> (StatusChange -> StatusChange -> Bool)
-> (StatusChange -> StatusChange -> Bool)
-> (StatusChange -> StatusChange -> Bool)
-> (StatusChange -> StatusChange -> Bool)
-> (StatusChange -> StatusChange -> StatusChange)
-> (StatusChange -> StatusChange -> StatusChange)
-> Ord StatusChange
StatusChange -> StatusChange -> Bool
StatusChange -> StatusChange -> Ordering
StatusChange -> StatusChange -> StatusChange
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: StatusChange -> StatusChange -> Ordering
compare :: StatusChange -> StatusChange -> Ordering
$c< :: StatusChange -> StatusChange -> Bool
< :: StatusChange -> StatusChange -> Bool
$c<= :: StatusChange -> StatusChange -> Bool
<= :: StatusChange -> StatusChange -> Bool
$c> :: StatusChange -> StatusChange -> Bool
> :: StatusChange -> StatusChange -> Bool
$c>= :: StatusChange -> StatusChange -> Bool
>= :: StatusChange -> StatusChange -> Bool
$cmax :: StatusChange -> StatusChange -> StatusChange
max :: StatusChange -> StatusChange -> StatusChange
$cmin :: StatusChange -> StatusChange -> StatusChange
min :: StatusChange -> StatusChange -> StatusChange
Ord, Int -> StatusChange -> ShowS
[StatusChange] -> ShowS
StatusChange -> [Char]
(Int -> StatusChange -> ShowS)
-> (StatusChange -> [Char])
-> ([StatusChange] -> ShowS)
-> Show StatusChange
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StatusChange -> ShowS
showsPrec :: Int -> StatusChange -> ShowS
$cshow :: StatusChange -> [Char]
show :: StatusChange -> [Char]
$cshowList :: [StatusChange] -> ShowS
showList :: [StatusChange] -> ShowS
Show)

decodeEvent :: Version -> Get Event
decodeEvent :: Version -> Get Event
decodeEvent Version
v = Get Text
decodeString Get Text -> (Text -> Get Event) -> Get Event
forall a b. Get a -> (a -> Get b) -> Get b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Get Event
forall {a}. (Eq a, IsString a, Show a) => a -> Get Event
decodeByType
  where
    decodeByType :: a -> Get Event
decodeByType a
"TOPOLOGY_CHANGE" = TopologyChange -> SockAddr -> Event
TopologyEvent (TopologyChange -> SockAddr -> Event)
-> Get TopologyChange -> Get (SockAddr -> Event)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get TopologyChange
decodeTopologyChange Get (SockAddr -> Event) -> Get SockAddr -> Get Event
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get SockAddr
decodeSockAddr
    decodeByType a
"STATUS_CHANGE"   = StatusChange -> SockAddr -> Event
StatusEvent   (StatusChange -> SockAddr -> Event)
-> Get StatusChange -> Get (SockAddr -> Event)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get StatusChange
decodeStatusChange Get (SockAddr -> Event) -> Get SockAddr -> Get Event
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get SockAddr
decodeSockAddr
    decodeByType a
"SCHEMA_CHANGE"   = SchemaChange -> Event
SchemaEvent   (SchemaChange -> Event) -> Get SchemaChange -> Get Event
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Version -> Get SchemaChange
decodeSchemaChange Version
v
    decodeByType a
other             = [Char] -> Get Event
forall a. [Char] -> Get a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Get Event) -> [Char] -> Get Event
forall a b. (a -> b) -> a -> b
$ [Char]
"decode-event: unknown: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
other

decodeTopologyChange :: Get TopologyChange
decodeTopologyChange :: Get TopologyChange
decodeTopologyChange = Get Text
decodeString Get Text -> (Text -> Get TopologyChange) -> Get TopologyChange
forall a b. Get a -> (a -> Get b) -> Get b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Get TopologyChange
forall {a} {m :: * -> *}.
(Eq a, IsString a, MonadFail m, Show a) =>
a -> m TopologyChange
fromString
  where
    fromString :: a -> m TopologyChange
fromString a
"NEW_NODE"     = TopologyChange -> m TopologyChange
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return TopologyChange
NewNode
    fromString a
"REMOVED_NODE" = TopologyChange -> m TopologyChange
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return TopologyChange
RemovedNode
    fromString a
other          = [Char] -> m TopologyChange
forall a. [Char] -> m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> m TopologyChange) -> [Char] -> m TopologyChange
forall a b. (a -> b) -> a -> b
$
        [Char]
"decode-topology: unknown: "  [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
other

decodeStatusChange :: Get StatusChange
decodeStatusChange :: Get StatusChange
decodeStatusChange = Get Text
decodeString Get Text -> (Text -> Get StatusChange) -> Get StatusChange
forall a b. Get a -> (a -> Get b) -> Get b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Get StatusChange
forall {a} {m :: * -> *}.
(Eq a, IsString a, MonadFail m, Show a) =>
a -> m StatusChange
fromString
  where
    fromString :: a -> m StatusChange
fromString a
"UP"   = StatusChange -> m StatusChange
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return StatusChange
Up
    fromString a
"DOWN" = StatusChange -> m StatusChange
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return StatusChange
Down
    fromString a
other  = [Char] -> m StatusChange
forall a. [Char] -> m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> m StatusChange) -> [Char] -> m StatusChange
forall a b. (a -> b) -> a -> b
$
        [Char]
"decode-status-change: unknown: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
other

-----------------------------------------------------------------------------
-- ERROR

-- | Error response.
data Error
    = AlreadyExists   !Text !Keyspace !Table
    | BadCredentials  !Text
    | ConfigError     !Text
    | FunctionFailure !Text !Keyspace !Text ![Text]
    | Invalid         !Text
    | IsBootstrapping !Text
    | Overloaded      !Text
    | ProtocolError   !Text
    | ServerError     !Text
    | SyntaxError     !Text
    | TruncateError   !Text
    | Unauthorized    !Text
    | Unprepared      !Text !ByteString
    | Unavailable
        { Error -> Text
unavailMessage     :: !Text
        , Error -> Consistency
unavailConsistency :: !Consistency
        , Error -> Int32
unavailNumRequired :: !Int32
        , Error -> Int32
unavailNumAlive    :: !Int32
        }
    | ReadFailure
        { Error -> Text
rFailureMessage     :: !Text
        , Error -> Consistency
rFailureConsistency :: !Consistency
        , Error -> Int32
rFailureNumAck      :: !Int32
        , Error -> Int32
rFailureNumRequired :: !Int32
        , Error -> Int32
rFailureNumFailures :: !Int32
        , Error -> Bool
rFailureDataPresent :: !Bool
        }

    | ReadTimeout
        { Error -> Text
rTimeoutMessage     :: !Text
        , Error -> Consistency
rTimeoutConsistency :: !Consistency
        , Error -> Int32
rTimeoutNumAck      :: !Int32
        , Error -> Int32
rTimeoutNumRequired :: !Int32
        , Error -> Bool
rTimeoutDataPresent :: !Bool
        }
    | WriteFailure
        { Error -> Text
wFailureMessage     :: !Text
        , Error -> Consistency
wFailureConsistency :: !Consistency
        , Error -> Int32
wFailureNumAck      :: !Int32
        , Error -> Int32
wFailureNumRequired :: !Int32
        , Error -> Int32
wFailureNumFailures :: !Int32
        , Error -> WriteType
wFailureWriteType   :: !WriteType
        }
    | WriteTimeout
        { Error -> Text
wTimeoutMessage     :: !Text
        , Error -> Consistency
wTimeoutConsistency :: !Consistency
        , Error -> Int32
wTimeoutNumAck      :: !Int32
        , Error -> Int32
wTimeoutNumRequired :: !Int32
        , Error -> WriteType
wTimeoutWriteType   :: !WriteType
        }

    deriving (Error -> Error -> Bool
(Error -> Error -> Bool) -> (Error -> Error -> Bool) -> Eq Error
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Error -> Error -> Bool
== :: Error -> Error -> Bool
$c/= :: Error -> Error -> Bool
/= :: Error -> Error -> Bool
Eq, Int -> Error -> ShowS
[Error] -> ShowS
Error -> [Char]
(Int -> Error -> ShowS)
-> (Error -> [Char]) -> ([Error] -> ShowS) -> Show Error
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Error -> ShowS
showsPrec :: Int -> Error -> ShowS
$cshow :: Error -> [Char]
show :: Error -> [Char]
$cshowList :: [Error] -> ShowS
showList :: [Error] -> ShowS
Show, Typeable)

instance Exception Error

data WriteType
    = WriteSimple
    | WriteBatch
    | WriteBatchLog
    | WriteUnloggedBatch
    | WriteCounter
    | WriteCas
    | WriteView
        -- ^ Since 'V4'.
    | WriteCdc
        -- ^ Since 'V4'.
    deriving (WriteType -> WriteType -> Bool
(WriteType -> WriteType -> Bool)
-> (WriteType -> WriteType -> Bool) -> Eq WriteType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: WriteType -> WriteType -> Bool
== :: WriteType -> WriteType -> Bool
$c/= :: WriteType -> WriteType -> Bool
/= :: WriteType -> WriteType -> Bool
Eq, Int -> WriteType -> ShowS
[WriteType] -> ShowS
WriteType -> [Char]
(Int -> WriteType -> ShowS)
-> (WriteType -> [Char])
-> ([WriteType] -> ShowS)
-> Show WriteType
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WriteType -> ShowS
showsPrec :: Int -> WriteType -> ShowS
$cshow :: WriteType -> [Char]
show :: WriteType -> [Char]
$cshowList :: [WriteType] -> ShowS
showList :: [WriteType] -> ShowS
Show)

decodeError :: Get Error
decodeError :: Get Error
decodeError = do
    Int32
code <- Get Int32
decodeInt
    Text
msg  <- Get Text
decodeString
    Int32 -> Text -> Get Error
toError Int32
code Text
msg
  where
    toError :: Int32 -> Text -> Get Error
    toError :: Int32 -> Text -> Get Error
toError Int32
0x0000 Text
m = Error -> Get Error
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Error -> Get Error) -> Error -> Get Error
forall a b. (a -> b) -> a -> b
$ Text -> Error
ServerError Text
m
    toError Int32
0x000A Text
m = Error -> Get Error
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Error -> Get Error) -> Error -> Get Error
forall a b. (a -> b) -> a -> b
$ Text -> Error
ProtocolError Text
m
    toError Int32
0x0100 Text
m = Error -> Get Error
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Error -> Get Error) -> Error -> Get Error
forall a b. (a -> b) -> a -> b
$ Text -> Error
BadCredentials Text
m
    toError Int32
0x1000 Text
m = Text -> Consistency -> Int32 -> Int32 -> Error
Unavailable Text
m (Consistency -> Int32 -> Int32 -> Error)
-> Get Consistency -> Get (Int32 -> Int32 -> Error)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Consistency
decodeConsistency Get (Int32 -> Int32 -> Error) -> Get Int32 -> Get (Int32 -> Error)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Int32
decodeInt Get (Int32 -> Error) -> Get Int32 -> Get Error
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Int32
decodeInt
    toError Int32
0x1001 Text
m = Error -> Get Error
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Error -> Get Error) -> Error -> Get Error
forall a b. (a -> b) -> a -> b
$ Text -> Error
Overloaded Text
m
    toError Int32
0x1002 Text
m = Error -> Get Error
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Error -> Get Error) -> Error -> Get Error
forall a b. (a -> b) -> a -> b
$ Text -> Error
IsBootstrapping Text
m
    toError Int32
0x1003 Text
m = Error -> Get Error
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Error -> Get Error) -> Error -> Get Error
forall a b. (a -> b) -> a -> b
$ Text -> Error
TruncateError Text
m
    toError Int32
0x1100 Text
m = Text -> Consistency -> Int32 -> Int32 -> WriteType -> Error
WriteTimeout Text
m
        (Consistency -> Int32 -> Int32 -> WriteType -> Error)
-> Get Consistency -> Get (Int32 -> Int32 -> WriteType -> Error)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Consistency
decodeConsistency
        Get (Int32 -> Int32 -> WriteType -> Error)
-> Get Int32 -> Get (Int32 -> WriteType -> Error)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Int32
decodeInt
        Get (Int32 -> WriteType -> Error)
-> Get Int32 -> Get (WriteType -> Error)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Int32
decodeInt
        Get (WriteType -> Error) -> Get WriteType -> Get Error
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get WriteType
decodeWriteType
    toError Int32
0x1200 Text
m = Text -> Consistency -> Int32 -> Int32 -> Bool -> Error
ReadTimeout Text
m
        (Consistency -> Int32 -> Int32 -> Bool -> Error)
-> Get Consistency -> Get (Int32 -> Int32 -> Bool -> Error)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Consistency
decodeConsistency
        Get (Int32 -> Int32 -> Bool -> Error)
-> Get Int32 -> Get (Int32 -> Bool -> Error)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Int32
decodeInt
        Get (Int32 -> Bool -> Error) -> Get Int32 -> Get (Bool -> Error)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Int32
decodeInt
        Get (Bool -> Error) -> Get Bool -> Get Error
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Word8 -> Bool
bool (Word8 -> Bool) -> Get Word8 -> Get Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
decodeByte)
    toError Int32
0x1300 Text
m = Text -> Consistency -> Int32 -> Int32 -> Int32 -> Bool -> Error
ReadFailure Text
m
        (Consistency -> Int32 -> Int32 -> Int32 -> Bool -> Error)
-> Get Consistency
-> Get (Int32 -> Int32 -> Int32 -> Bool -> Error)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Consistency
decodeConsistency
        Get (Int32 -> Int32 -> Int32 -> Bool -> Error)
-> Get Int32 -> Get (Int32 -> Int32 -> Bool -> Error)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Int32
decodeInt
        Get (Int32 -> Int32 -> Bool -> Error)
-> Get Int32 -> Get (Int32 -> Bool -> Error)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Int32
decodeInt
        Get (Int32 -> Bool -> Error) -> Get Int32 -> Get (Bool -> Error)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Int32
decodeInt
        Get (Bool -> Error) -> Get Bool -> Get Error
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Word8 -> Bool
bool (Word8 -> Bool) -> Get Word8 -> Get Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
decodeByte)
    toError Int32
0x1400 Text
m = Text -> Keyspace -> Text -> [Text] -> Error
FunctionFailure Text
m
        (Keyspace -> Text -> [Text] -> Error)
-> Get Keyspace -> Get (Text -> [Text] -> Error)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Keyspace
decodeKeyspace
        Get (Text -> [Text] -> Error) -> Get Text -> Get ([Text] -> Error)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Text
decodeString
        Get ([Text] -> Error) -> Get [Text] -> Get Error
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get [Text]
decodeList
    toError Int32
0x1500 Text
m = Text
-> Consistency -> Int32 -> Int32 -> Int32 -> WriteType -> Error
WriteFailure Text
m
        (Consistency -> Int32 -> Int32 -> Int32 -> WriteType -> Error)
-> Get Consistency
-> Get (Int32 -> Int32 -> Int32 -> WriteType -> Error)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Consistency
decodeConsistency
        Get (Int32 -> Int32 -> Int32 -> WriteType -> Error)
-> Get Int32 -> Get (Int32 -> Int32 -> WriteType -> Error)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Int32
decodeInt
        Get (Int32 -> Int32 -> WriteType -> Error)
-> Get Int32 -> Get (Int32 -> WriteType -> Error)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Int32
decodeInt
        Get (Int32 -> WriteType -> Error)
-> Get Int32 -> Get (WriteType -> Error)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Int32
decodeInt
        Get (WriteType -> Error) -> Get WriteType -> Get Error
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get WriteType
decodeWriteType
    toError Int32
0x2000 Text
m = Error -> Get Error
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Error -> Get Error) -> Error -> Get Error
forall a b. (a -> b) -> a -> b
$ Text -> Error
SyntaxError Text
m
    toError Int32
0x2100 Text
m = Error -> Get Error
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Error -> Get Error) -> Error -> Get Error
forall a b. (a -> b) -> a -> b
$ Text -> Error
Unauthorized Text
m
    toError Int32
0x2200 Text
m = Error -> Get Error
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Error -> Get Error) -> Error -> Get Error
forall a b. (a -> b) -> a -> b
$ Text -> Error
Invalid Text
m
    toError Int32
0x2300 Text
m = Error -> Get Error
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Error -> Get Error) -> Error -> Get Error
forall a b. (a -> b) -> a -> b
$ Text -> Error
ConfigError Text
m
    toError Int32
0x2400 Text
m = Text -> Keyspace -> Table -> Error
AlreadyExists Text
m (Keyspace -> Table -> Error)
-> Get Keyspace -> Get (Table -> Error)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Keyspace
decodeKeyspace Get (Table -> Error) -> Get Table -> Get Error
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Table
decodeTable
    toError Int32
0x2500 Text
m = Text -> ByteString -> Error
Unprepared Text
m (ByteString -> Error) -> Get ByteString -> Get Error
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get ByteString
decodeShortBytes
    toError Int32
code Text
_   = [Char] -> Get Error
forall a. [Char] -> Get a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Get Error) -> [Char] -> Get Error
forall a b. (a -> b) -> a -> b
$ [Char]
"decode-error: unknown: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Int32 -> [Char]
forall a. Show a => a -> [Char]
show Int32
code

    bool :: Word8 -> Bool
    bool :: Word8 -> Bool
bool Word8
0 = Bool
False
    bool Word8
_ = Bool
True

decodeWriteType :: Get WriteType
decodeWriteType :: Get WriteType
decodeWriteType = Get Text
decodeString Get Text -> (Text -> Get WriteType) -> Get WriteType
forall a b. Get a -> (a -> Get b) -> Get b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Get WriteType
forall {a} {m :: * -> *}.
(Eq a, IsString a, MonadFail m, Show a) =>
a -> m WriteType
fromString
  where
    fromString :: a -> m WriteType
fromString a
"SIMPLE"          = WriteType -> m WriteType
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return WriteType
WriteSimple
    fromString a
"BATCH"           = WriteType -> m WriteType
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return WriteType
WriteBatch
    fromString a
"BATCH_LOG"       = WriteType -> m WriteType
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return WriteType
WriteBatchLog
    fromString a
"UNLOGGED_BATCH"  = WriteType -> m WriteType
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return WriteType
WriteUnloggedBatch
    fromString a
"COUNTER"         = WriteType -> m WriteType
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return WriteType
WriteCounter
    fromString a
"CAS"             = WriteType -> m WriteType
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return WriteType
WriteCas
    fromString a
"VIEW"            = WriteType -> m WriteType
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return WriteType
WriteView
    fromString a
"CDC"             = WriteType -> m WriteType
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return WriteType
WriteCdc
    fromString a
unknown           = [Char] -> m WriteType
forall a. [Char] -> m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> m WriteType) -> [Char] -> m WriteType
forall a b. (a -> b) -> a -> b
$
        [Char]
"decode: unknown write-type: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
unknown