{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-missing-fields #-}
{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-----------------------------------------------------------------
-- Autogenerated by Thrift Compiler (0.9.2)                      --
--                                                             --
-- DO NOT EDIT UNLESS YOU ARE SURE YOU KNOW WHAT YOU ARE DOING --
-----------------------------------------------------------------

module GreetingService where
import Prelude (($), (.), (>>=), (==), (++))
import qualified Prelude as P
import qualified Control.Exception as X
import qualified Control.Monad as M ( liftM, ap, when )
import Data.Functor ( (<$>) )
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Hashable as H
import qualified Data.Int as I
import qualified Data.Maybe as M (catMaybes)
import qualified Data.Text.Lazy.Encoding as E ( decodeUtf8, encodeUtf8 )
import qualified Data.Text.Lazy as LT
import qualified Data.Typeable as TY ( Typeable )
import qualified Data.HashMap.Strict as Map
import qualified Data.HashSet as Set
import qualified Data.Vector as Vector
import qualified Test.QuickCheck.Arbitrary as QC ( Arbitrary(..) )
import qualified Test.QuickCheck as QC ( elements )

import qualified Thrift as T
import qualified Thrift.Types as T
import qualified Thrift.Arbitraries as T


import Greet_Types
import qualified GreetingService_Iface as Iface
-- HELPER FUNCTIONS AND STRUCTURES --

data Greet_args = Greet_args  { greet_args_msg :: Message
  } deriving (P.Show,P.Eq,TY.Typeable)
instance H.Hashable Greet_args where
  hashWithSalt salt record = salt   `H.hashWithSalt` greet_args_msg record  
instance QC.Arbitrary Greet_args where 
  arbitrary = M.liftM Greet_args (QC.arbitrary)
  shrink obj | obj == default_Greet_args = []
             | P.otherwise = M.catMaybes
    [ if obj == default_Greet_args{greet_args_msg = greet_args_msg obj} then P.Nothing else P.Just $ default_Greet_args{greet_args_msg = greet_args_msg obj}
    ]
from_Greet_args :: Greet_args -> T.ThriftVal
from_Greet_args record = T.TStruct $ Map.fromList $ M.catMaybes
  [ (\_v8 -> P.Just (1, ("msg",from_Message _v8))) $ greet_args_msg record
  ]
write_Greet_args :: (T.Protocol p, T.Transport t) => p t -> Greet_args -> P.IO ()
write_Greet_args oprot record = T.writeVal oprot $ from_Greet_args record
encode_Greet_args :: (T.Protocol p, T.Transport t) => p t -> Greet_args -> LBS.ByteString
encode_Greet_args oprot record = T.serializeVal oprot $ from_Greet_args record
to_Greet_args :: T.ThriftVal -> Greet_args
to_Greet_args (T.TStruct fields) = Greet_args{
  greet_args_msg = P.maybe (greet_args_msg default_Greet_args) (\(_,_val10) -> (case _val10 of {T.TStruct _val11 -> (to_Message (T.TStruct _val11)); _ -> P.error "wrong type"})) (Map.lookup (1) fields)
  }
to_Greet_args _ = P.error "not a struct"
read_Greet_args :: (T.Transport t, T.Protocol p) => p t -> P.IO Greet_args
read_Greet_args iprot = to_Greet_args <$> T.readVal iprot (T.T_STRUCT typemap_Greet_args)
decode_Greet_args :: (T.Protocol p, T.Transport t) => p t -> LBS.ByteString -> Greet_args
decode_Greet_args iprot bs = to_Greet_args $ T.deserializeVal iprot (T.T_STRUCT typemap_Greet_args) bs
typemap_Greet_args :: T.TypeMap
typemap_Greet_args = Map.fromList [(1,("msg",(T.T_STRUCT typemap_Message)))]
default_Greet_args :: Greet_args
default_Greet_args = Greet_args{
  greet_args_msg = default_Message}
data Greet_result = Greet_result  { greet_result_success :: Message
  } deriving (P.Show,P.Eq,TY.Typeable)
instance H.Hashable Greet_result where
  hashWithSalt salt record = salt   `H.hashWithSalt` greet_result_success record  
instance QC.Arbitrary Greet_result where 
  arbitrary = M.liftM Greet_result (QC.arbitrary)
  shrink obj | obj == default_Greet_result = []
             | P.otherwise = M.catMaybes
    [ if obj == default_Greet_result{greet_result_success = greet_result_success obj} then P.Nothing else P.Just $ default_Greet_result{greet_result_success = greet_result_success obj}
    ]
from_Greet_result :: Greet_result -> T.ThriftVal
from_Greet_result record = T.TStruct $ Map.fromList $ M.catMaybes
  [ (\_v14 -> P.Just (0, ("success",from_Message _v14))) $ greet_result_success record
  ]
write_Greet_result :: (T.Protocol p, T.Transport t) => p t -> Greet_result -> P.IO ()
write_Greet_result oprot record = T.writeVal oprot $ from_Greet_result record
encode_Greet_result :: (T.Protocol p, T.Transport t) => p t -> Greet_result -> LBS.ByteString
encode_Greet_result oprot record = T.serializeVal oprot $ from_Greet_result record
to_Greet_result :: T.ThriftVal -> Greet_result
to_Greet_result (T.TStruct fields) = Greet_result{
  greet_result_success = P.maybe (greet_result_success default_Greet_result) (\(_,_val16) -> (case _val16 of {T.TStruct _val17 -> (to_Message (T.TStruct _val17)); _ -> P.error "wrong type"})) (Map.lookup (0) fields)
  }
to_Greet_result _ = P.error "not a struct"
read_Greet_result :: (T.Transport t, T.Protocol p) => p t -> P.IO Greet_result
read_Greet_result iprot = to_Greet_result <$> T.readVal iprot (T.T_STRUCT typemap_Greet_result)
decode_Greet_result :: (T.Protocol p, T.Transport t) => p t -> LBS.ByteString -> Greet_result
decode_Greet_result iprot bs = to_Greet_result $ T.deserializeVal iprot (T.T_STRUCT typemap_Greet_result) bs
typemap_Greet_result :: T.TypeMap
typemap_Greet_result = Map.fromList [(0,("success",(T.T_STRUCT typemap_Message)))]
default_Greet_result :: Greet_result
default_Greet_result = Greet_result{
  greet_result_success = default_Message}
process_greet (seqid, iprot, oprot, handler) = do
  args <- read_Greet_args iprot
  (X.catch
    (do
      val <- Iface.greet handler (greet_args_msg args)
      let res = default_Greet_result{greet_result_success = val}
      T.writeMessageBegin oprot ("greet", T.M_REPLY, seqid)
      write_Greet_result oprot res
      T.writeMessageEnd oprot
      T.tFlush (T.getTransport oprot))
    ((\_ -> do
      T.writeMessageBegin oprot ("greet", T.M_EXCEPTION, seqid)
      T.writeAppExn oprot (T.AppExn T.AE_UNKNOWN "")
      T.writeMessageEnd oprot
      T.tFlush (T.getTransport oprot)) :: X.SomeException -> P.IO ()))
proc_ handler (iprot,oprot) (name,typ,seqid) = case name of
  "greet" -> process_greet (seqid,iprot,oprot,handler)
  _ -> do
    _ <- T.readVal iprot (T.T_STRUCT Map.empty)
    T.writeMessageBegin oprot (name,T.M_EXCEPTION,seqid)
    T.writeAppExn oprot (T.AppExn T.AE_UNKNOWN_METHOD ("Unknown function " ++ LT.unpack name))
    T.writeMessageEnd oprot
    T.tFlush (T.getTransport oprot)
process handler (iprot, oprot) = do
  (name, typ, seqid) <- T.readMessageBegin iprot
  proc_ handler (iprot,oprot) (name,typ,seqid)
  T.readMessageEnd iprot
  P.return P.True
