Thrift module: tutorial
The first thing to know about are types. The available types in Thrift are:
bool Boolean, one byte
i8 (byte) Signed 8-bit integer
i16 Signed 16-bit integer
i32 Signed 32-bit integer
i64 Signed 64-bit integer
double 64-bit floating point value
string String
binary Blob (byte array)
map<t1,t2> Map from one type to another
list<t1> Ordered list of one type
set<t1> Set of unique elements of one type
Did you also notice that Thrift supports C style comments?
Constants
Constant | Type | Value |
INT32CONSTANT | i32 | 9853 |
Thrift also lets you define constants for use across languages. Complex
types and structs are specified using JSON notation.
|
MAPCONSTANT | map<string , string > | { "goodnight" = "moon", "hello" = "world" } |
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.
ADD | 1 |
|
SUBTRACT | 2 |
|
MULTIPLY | 3 |
|
DIVIDE | 4 |
|
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
Key | Field | Type | Description | Requiredness | Default value |
1 | num1 | i32 | | default | 0 |
2 | num2 | i32 | | default | |
3 | op | Operation | | default | |
4 | comment | string | | optional | |
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
Key | Field | Type | Description | Requiredness | Default value |
1 | whatOp | i32 | | default | |
2 | why | string | | default | |
Structs can also be exceptions, if they are nasty.
Services
Service: Calculator
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.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.