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