language-thrift-0.10.0.0: Parser and pretty printer for the Thrift IDL format.

Copyright(c) Abhinav Gupta 2016
LicenseBSD3
MaintainerAbhinav Gupta <mail@abhinavg.net>
Stabilityexperimental
Safe HaskellSafe
LanguageHaskell2010

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.

Synopsis

AST

data Program srcAnnot Source

A program represents a single Thrift document.

Constructors

Program 

Fields

programHeaders :: [Header srcAnnot]

Headers in a document define includes and namespaces.

programDefinitions :: [Definition srcAnnot]

Types and services defined in the document.

Instances

Functor Program 
Eq srcAnnot => Eq (Program srcAnnot) 
Data srcAnnot => Data (Program srcAnnot) 
Ord srcAnnot => Ord (Program srcAnnot) 
Show srcAnnot => Show (Program srcAnnot) 
Generic (Program srcAnnot) 
type Rep (Program srcAnnot) 

headers :: Lens (Program a) [Header a] Source

data Header srcAnnot Source

Headers for a program.

Constructors

HeaderInclude (Include srcAnnot)

Request to include another Thrift file.

HeaderNamespace (Namespace srcAnnot)

A namespace specifier.

Instances

Functor Header 
Eq srcAnnot => Eq (Header srcAnnot) 
Data srcAnnot => Data (Header srcAnnot) 
Ord srcAnnot => Ord (Header srcAnnot) 
Show srcAnnot => Show (Header srcAnnot) 
Generic (Header srcAnnot) 
type Rep (Header srcAnnot) 

data Include srcAnnot Source

The IDL includes another Thrift file.

include "common.thrift"

typedef common.Foo Bar

Constructors

Include 

Fields

includePath :: Text

Path to the included file.

includeSrcAnnot :: srcAnnot
 

Instances

Functor Include 
HasSrcAnnot Include 
Eq srcAnnot => Eq (Include srcAnnot) 
Data srcAnnot => Data (Include srcAnnot) 
Ord srcAnnot => Ord (Include srcAnnot) 
Show srcAnnot => Show (Include srcAnnot) 
Generic (Include srcAnnot) 
type Rep (Include srcAnnot) 

path :: Lens (Include a) Text Source

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

namespaceLanguage :: Text

The language for which the namespace is being specified. This may be * to refer to all languages.

namespaceName :: Text

Namespace or package path to use in the generated code for that language.

namespaceSrcAnnot :: srcAnnot
 

Instances

Functor Namespace 
HasSrcAnnot Namespace 
Eq srcAnnot => Eq (Namespace srcAnnot) 
Data srcAnnot => Data (Namespace srcAnnot) 
Ord srcAnnot => Ord (Namespace srcAnnot) 
Show srcAnnot => Show (Namespace srcAnnot) 
Generic (Namespace srcAnnot) 
HasName (Namespace a) 
type Rep (Namespace srcAnnot) 

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) 

data Const srcAnnot Source

A declared constant.

const i32 code = 1;

Constructors

Const 

Fields

constValueType :: TypeReference srcAnnot

Type of the constant.

constName :: Text

Name of the constant.

constValue :: ConstValue srcAnnot

Value of the constant.

constDocstring :: Docstring

Documentation.

constSrcAnnot :: srcAnnot
 

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) 

data Service srcAnnot Source

A service definition.

service MyService {
    // ...
}

Constructors

Service 

Fields

serviceName :: Text

Name of the service.

serviceExtends :: Maybe Text

Name of the service this service extends.

serviceFunctions :: [Function srcAnnot]

All the functions defined for the service.

serviceAnnotations :: [TypeAnnotation]

Annotations added to the service.

serviceDocstring :: Docstring

Documentation.

serviceSrcAnnot :: srcAnnot
 

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) 

data Type srcAnnot Source

Defines the various types that can be declared in Thrift.

Constructors

TypedefType (Typedef srcAnnot)
typedef
EnumType (Enum srcAnnot)
enum
StructType (Struct srcAnnot)

structunionexception

SenumType (Senum srcAnnot)
senum

Instances

Functor Type 
HasSrcAnnot Type 
Eq srcAnnot => Eq (Type srcAnnot) 
Data srcAnnot => Data (Type srcAnnot) 
Ord srcAnnot => Ord (Type srcAnnot) 
Show srcAnnot => Show (Type srcAnnot) 
Generic (Type srcAnnot) 
HasName (Type a) 
type Rep (Type srcAnnot) 

data Typedef srcAnnot Source

A typedef is just an alias for another type.

typedef common.Foo Bar

Constructors

Typedef 

Fields

typedefTargetType :: TypeReference srcAnnot

The aliased type.

typedefName :: Text

Name of the new type.

typedefAnnotations :: [TypeAnnotation]

Annotations added to the typedef.

typedefDocstring :: Docstring

Documentation.

typedefSrcAnnot :: srcAnnot
 

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) 

data Enum srcAnnot Source

Enums are sets of named integer values.

enum Role {
    User = 1, Admin = 2
}

Constructors

Enum 

Fields

enumName :: Text

Name of the enum type.

enumValues :: [EnumDef srcAnnot]

Values defined in the enum.

enumAnnotations :: [TypeAnnotation]

Annotations added to the enum.

enumDocstring :: Docstring

Documentation.

enumSrcAnnot :: srcAnnot
 

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

data Struct srcAnnot Source

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

structKind :: StructKind

Kind of the structure.

structName :: Text

Name of the struct.

structFields :: [Field srcAnnot]

Fields defined in the struct.

structAnnotations :: [TypeAnnotation]

Annotations added to the struct.

structDocstring :: Docstring

Documentation.

structSrcAnnot :: srcAnnot
 

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) 

type Union = Struct Source

Deprecated: The type has been consolidated into Struct.

A union of other types.

unionName :: Union a -> Text Source

Deprecated: Use structName.

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.

data Senum srcAnnot Source

An string-only enum. These are a deprecated feature of Thrift and shouldn't be used.

Constructors

Senum 

Fields

senumName :: Text
 
senumValues :: [Text]
 
senumAnnotations :: [TypeAnnotation]

Annotations added to the senum.

senumDocstring :: Docstring

Documentation.

senumSrcAnnot :: srcAnnot
 

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.

Constructors

Required

The field is required.

Optional

The field is optional.

data Field srcAnnot Source

A field inside a struct, exception, or function parameters list.

Constructors

Field 

Fields

fieldIdentifier :: Maybe Integer

Position of the field.

While this is optional, it is recommended that Thrift files always contain specific field IDs.

fieldRequiredness :: Maybe FieldRequiredness

Whether this field is required or not.

Behavior may differ between languages if requiredness is not specified. Therefore it's recommended that requiredness for a field is always specified.

fieldValueType :: TypeReference srcAnnot

Type of value the field holds.

fieldName :: Text

Name of the field.

fieldDefaultValue :: Maybe (ConstValue srcAnnot)

Default value of the field, if any.

fieldAnnotations :: [TypeAnnotation]

Field annotations.

fieldDocstring :: Docstring

Documentation.

fieldSrcAnnot :: srcAnnot
 

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) 

data EnumDef srcAnnot Source

A named value inside an enum.

Constructors

EnumDef 

Fields

enumDefName :: Text

Name of the value.

enumDefValue :: Maybe Integer

Value attached to the enum for that name.

enumDefAnnotations :: [TypeAnnotation]

Annotations added to this enum field.

enumDefDocstring :: Docstring

Documentation

enumDefSrcAnnot :: srcAnnot
 

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. 42

ConstFloat Double srcAnnot

A float. 4.2

ConstLiteral Text srcAnnot

A literal string. "hello"

ConstIdentifier Text srcAnnot

A reference to another constant. Foo.bar

ConstList [ConstValue srcAnnot] srcAnnot

A literal list containing other constant values. [42]

ConstMap [(ConstValue srcAnnot, ConstValue srcAnnot)] srcAnnot

A literal list containing other constant values. {"hellO": 1, "world": 2}

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

string and annotations.

BinaryType [TypeAnnotation] srcAnnot

binary and annotations.

SListType [TypeAnnotation] srcAnnot

slist and annotations.

BoolType [TypeAnnotation] srcAnnot

bool and annotations.

ByteType [TypeAnnotation] srcAnnot

byte and annotations.

I16Type [TypeAnnotation] srcAnnot

i16 and annotations.

I32Type [TypeAnnotation] srcAnnot

i32 and annotations.

I64Type [TypeAnnotation] srcAnnot

i64 and annotations.

DoubleType [TypeAnnotation] srcAnnot

double and annotations.

MapType (TypeReference srcAnnot) (TypeReference srcAnnot) [TypeAnnotation] srcAnnot

map<foo, bar> and annotations.

SetType (TypeReference srcAnnot) [TypeAnnotation] srcAnnot

set<baz> and annotations.

ListType (TypeReference srcAnnot) [TypeAnnotation] srcAnnot

list<qux> and annotations.

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) 

data Function srcAnnot Source

A function defined inside a service.

Constructors

Function 

Fields

functionOneWay :: Bool

Whether the function is oneway. If it's one way, it cannot receive repsonses.

functionReturnType :: Maybe (TypeReference srcAnnot)

Return type of the function, or Nothing if it's void or oneway.

functionName :: Text

Name of the function.

functionParameters :: [Field srcAnnot]

Parameters accepted by the function.

functionExceptions :: Maybe [Field srcAnnot]

Exceptions raised by the function, if any.

functionAnnotations :: [TypeAnnotation]

Annotations added to the function.

functionDocstring :: Docstring

Documentation.

functionSrcAnnot :: srcAnnot
 

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) 

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

typeAnnotationName :: Text

Name of the annotation.

typeAnnotationValue :: Maybe Text

Value for the annotation.

type Docstring = Maybe Text Source

Docstrings are Javadoc-style comments attached various defined objects.

/**
 * Fetches an item.
 */
Item getItem()

Typeclasses

class HasFields t where Source

Methods

fields :: Lens (t a) [Field a] Source

Instances

class HasValue s a | s -> a where Source

Methods

value :: Lens s a Source

class HasValues s a | s -> a where Source

Methods

values :: Lens s a Source

Instances

class HasValueType t where Source

Methods

valueType :: Lens (t a) (TypeReference a) Source