Thrift module: tutorial

ModuleServicesData typesConstants
tutorialCalculator
InvalidOperation
MyInteger
Operation
Work
INT32CONSTANT
MAPCONSTANT

Constants

ConstantTypeValue
INT32CONSTANTi329853
Thrift also lets you define constants for use across languages. Complex types and structs are specified using JSON notation.
MAPCONSTANTmap<string, string>{ "hello" = "world", "goodnight" = "moon" }

Enumerations

Enumeration: Operation

You can define enums, which are just 32 bit integers. Values are optional and start at 1 if not supplied, C style again.

ADD1
SUBTRACT2
MULTIPLY3
DIVIDE4

Type declarations

Typedef: MyInteger

Base type: i32

Thrift lets you do typedefs to get pretty names for your types. Standard C style here.

Data structures

Struct: Work

KeyFieldTypeDescriptionRequirednessDefault value
1num1i32default0
2num2i32default
3opOperationdefault
4commentstringoptional

Structs are the basic complex data structures. They are comprised of fields which each have an integer identifier, a type, a symbolic name, and an optional default value.

Fields can be declared "optional", which ensures they will not be included in the serialized output if they aren't set. Note that this requires some manual management in some languages.

Exception: InvalidOperation

KeyFieldTypeDescriptionRequirednessDefault value
1whati32default
2whystringdefault

Structs can also be exceptions, if they are nasty.

Services

Service: Calculator

extends shared.SharedService
Ahh, now onto the cool part, defining a service. Services just need a name and can optionally inherit from another service using the extends keyword.

Function: Calculator.ping

void ping()
A method definition looks like C code. It has a return type, arguments, and optionally a list of exceptions that it may throw. Note that argument lists and exception lists are specified using the exact same syntax as field lists in struct or exception definitions.

Function: Calculator.add

i32 add(i32 num1,
        i32 num2)

Function: Calculator.calculate

i32 calculate(i32 logid,
              Work w)
    throws InvalidOperation

Function: Calculator.zip

void zip()
This method has a oneway modifier. That means the client only makes a request and does not listen for any response at all. Oneway methods must be void.