{-# 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
--                                                             --
-- DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
--  @generated
-----------------------------------------------------------------

module Module0_Types where
import Prelude ( Bool(..), Enum, Float, IO, Double, String, Maybe(..),
                 Eq, Show, Ord,
                 concat, error, fromIntegral, fromEnum, length, map,
                 maybe, not, null, otherwise, return, show, toEnum,
                 enumFromTo, Bounded, minBound, maxBound, seq, succ,
                 pred, enumFrom, enumFromThen, enumFromThenTo,
                 (.), (&&), (||), (==), (++), ($), (-), (>>=), (>>))

import qualified Control.Applicative as Applicative (ZipList(..))
import Control.Applicative ( (<*>) )
import qualified Control.DeepSeq as DeepSeq
import qualified Control.Exception as Exception
import qualified Control.Monad as Monad ( liftM, ap, when )
import qualified Data.ByteString.Lazy as BS
import Data.Functor ( (<$>) )
import qualified Data.Hashable as Hashable
import qualified Data.Int as Int
import Data.List
import qualified Data.Maybe as Maybe (catMaybes)
import qualified Data.Text.Lazy.Encoding as Encoding ( decodeUtf8, encodeUtf8 )
import qualified Data.Text.Lazy as LT
import qualified Data.Typeable as Typeable ( 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 Arbitrary ( Arbitrary(..) )
import qualified Test.QuickCheck as QuickCheck ( elements )

import qualified Thrift
import qualified Thrift.Types as Types
import qualified Thrift.Serializable as Serializable
import qualified Thrift.Arbitraries as Arbitraries


data Enum = ONE|TWO|THREE  deriving (Show,Eq, Typeable.Typeable, Ord, Bounded)
instance Enum Enum where
  fromEnum t = case t of
    ONE -> 1
    TWO -> 2
    THREE -> 3
  toEnum t = case t of
    1 -> ONE
    2 -> TWO
    3 -> THREE
    _ -> Exception.throw Thrift.ThriftException
  succ t = case t of
    ONE -> TWO
    TWO -> THREE
    THREE -> Exception.throw Thrift.ThriftException
  pred t = case t of
    ONE -> Exception.throw Thrift.ThriftException
    TWO -> ONE
    THREE -> TWO
  enumFrom x = enumFromTo x maxBound
  enumFromTo x y = takeUpToInc y $ iterate succ x
    where
    takeUpToInc _ [] = []
    takeUpToInc m (x:_) | m == x = [x]
    takeUpToInc m (x:xs) | otherwise = x : takeUpToInc m xs
  enumFromThen _ _ = Exception.throw Thrift.ThriftException
  enumFromThenTo _ _ _ = Exception.throw Thrift.ThriftException
instance Hashable.Hashable Enum where
  hashWithSalt salt = Hashable.hashWithSalt salt . fromEnum
instance DeepSeq.NFData Enum where
  rnf x = x `seq` ()
instance Arbitrary.Arbitrary Enum where
  arbitrary = QuickCheck.elements (enumFromTo minBound maxBound)
data Struct = Struct
  { struct_first :: Int.Int32
  , struct_second :: LT.Text
  } deriving (Show,Eq,Typeable.Typeable)
instance Serializable.ThriftSerializable Struct where
  encode = encode_Struct
  decode = decode_Struct
instance Hashable.Hashable Struct where
  hashWithSalt salt record = salt   `Hashable.hashWithSalt` struct_first record   `Hashable.hashWithSalt` struct_second record  
instance DeepSeq.NFData Struct where
  rnf _record0 =
    DeepSeq.rnf (struct_first _record0) `seq`
    DeepSeq.rnf (struct_second _record0) `seq`
    ()
instance Arbitrary.Arbitrary Struct where 
  arbitrary = Monad.liftM Struct (Arbitrary.arbitrary)
          `Monad.ap`(Arbitrary.arbitrary)
  shrink obj | obj == default_Struct = []
             | otherwise = Maybe.catMaybes
    [ if obj == default_Struct{struct_first = struct_first obj} then Nothing else Just $ default_Struct{struct_first = struct_first obj}
    , if obj == default_Struct{struct_second = struct_second obj} then Nothing else Just $ default_Struct{struct_second = struct_second obj}
    ]
from_Struct :: Struct -> Types.ThriftVal
from_Struct record = Types.TStruct $ Map.fromList $ Maybe.catMaybes
  [ (\_v3 -> Just (1, ("first",Types.TI32 _v3))) $ struct_first record
  , (\_v3 -> Just (2, ("second",Types.TString $ Encoding.encodeUtf8 _v3))) $ struct_second record
  ]
write_Struct :: (Thrift.Protocol p, Thrift.Transport t) => p t -> Struct -> IO ()
write_Struct oprot record = Thrift.writeVal oprot $ from_Struct record
encode_Struct :: (Thrift.Protocol p, Thrift.Transport t) => p t -> Struct -> BS.ByteString
encode_Struct oprot record = Thrift.serializeVal oprot $ from_Struct record
to_Struct :: Types.ThriftVal -> Struct
to_Struct (Types.TStruct fields) = Struct{
  struct_first = maybe (struct_first default_Struct) (\(_,_val5) -> (case _val5 of {Types.TI32 _val6 -> _val6; _ -> error "wrong type"})) (Map.lookup (1) fields),
  struct_second = maybe (struct_second default_Struct) (\(_,_val5) -> (case _val5 of {Types.TString _val7 -> Encoding.decodeUtf8 _val7; _ -> error "wrong type"})) (Map.lookup (2) fields)
  }
to_Struct _ = error "not a struct"
read_Struct :: (Thrift.Transport t, Thrift.Protocol p) => p t -> IO Struct
read_Struct iprot = to_Struct <$> Thrift.readVal iprot (Types.T_STRUCT typemap_Struct)
decode_Struct :: (Thrift.Protocol p, Thrift.Transport t) => p t -> BS.ByteString -> Struct
decode_Struct iprot bs = to_Struct $ Thrift.deserializeVal iprot (Types.T_STRUCT typemap_Struct) bs
typemap_Struct :: Types.TypeMap
typemap_Struct = Map.fromList [("first",(1,Types.T_I32)),("second",(2,Types.T_STRING))]
default_Struct :: Struct
default_Struct = Struct{
  struct_first = 0,
  struct_second = ""}
