blob: 1eb83b6a50e93ce1c20104c71dab1641c060815d [file] [log] [blame]
Kevin Clark7618fb42008-06-18 01:02:46 +00001#!/usr/local/bin/thrift --gen cpp --gen java --gen py --php --gen rb --gen perl --erl --xsd -r
Mark Slee07a3aab2007-03-07 05:45:10 +00002#
3# Thrift Tutorial
4# Mark Slee (mcslee@facebook.com)
5#
6# This file aims to teach you how to use Thrift, in a .thrift file. Neato. The
7# first thing to notice is that .thrift files support standard shell comments.
8# This lets you make your thrift file executable and include your Thrift build
9# step on the top line. And you can place comments like this anywhere you like.
10#
11# Before running this file, you will need to have installed the thrift compiler
12# into /usr/local/bin.
13
14/**
15 * The first thing to know about are types. The available types in Thrift are:
16 *
17 * bool Boolean, one byte
18 * byte Signed byte
19 * i16 Signed 16-bit integer
20 * i32 Signed 32-bit integer
21 * i64 Signed 64-bit integer
22 * double 64-bit floating point value
23 * string String
Bryan Duxbury7003f872009-02-01 06:21:13 +000024 * binary Blob (byte array)
Mark Slee07a3aab2007-03-07 05:45:10 +000025 * map<t1,t2> Map from one type to another
26 * list<t1> Ordered list of one type
27 * set<t1> Set of unique elements of one type
28 *
29 * Did you also notice that Thrift supports C style comments?
30 */
31
32// Just in case you were wondering... yes. We support simple C comments too.
33
34/**
35 * Thrift files can reference other Thrift files to include common struct
36 * and service definitions. These are found using the current path, or by
37 * searching relative to any paths specified with the -I compiler flag.
38 *
39 * Included objects are accessed using the name of the .thrift file as a
40 * prefix. i.e. shared.SharedObject
41 */
42include "shared.thrift"
43
44/**
45 * Thrift files can namespace, package, or prefix their output in various
46 * target languages.
47 */
David Reiss9a08dc62008-02-27 01:55:17 +000048namespace cpp tutorial
David Reiss771f8c72008-02-27 01:55:25 +000049namespace java tutorial
David Reiss554ea6f2009-02-17 20:28:37 +000050namespace php tutorial
David Reiss07ef3a92008-03-27 21:42:39 +000051namespace perl tutorial
David Reiss3b455012008-03-27 21:40:46 +000052namespace smalltalk.category Thrift.Tutorial
Mark Slee07a3aab2007-03-07 05:45:10 +000053
54/**
55 * Thrift lets you do typedefs to get pretty names for your types. Standard
56 * C style here.
57 */
58typedef i32 MyInteger
59
60/**
61 * Thrift also lets you define constants for use across languages. Complex
62 * types and structs are specified using JSON notation.
63 */
64const i32 INT32CONSTANT = 9853
65const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'}
66
67/**
68 * You can define enums, which are just 32 bit integers. Values are optional
69 * and start at 1 if not supplied, C style again.
70 */
71enum Operation {
72 ADD = 1,
73 SUBTRACT = 2,
74 MULTIPLY = 3,
75 DIVIDE = 4
76}
77
78/**
79 * Structs are the basic complex data structures. They are comprised of fields
80 * which each have an integer identifier, a type, a symbolic name, and an
81 * optional default value.
David Reiss5ff21a42008-07-24 19:13:54 +000082 *
83 * Fields can be declared "optional", which ensures they will not be included
84 * in the serialized output if they aren't set. Note that this requires some
85 * manual management in some languages.
Mark Slee07a3aab2007-03-07 05:45:10 +000086 */
87struct Work {
88 1: i32 num1 = 0,
89 2: i32 num2,
David Reiss5ff21a42008-07-24 19:13:54 +000090 3: Operation op,
91 4: optional string comment,
Mark Slee07a3aab2007-03-07 05:45:10 +000092}
93
94/**
95 * Structs can also be exceptions, if they are nasty.
96 */
97exception InvalidOperation {
98 1: i32 what,
99 2: string why
100}
101
102/**
103 * Ahh, now onto the cool part, defining a service. Services just need a name
104 * and can optionally inherit from another service using the extends keyword.
105 */
106service Calculator extends shared.SharedService {
David Reiss0c90f6f2008-02-06 22:18:40 +0000107
Mark Slee07a3aab2007-03-07 05:45:10 +0000108 /**
109 * A method definition looks like C code. It has a return type, arguments,
110 * and optionally a list of exceptions that it may throw. Note that argument
111 * lists and exception lists are specified using the exact same syntax as
112 * field lists in struct or exception definitions.
113 */
114
115 void ping(),
116
117 i32 add(1:i32 num1, 2:i32 num2),
118
119 i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),
120
121 /**
122 * This method has an async modifier. That means the client only makes
123 * a request and does not listen for any response at all. Async methods
124 * must be void.
125 */
126 async void zip()
127
128}
129
130/**
131 * That just about covers the basics. Take a look in the test/ folder for more
132 * detailed examples. After you run this file, your generated code shows up
133 * in folders with names gen-<language>. The generated code isn't too scary
134 * to look at. It even has pretty indentation.
135 */