module Language.Thrift.Internal.Types
(
Program(..)
, headers
, definitions
, Header(..)
, _Include
, _Namespace
, Include(..)
, path
, Namespace(..)
, language
, Definition(..)
, _Const
, _Service
, _Type
, Const(..)
, Service(..)
, functions
, extends
, Type(..)
, _Typedef
, _Enum
, _Struct
, _Union
, _Exception
, _Senum
, Typedef(..)
, targetType
, Enum(..)
, Struct(..)
, Union(..)
, Exception(..)
, Senum(..)
, FieldRequiredness(..)
, _Required
, _Optional
, Field(..)
, identifier
, requiredness
, defaultValue
, EnumDef(..)
, ConstValue(..)
, _ConstInt
, _ConstFloat
, _ConstLiteral
, _ConstIdentifier
, _ConstList
, _ConstMap
, TypeReference(..)
, _DefinedType
, _StringType
, _BinaryType
, _SListType
, _BoolType
, _ByteType
, _I16Type
, _I32Type
, _I64Type
, _DoubleType
, _MapType
, _SetType
, _ListType
, Function(..)
, oneWay
, returnType
, parameters
, exceptions
, TypeAnnotation(..)
, Docstring
, HasAnnotations(..)
, HasDocstring(..)
, HasFields(..)
, HasName(..)
, HasSrcAnnot(..)
, HasValue(..)
, HasValues(..)
, HasValueType(..)
) where
import Data.Data (Data, Typeable)
import Data.Text (Text)
import GHC.Generics (Generic)
import Prelude hiding (Enum)
import Language.Thrift.Internal.TH
import qualified Control.Lens as L
class HasSrcAnnot t where
srcAnnot :: L.Lens' (t a) a
class HasName t where
name :: L.Lens' t Text
data TypeAnnotation = TypeAnnotation
{ typeAnnotationName :: Text
, typeAnnotationValue :: Maybe Text
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
makeFieldsFor ["typeAnnotationValue"] ''TypeAnnotation
instance HasName TypeAnnotation where
name = $(accessorLens 'typeAnnotationName)
class HasAnnotations t where
annotations :: L.Lens' t [TypeAnnotation]
type Docstring = Maybe Text
class HasDocstring t where
docstring :: L.Lens' t Docstring
data ConstValue srcAnnot
= ConstInt Integer srcAnnot
| ConstFloat Double srcAnnot
| ConstLiteral Text srcAnnot
| ConstIdentifier Text srcAnnot
| ConstList [ConstValue srcAnnot] srcAnnot
| ConstMap [(ConstValue srcAnnot, ConstValue srcAnnot)] srcAnnot
deriving (Show, Ord, Eq, Data, Typeable, Generic)
L.makePrisms ''ConstValue
instance HasSrcAnnot ConstValue where
srcAnnot = L.lens getter setter
where
getter (ConstInt _ a) = a
getter (ConstFloat _ a) = a
getter (ConstLiteral _ a) = a
getter (ConstIdentifier _ a) = a
getter (ConstList _ a) = a
getter (ConstMap _ a) = a
setter (ConstInt x _) a = ConstInt x a
setter (ConstFloat x _) a = ConstFloat x a
setter (ConstLiteral x _) a = ConstLiteral x a
setter (ConstIdentifier x _) a = ConstIdentifier x a
setter (ConstList x _) a = ConstList x a
setter (ConstMap x _) a = ConstMap x a
data TypeReference srcAnnot
= DefinedType Text srcAnnot
| StringType [TypeAnnotation] srcAnnot
| BinaryType [TypeAnnotation] srcAnnot
| SListType [TypeAnnotation] srcAnnot
| BoolType [TypeAnnotation] srcAnnot
| ByteType [TypeAnnotation] srcAnnot
| I16Type [TypeAnnotation] srcAnnot
| I32Type [TypeAnnotation] srcAnnot
| I64Type [TypeAnnotation] srcAnnot
| DoubleType [TypeAnnotation] srcAnnot
| MapType
(TypeReference srcAnnot)
(TypeReference srcAnnot)
[TypeAnnotation]
srcAnnot
| SetType (TypeReference srcAnnot) [TypeAnnotation] srcAnnot
| ListType (TypeReference srcAnnot) [TypeAnnotation] srcAnnot
deriving (Show, Ord, Eq, Data, Typeable, Generic)
L.makePrisms ''TypeReference
instance HasSrcAnnot TypeReference where
srcAnnot = L.lens getter setter
where
getter (DefinedType _ a) = a
getter (StringType _ a) = a
getter (BinaryType _ a) = a
getter (SListType _ a) = a
getter (BoolType _ a) = a
getter (ByteType _ a) = a
getter (I16Type _ a) = a
getter (I32Type _ a) = a
getter (I64Type _ a) = a
getter (DoubleType _ a) = a
getter (MapType _ _ _ a) = a
getter (SetType _ _ a) = a
getter (ListType _ _ a) = a
setter (DefinedType x _) a = DefinedType x a
setter (StringType x _) a = StringType x a
setter (BinaryType x _) a = BinaryType x a
setter (SListType x _) a = SListType x a
setter (BoolType x _) a = BoolType x a
setter (ByteType x _) a = ByteType x a
setter (I16Type x _) a = I16Type x a
setter (I32Type x _) a = I32Type x a
setter (I64Type x _) a = I64Type x a
setter (DoubleType x _) a = DoubleType x a
setter (MapType k v x _) a = MapType k v x a
setter (SetType t x _) a = SetType t x a
setter (ListType t x _) a = ListType t x a
class HasValueType t where
valueType :: L.Lens' (t a) (TypeReference a)
data FieldRequiredness
= Required
| Optional
deriving (Show, Ord, Eq, Data, Typeable, Generic)
L.makePrisms ''FieldRequiredness
data Field srcAnnot = Field
{ fieldIdentifier :: Maybe Integer
, fieldRequiredness :: Maybe FieldRequiredness
, fieldValueType :: TypeReference srcAnnot
, fieldName :: Text
, fieldDefaultValue :: Maybe (ConstValue srcAnnot)
, fieldAnnotations :: [TypeAnnotation]
, fieldDocstring :: Docstring
, fieldSrcAnnot :: srcAnnot
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
L.makeLensesFor
[ ("fieldIdentifier", "identifier")
, ("fieldRequiredness", "requiredness")
, ("fieldDefaultValue", "defaultValue")
] ''Field
instance HasName (Field a) where
name = $(accessorLens 'fieldName)
instance HasValueType Field where
valueType = $(accessorLens 'fieldValueType)
instance HasSrcAnnot Field where
srcAnnot = $(accessorLens 'fieldSrcAnnot)
instance HasDocstring (Field a) where
docstring = $(accessorLens 'fieldDocstring)
instance HasAnnotations (Field a) where
annotations = $(accessorLens 'fieldAnnotations)
class HasFields t where
fields :: L.Lens' (t a) [Field a]
data Function srcAnnot = Function
{ functionOneWay :: Bool
, functionReturnType :: Maybe (TypeReference srcAnnot)
, functionName :: Text
, functionParameters :: [Field srcAnnot]
, functionExceptions :: Maybe [Field srcAnnot]
, functionAnnotations :: [TypeAnnotation]
, functionDocstring :: Docstring
, functionSrcAnnot :: srcAnnot
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
L.makeLensesFor
[ ("functionOneWay", "oneWay")
, ("functionReturnType", "returnType")
, ("functionParameters", "parameters")
, ("functionExceptions", "exceptions")
] ''Function
instance HasName (Function a) where
name = $(accessorLens 'functionName)
instance HasSrcAnnot Function where
srcAnnot = $(accessorLens 'functionSrcAnnot)
instance HasDocstring (Function a) where
docstring = $(accessorLens 'functionDocstring)
instance HasAnnotations (Function a) where
annotations = $(accessorLens 'functionAnnotations)
data Service srcAnnot = Service
{ serviceName :: Text
, serviceExtends :: Maybe Text
, serviceFunctions :: [Function srcAnnot]
, serviceAnnotations :: [TypeAnnotation]
, serviceDocstring :: Docstring
, serviceSrcAnnot :: srcAnnot
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
L.makeLensesFor
[ ("serviceExtends", "extends")
, ("serviceFunctions", "functions")
] ''Service
instance HasName (Service a) where
name = $(accessorLens 'serviceName)
instance HasSrcAnnot Service where
srcAnnot = $(accessorLens 'serviceSrcAnnot)
instance HasDocstring (Service a) where
docstring = $(accessorLens 'serviceDocstring)
instance HasAnnotations (Service a) where
annotations = $(accessorLens 'serviceAnnotations)
data Const srcAnnot = Const
{ constValueType :: TypeReference srcAnnot
, constName :: Text
, constValue :: ConstValue srcAnnot
, constDocstring :: Docstring
, constSrcAnnot :: srcAnnot
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
makeFieldsFor ["constValue"] ''Const
instance HasName (Const a) where
name = $(accessorLens 'constName)
instance HasSrcAnnot Const where
srcAnnot = $(accessorLens 'constSrcAnnot)
instance HasValueType Const where
valueType = $(accessorLens 'constValueType)
instance HasDocstring (Const a) where
docstring = $(accessorLens 'constDocstring)
data Typedef srcAnnot = Typedef
{ typedefTargetType :: TypeReference srcAnnot
, typedefName :: Text
, typedefAnnotations :: [TypeAnnotation]
, typedefDocstring :: Docstring
, typedefSrcAnnot :: srcAnnot
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
L.makeLensesFor [("typedefTargetType", "targetType")] ''Typedef
instance HasName (Typedef a) where
name = $(accessorLens 'typedefName)
instance HasSrcAnnot Typedef where
srcAnnot = $(accessorLens 'typedefSrcAnnot)
instance HasDocstring (Typedef a) where
docstring = $(accessorLens 'typedefDocstring)
instance HasAnnotations (Typedef a) where
annotations = $(accessorLens 'typedefAnnotations)
data EnumDef srcAnnot = EnumDef
{ enumDefName :: Text
, enumDefValue :: Maybe Integer
, enumDefAnnotations :: [TypeAnnotation]
, enumDefDocstring :: Docstring
, enumDefSrcAnnot :: srcAnnot
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
makeFieldsFor ["enumDefValue"] ''EnumDef
instance HasName (EnumDef a) where
name = $(accessorLens 'enumDefName)
instance HasSrcAnnot EnumDef where
srcAnnot = $(accessorLens 'enumDefSrcAnnot)
instance HasDocstring (EnumDef a) where
docstring = $(accessorLens 'enumDefDocstring)
instance HasAnnotations (EnumDef a) where
annotations = $(accessorLens 'enumDefAnnotations)
data Enum srcAnnot = Enum
{ enumName :: Text
, enumValues :: [EnumDef srcAnnot]
, enumAnnotations :: [TypeAnnotation]
, enumDocstring :: Docstring
, enumSrcAnnot :: srcAnnot
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
makeFieldsFor ["enumValues"] ''Enum
instance HasName (Enum a) where
name = $(accessorLens 'enumName)
instance HasSrcAnnot Enum where
srcAnnot = $(accessorLens 'enumSrcAnnot)
instance HasDocstring (Enum a) where
docstring = $(accessorLens 'enumDocstring)
instance HasAnnotations (Enum a) where
annotations = $(accessorLens 'enumAnnotations)
data Struct srcAnnot = Struct
{ structName :: Text
, structFields :: [Field srcAnnot]
, structAnnotations :: [TypeAnnotation]
, structDocstring :: Docstring
, structSrcAnnot :: srcAnnot
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
instance HasName (Struct a) where
name = $(accessorLens 'structName)
instance HasFields Struct where
fields = $(accessorLens 'structFields)
instance HasSrcAnnot Struct where
srcAnnot = $(accessorLens 'structSrcAnnot)
instance HasDocstring (Struct a) where
docstring = $(accessorLens 'structDocstring)
instance HasAnnotations (Struct a) where
annotations = $(accessorLens 'structAnnotations)
data Union srcAnnot = Union
{ unionName :: Text
, unionFields :: [Field srcAnnot]
, unionAnnotations :: [TypeAnnotation]
, unionDocstring :: Docstring
, unionSrcAnnot :: srcAnnot
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
instance HasName (Union a) where
name = $(accessorLens 'unionName)
instance HasFields Union where
fields = $(accessorLens 'unionFields)
instance HasSrcAnnot Union where
srcAnnot = $(accessorLens 'unionSrcAnnot)
instance HasDocstring (Union a) where
docstring = $(accessorLens 'unionDocstring)
instance HasAnnotations (Union a) where
annotations = $(accessorLens 'unionAnnotations)
data Exception srcAnnot = Exception
{ exceptionName :: Text
, exceptionFields :: [Field srcAnnot]
, exceptionAnnotations :: [TypeAnnotation]
, exceptionDocstring :: Docstring
, exceptionSrcAnnot :: srcAnnot
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
instance HasName (Exception a) where
name = $(accessorLens 'exceptionName)
instance HasFields Exception where
fields = $(accessorLens 'exceptionFields)
instance HasSrcAnnot Exception where
srcAnnot = $(accessorLens 'exceptionSrcAnnot)
instance HasDocstring (Exception a) where
docstring = $(accessorLens 'exceptionDocstring)
instance HasAnnotations (Exception a) where
annotations = $(accessorLens 'exceptionAnnotations)
data Senum srcAnnot = Senum
{ senumName :: Text
, senumValues :: [Text]
, senumAnnotations :: [TypeAnnotation]
, senumDocstring :: Docstring
, senumSrcAnnot :: srcAnnot
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
makeFieldsFor ["senumValues"] ''Senum
instance HasName (Senum a) where
name = $(accessorLens 'senumName)
instance HasSrcAnnot Senum where
srcAnnot = $(accessorLens 'senumSrcAnnot)
instance HasDocstring (Senum a) where
docstring = $(accessorLens 'senumDocstring)
instance HasAnnotations (Senum a) where
annotations = $(accessorLens 'senumAnnotations)
data Type srcAnnot
=
TypedefType (Typedef srcAnnot)
|
EnumType (Enum srcAnnot)
|
StructType (Struct srcAnnot)
|
UnionType (Union srcAnnot)
|
ExceptionType (Exception srcAnnot)
|
SenumType (Senum srcAnnot)
deriving (Show, Ord, Eq, Data, Typeable, Generic)
instance HasName (Type a) where
name = L.lens getter setter
where
getter (TypedefType t) = L.view name t
getter (EnumType t) = L.view name t
getter (StructType t) = L.view name t
getter (UnionType t) = L.view name t
getter (ExceptionType t) = L.view name t
getter (SenumType t) = L.view name t
setter (TypedefType t) n = TypedefType $ L.set name n t
setter (EnumType t) n = EnumType $ L.set name n t
setter (StructType t) n = StructType $ L.set name n t
setter (UnionType t) n = UnionType $ L.set name n t
setter (ExceptionType t) n = ExceptionType $ L.set name n t
setter (SenumType t) n = SenumType $ L.set name n t
instance HasSrcAnnot Type where
srcAnnot = L.lens getter setter
where
getter (TypedefType t) = L.view srcAnnot t
getter (EnumType t) = L.view srcAnnot t
getter (StructType t) = L.view srcAnnot t
getter (UnionType t) = L.view srcAnnot t
getter (ExceptionType t) = L.view srcAnnot t
getter (SenumType t) = L.view srcAnnot t
setter (TypedefType t) a = TypedefType $ L.set srcAnnot a t
setter (EnumType t) a = EnumType $ L.set srcAnnot a t
setter (StructType t) a = StructType $ L.set srcAnnot a t
setter (UnionType t) a = UnionType $ L.set srcAnnot a t
setter (ExceptionType t) a = ExceptionType $ L.set srcAnnot a t
setter (SenumType t) a = SenumType $ L.set srcAnnot a t
_Typedef :: L.Prism' (Type ann) (Typedef ann)
_Typedef = L.prism' TypedefType $ \t ->
case t of
TypedefType a -> Just a
_ -> Nothing
_Enum :: L.Prism' (Type ann) (Enum ann)
_Enum = L.prism' EnumType $ \t ->
case t of
EnumType a -> Just a
_ -> Nothing
_Struct :: L.Prism' (Type ann) (Struct ann)
_Struct = L.prism' StructType $ \t ->
case t of
StructType a -> Just a
_ -> Nothing
_Union :: L.Prism' (Type ann) (Union ann)
_Union = L.prism' UnionType $ \t ->
case t of
UnionType a -> Just a
_ -> Nothing
_Exception :: L.Prism' (Type ann) (Exception ann)
_Exception = L.prism' ExceptionType $ \t ->
case t of
ExceptionType a -> Just a
_ -> Nothing
_Senum :: L.Prism' (Type ann) (Senum ann)
_Senum = L.prism' SenumType $ \t ->
case t of
SenumType a -> Just a
_ -> Nothing
data Definition srcAnnot
=
ConstDefinition (Const srcAnnot)
|
TypeDefinition (Type srcAnnot)
|
ServiceDefinition (Service srcAnnot)
deriving (Show, Ord, Eq, Data, Typeable, Generic)
instance HasName (Definition a) where
name = L.lens getter setter
where
getter (ConstDefinition d) = L.view name d
getter (TypeDefinition d) = L.view name d
getter (ServiceDefinition d) = L.view name d
setter (ConstDefinition d) n = ConstDefinition $ L.set name n d
setter (TypeDefinition d) n = TypeDefinition $ L.set name n d
setter (ServiceDefinition d) n = ServiceDefinition $ L.set name n d
instance HasSrcAnnot Definition where
srcAnnot = L.lens getter setter
where
getter (ConstDefinition d) = L.view srcAnnot d
getter (TypeDefinition d) = L.view srcAnnot d
getter (ServiceDefinition d) = L.view srcAnnot d
setter (ConstDefinition d) a = ConstDefinition $ L.set srcAnnot a d
setter (TypeDefinition d) a = TypeDefinition $ L.set srcAnnot a d
setter (ServiceDefinition d) a = ServiceDefinition $ L.set srcAnnot a d
_Const :: L.Prism' (Definition ann) (Const ann)
_Const = L.prism' ConstDefinition $ \def ->
case def of
ConstDefinition c -> Just c
_ -> Nothing
_Type :: L.Prism' (Definition ann) (Type ann)
_Type = L.prism' TypeDefinition $ \def ->
case def of
TypeDefinition c -> Just c
_ -> Nothing
_Service :: L.Prism' (Definition ann) (Service ann)
_Service = L.prism' ServiceDefinition $ \def ->
case def of
ServiceDefinition c -> Just c
_ -> Nothing
data Namespace srcAnnot = Namespace
{ namespaceLanguage :: Text
, namespaceName :: Text
, namespaceSrcAnnot :: srcAnnot
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
L.makeLensesFor [("namespaceLanguage", "language")] ''Namespace
instance HasName (Namespace a) where
name = $(accessorLens 'namespaceName)
instance HasSrcAnnot Namespace where
srcAnnot = $(accessorLens 'namespaceSrcAnnot)
data Include srcAnnot = Include
{ includePath :: Text
, includeSrcAnnot :: srcAnnot
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
L.makeLensesFor [("includePath", "path")] ''Include
instance HasSrcAnnot Include where
srcAnnot = $(accessorLens 'includeSrcAnnot)
data Header srcAnnot
=
HeaderInclude (Include srcAnnot)
|
HeaderNamespace (Namespace srcAnnot)
deriving (Show, Ord, Eq, Data, Typeable, Generic)
_Include :: L.Prism' (Header ann) (Include ann)
_Include = L.prism' HeaderInclude $ \h ->
case h of
HeaderInclude inc -> Just inc
_ -> Nothing
_Namespace :: L.Prism' (Header ann) (Namespace ann)
_Namespace = L.prism' HeaderNamespace $ \h ->
case h of
HeaderNamespace ns -> Just ns
_ -> Nothing
data Program srcAnnot = Program
{ programHeaders :: [Header srcAnnot]
, programDefinitions :: [Definition srcAnnot]
}
deriving (Show, Ord, Eq, Data, Typeable, Generic)
L.makeLensesFor
[ ("programHeaders", "headers")
, ("programDefinitions", "definitions")
] ''Program