| Copyright | (c) Abhinav Gupta 2016 |
|---|---|
| License | BSD3 |
| Maintainer | Abhinav Gupta <mail@abhinavg.net> |
| Stability | experimental |
| Safe Haskell | Safe |
| Language | Haskell2010 |
Language.Thrift.AST
Contents
Description
This module defines types that compose a Thrift IDL file.
Most of the types have an optional srcAnnot parameter that represents a
source annotation. The parser produces types annotated with their position
in the Thrift file (SourcePos). When constructing the AST
by hand, you can use (). The types are Functors so you can use fmap
to change the annotation on all objects in a tree.
Lenses for attributes of most types are provided for use with the lens
library.
Types representing the AST all have Pretty
instances to go with them.
- data Program srcAnnot = Program {
- programHeaders :: [Header srcAnnot]
- programDefinitions :: [Definition srcAnnot]
- headers :: Lens (Program a) [Header a]
- definitions :: Lens (Program a) [Definition a]
- data Header srcAnnot
- = HeaderInclude (Include srcAnnot)
- | HeaderNamespace (Namespace srcAnnot)
- data Include srcAnnot = Include {
- includePath :: Text
- includeSrcAnnot :: srcAnnot
- path :: Lens (Include a) Text
- data Namespace srcAnnot = Namespace {
- namespaceLanguage :: Text
- namespaceName :: Text
- namespaceSrcAnnot :: srcAnnot
- language :: Lens (Namespace a) Text
- data Definition srcAnnot
- = ConstDefinition (Const srcAnnot)
- | TypeDefinition (Type srcAnnot)
- | ServiceDefinition (Service srcAnnot)
- data Const srcAnnot = Const {
- constValueType :: TypeReference srcAnnot
- constName :: Text
- constValue :: ConstValue srcAnnot
- constDocstring :: Docstring
- constSrcAnnot :: srcAnnot
- data Service srcAnnot = Service {
- serviceName :: Text
- serviceExtends :: Maybe Text
- serviceFunctions :: [Function srcAnnot]
- serviceAnnotations :: [TypeAnnotation]
- serviceDocstring :: Docstring
- serviceSrcAnnot :: srcAnnot
- functions :: Lens (Service a) [Function a]
- extends :: Lens (Service a) (Maybe Text)
- data Type srcAnnot
- = TypedefType (Typedef srcAnnot)
- | EnumType (Enum srcAnnot)
- | StructType (Struct srcAnnot)
- | SenumType (Senum srcAnnot)
- data Typedef srcAnnot = Typedef {
- typedefTargetType :: TypeReference srcAnnot
- typedefName :: Text
- typedefAnnotations :: [TypeAnnotation]
- typedefDocstring :: Docstring
- typedefSrcAnnot :: srcAnnot
- targetType :: Lens (Typedef a) (TypeReference a)
- data Enum srcAnnot = Enum {
- enumName :: Text
- enumValues :: [EnumDef srcAnnot]
- enumAnnotations :: [TypeAnnotation]
- enumDocstring :: Docstring
- enumSrcAnnot :: srcAnnot
- data StructKind
- data Struct srcAnnot = Struct {
- structKind :: StructKind
- structName :: Text
- structFields :: [Field srcAnnot]
- structAnnotations :: [TypeAnnotation]
- structDocstring :: Docstring
- structSrcAnnot :: srcAnnot
- kind :: Lens (Struct a) StructKind
- type Union = Struct
- unionName :: Union a -> Text
- unionFields :: Union a -> [Field a]
- unionAnnotations :: Union a -> [TypeAnnotation]
- unionDocstring :: Union a -> Docstring
- unionSrcAnnot :: Union a -> a
- type Exception = Struct
- exceptionName :: Exception a -> Text
- exceptionFields :: Exception a -> [Field a]
- exceptionAnnotations :: Exception a -> [TypeAnnotation]
- exceptionDocstring :: Exception a -> Docstring
- exceptionSrcAnnot :: Exception a -> a
- data Senum srcAnnot = Senum {
- senumName :: Text
- senumValues :: [Text]
- senumAnnotations :: [TypeAnnotation]
- senumDocstring :: Docstring
- senumSrcAnnot :: srcAnnot
- data 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
- identifier :: Lens (Field a) (Maybe Integer)
- requiredness :: Lens (Field a) (Maybe FieldRequiredness)
- defaultValue :: Lens (Field a) (Maybe (ConstValue a))
- data EnumDef srcAnnot = EnumDef {
- enumDefName :: Text
- enumDefValue :: Maybe Integer
- enumDefAnnotations :: [TypeAnnotation]
- enumDefDocstring :: Docstring
- enumDefSrcAnnot :: srcAnnot
- 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
- 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
- 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
- oneWay :: Lens (Function a) Bool
- returnType :: Lens (Function a) (Maybe (TypeReference a))
- parameters :: Lens (Function a) [Field a]
- exceptions :: Lens (Function a) (Maybe [Field a])
- data TypeAnnotation = TypeAnnotation {}
- type Docstring = Maybe Text
- class HasAnnotations t where
- annotations :: Lens t [TypeAnnotation]
- class HasDocstring t where
- class HasFields t where
- class HasName t where
- class HasSrcAnnot t where
- srcAnnot :: Lens (t a) a
- class HasValue s a | s -> a where
- value :: Lens s a
- class HasValues s a | s -> a where
- values :: Lens s a
- class HasValueType t where
- valueType :: Lens (t a) (TypeReference a)
AST
A program represents a single Thrift document.
Constructors
| Program | |
Fields
| |
definitions :: Lens (Program a) [Definition a] Source
Headers for a program.
Constructors
| HeaderInclude (Include srcAnnot) | Request to include another Thrift file. |
| HeaderNamespace (Namespace srcAnnot) | A |
The IDL includes another Thrift file.
include "common.thrift" typedef common.Foo Bar
Constructors
| Include | |
Fields
| |
data Namespace srcAnnot Source
Namespace directives allows control of the namespace or package name used by the generated code for certain languages.
namespace py my_service.generated
Constructors
| Namespace | |
Fields
| |
data Definition srcAnnot Source
A definition either consists of new constants, new types, or new services.
Constructors
| ConstDefinition (Const srcAnnot) | A declared constant. |
| TypeDefinition (Type srcAnnot) | A custom type. |
| ServiceDefinition (Service srcAnnot) | A service definition. |
Instances
| Functor Definition | |
| HasSrcAnnot Definition | |
| Eq srcAnnot => Eq (Definition srcAnnot) | |
| Data srcAnnot => Data (Definition srcAnnot) | |
| Ord srcAnnot => Ord (Definition srcAnnot) | |
| Show srcAnnot => Show (Definition srcAnnot) | |
| Generic (Definition srcAnnot) | |
| HasName (Definition a) | |
| type Rep (Definition srcAnnot) |
A declared constant.
const i32 code = 1;
Constructors
| Const | |
Fields
| |
Instances
| Functor Const | |
| HasValueType Const | |
| HasSrcAnnot Const | |
| Eq srcAnnot => Eq (Const srcAnnot) | |
| Data srcAnnot => Data (Const srcAnnot) | |
| Ord srcAnnot => Ord (Const srcAnnot) | |
| Show srcAnnot => Show (Const srcAnnot) | |
| Generic (Const srcAnnot) | |
| HasDocstring (Const a) | |
| HasName (Const a) | |
| HasValue (Const a) (ConstValue a) | |
| type Rep (Const srcAnnot) |
A service definition.
service MyService {
// ...
}Constructors
| Service | |
Fields
| |
Instances
| Functor Service | |
| HasSrcAnnot Service | |
| Eq srcAnnot => Eq (Service srcAnnot) | |
| Data srcAnnot => Data (Service srcAnnot) | |
| Ord srcAnnot => Ord (Service srcAnnot) | |
| Show srcAnnot => Show (Service srcAnnot) | |
| Generic (Service srcAnnot) | |
| HasDocstring (Service a) | |
| HasAnnotations (Service a) | |
| HasName (Service a) | |
| type Rep (Service srcAnnot) |
Defines the various types that can be declared in Thrift.
Constructors
| TypedefType (Typedef srcAnnot) | typedef |
| EnumType (Enum srcAnnot) | enum |
| StructType (Struct srcAnnot) |
|
| SenumType (Senum srcAnnot) | senum |
A typedef is just an alias for another type.
typedef common.Foo Bar
Constructors
| Typedef | |
Fields
| |
Instances
| Functor Typedef | |
| HasSrcAnnot Typedef | |
| Eq srcAnnot => Eq (Typedef srcAnnot) | |
| Data srcAnnot => Data (Typedef srcAnnot) | |
| Ord srcAnnot => Ord (Typedef srcAnnot) | |
| Show srcAnnot => Show (Typedef srcAnnot) | |
| Generic (Typedef srcAnnot) | |
| HasDocstring (Typedef a) | |
| HasAnnotations (Typedef a) | |
| HasName (Typedef a) | |
| type Rep (Typedef srcAnnot) |
targetType :: Lens (Typedef a) (TypeReference a) Source
Enums are sets of named integer values.
enum Role {
User = 1, Admin = 2
}Constructors
| Enum | |
Fields
| |
Instances
| Functor Enum | |
| HasSrcAnnot Enum | |
| Eq srcAnnot => Eq (Enum srcAnnot) | |
| Data srcAnnot => Data (Enum srcAnnot) | |
| Ord srcAnnot => Ord (Enum srcAnnot) | |
| Show srcAnnot => Show (Enum srcAnnot) | |
| Generic (Enum srcAnnot) | |
| HasDocstring (Enum a) | |
| HasAnnotations (Enum a) | |
| HasName (Enum a) | |
| HasValues (Enum a) [EnumDef a] | |
| type Rep (Enum srcAnnot) |
data StructKind Source
The kind of the struct.
Constructors
| StructKind | struct |
| UnionKind | union |
| ExceptionKind | exception |
Instances
A struct, union, or exception definition.
struct User {
1: Role role = Role.User;
}union Value {
1: string stringValue;
2: i32 intValue;
}exception UserDoesNotExist {
1: optional string message
2: required string username
}Constructors
| Struct | |
Fields
| |
Instances
| Functor Struct | |
| HasFields Struct | |
| HasSrcAnnot Struct | |
| Eq srcAnnot => Eq (Struct srcAnnot) | |
| Data srcAnnot => Data (Struct srcAnnot) | |
| Ord srcAnnot => Ord (Struct srcAnnot) | |
| Show srcAnnot => Show (Struct srcAnnot) | |
| Generic (Struct srcAnnot) | |
| HasDocstring (Struct a) | |
| HasAnnotations (Struct a) | |
| HasName (Struct a) | |
| type Rep (Struct srcAnnot) |
kind :: Lens (Struct a) StructKind Source
Deprecated: The type has been consolidated into Struct.
A union of other types.
unionFields :: Union a -> [Field a] Source
Deprecated: Use structFields.
unionAnnotations :: Union a -> [TypeAnnotation] Source
Deprecated: Use structAnnotations.
unionDocstring :: Union a -> Docstring Source
Deprecated: Use structDocstring.
unionSrcAnnot :: Union a -> a Source
Deprecated: Use structSrcAnnot.
type Exception = Struct Source
Deprecated: The type has been consolidated into Struct.
Exception types.
exceptionName :: Exception a -> Text Source
Deprecated: Use structName.
exceptionFields :: Exception a -> [Field a] Source
Deprecated: Use structFields.
exceptionAnnotations :: Exception a -> [TypeAnnotation] Source
Deprecated: Use structAnnotations.
exceptionDocstring :: Exception a -> Docstring Source
Deprecated: Use structDocstring.
exceptionSrcAnnot :: Exception a -> a Source
Deprecated: Use structSrcAnnot.
An string-only enum. These are a deprecated feature of Thrift and shouldn't be used.
Constructors
| Senum | |
Fields
| |
Instances
| Functor Senum | |
| HasSrcAnnot Senum | |
| Eq srcAnnot => Eq (Senum srcAnnot) | |
| Data srcAnnot => Data (Senum srcAnnot) | |
| Ord srcAnnot => Ord (Senum srcAnnot) | |
| Show srcAnnot => Show (Senum srcAnnot) | |
| Generic (Senum srcAnnot) | |
| HasDocstring (Senum a) | |
| HasAnnotations (Senum a) | |
| HasName (Senum a) | |
| HasValues (Senum a) [Text] | |
| type Rep (Senum srcAnnot) |
data FieldRequiredness Source
Whether a field is required or optional.
A field inside a struct, exception, or function parameters list.
Constructors
| Field | |
Fields
| |
Instances
| Functor Field | |
| HasValueType Field | |
| HasSrcAnnot Field | |
| Eq srcAnnot => Eq (Field srcAnnot) | |
| Data srcAnnot => Data (Field srcAnnot) | |
| Ord srcAnnot => Ord (Field srcAnnot) | |
| Show srcAnnot => Show (Field srcAnnot) | |
| Generic (Field srcAnnot) | |
| HasDocstring (Field a) | |
| HasAnnotations (Field a) | |
| HasName (Field a) | |
| type Rep (Field srcAnnot) |
identifier :: Lens (Field a) (Maybe Integer) Source
requiredness :: Lens (Field a) (Maybe FieldRequiredness) Source
defaultValue :: Lens (Field a) (Maybe (ConstValue a)) Source
A named value inside an enum.
Constructors
| EnumDef | |
Fields
| |
Instances
| Functor EnumDef | |
| HasSrcAnnot EnumDef | |
| Eq srcAnnot => Eq (EnumDef srcAnnot) | |
| Data srcAnnot => Data (EnumDef srcAnnot) | |
| Ord srcAnnot => Ord (EnumDef srcAnnot) | |
| Show srcAnnot => Show (EnumDef srcAnnot) | |
| Generic (EnumDef srcAnnot) | |
| HasDocstring (EnumDef a) | |
| HasAnnotations (EnumDef a) | |
| HasName (EnumDef a) | |
| HasValues (Enum a) [EnumDef a] | |
| HasValue (EnumDef a) (Maybe Integer) | |
| type Rep (EnumDef srcAnnot) |
data ConstValue srcAnnot Source
A constant literal value in the IDL. Only a few basic types, lists, and maps can be presented in Thrift files as literals.
Constants are used for IDL-level constants and default values for fields.
Constructors
| ConstInt Integer srcAnnot | An integer. |
| ConstFloat Double srcAnnot | A float. |
| ConstLiteral Text srcAnnot | A literal string. |
| ConstIdentifier Text srcAnnot | A reference to another constant. |
| ConstList [ConstValue srcAnnot] srcAnnot | A literal list containing other constant values. |
| ConstMap [(ConstValue srcAnnot, ConstValue srcAnnot)] srcAnnot | A literal list containing other constant values.
|
Instances
| Functor ConstValue | |
| HasSrcAnnot ConstValue | |
| Eq srcAnnot => Eq (ConstValue srcAnnot) | |
| Data srcAnnot => Data (ConstValue srcAnnot) | |
| Ord srcAnnot => Ord (ConstValue srcAnnot) | |
| Show srcAnnot => Show (ConstValue srcAnnot) | |
| Generic (ConstValue srcAnnot) | |
| HasValue (Const a) (ConstValue a) | |
| type Rep (ConstValue srcAnnot) |
data TypeReference srcAnnot Source
A reference to a type.
Constructors
| DefinedType Text srcAnnot | A custom defined type referred to by name. |
| 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 |
|
Instances
| Functor TypeReference | |
| HasSrcAnnot TypeReference | |
| Eq srcAnnot => Eq (TypeReference srcAnnot) | |
| Data srcAnnot => Data (TypeReference srcAnnot) | |
| Ord srcAnnot => Ord (TypeReference srcAnnot) | |
| Show srcAnnot => Show (TypeReference srcAnnot) | |
| Generic (TypeReference srcAnnot) | |
| type Rep (TypeReference srcAnnot) |
A function defined inside a service.
Constructors
| Function | |
Fields
| |
Instances
| Functor Function | |
| HasSrcAnnot Function | |
| Eq srcAnnot => Eq (Function srcAnnot) | |
| Data srcAnnot => Data (Function srcAnnot) | |
| Ord srcAnnot => Ord (Function srcAnnot) | |
| Show srcAnnot => Show (Function srcAnnot) | |
| Generic (Function srcAnnot) | |
| HasDocstring (Function a) | |
| HasAnnotations (Function a) | |
| HasName (Function a) | |
| type Rep (Function srcAnnot) |
returnType :: Lens (Function a) (Maybe (TypeReference a)) Source
parameters :: Lens (Function a) [Field a] Source
exceptions :: Lens (Function a) (Maybe [Field a]) Source
data TypeAnnotation Source
Type annoations may be added in various places in the form,
(foo = "bar", baz, qux = "quux")
These do not usually affect code generation but allow for custom logic if writing your own code generator.
Constructors
| TypeAnnotation | |
Fields
| |
type Docstring = Maybe Text Source
Docstrings are Javadoc-style comments attached various defined objects.
/** * Fetches an item. */ Item getItem()
Typeclasses
class HasAnnotations t where Source
Methods
annotations :: Lens t [TypeAnnotation] Source
Instances
| HasAnnotations (Senum a) | |
| HasAnnotations (Struct a) | |
| HasAnnotations (Enum a) | |
| HasAnnotations (EnumDef a) | |
| HasAnnotations (Typedef a) | |
| HasAnnotations (Service a) | |
| HasAnnotations (Function a) | |
| HasAnnotations (Field a) |
class HasDocstring t where Source
Instances
| HasDocstring (Senum a) | |
| HasDocstring (Struct a) | |
| HasDocstring (Enum a) | |
| HasDocstring (EnumDef a) | |
| HasDocstring (Typedef a) | |
| HasDocstring (Const a) | |
| HasDocstring (Service a) | |
| HasDocstring (Function a) | |
| HasDocstring (Field a) |
class HasSrcAnnot t where Source
Instances