| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* | 
 | 2 |  * Licensed to the Apache Software Foundation (ASF) under one | 
 | 3 |  * or more contributor license agreements. See the NOTICE file | 
 | 4 |  * distributed with this work for additional information | 
 | 5 |  * regarding copyright ownership. The ASF licenses this file | 
 | 6 |  * to you under the Apache License, Version 2.0 (the | 
 | 7 |  * "License"); you may not use this file except in compliance | 
 | 8 |  * with the License. You may obtain a copy of the License at | 
 | 9 |  * | 
 | 10 |  *   http://www.apache.org/licenses/LICENSE-2.0 | 
 | 11 |  * | 
 | 12 |  * Unless required by applicable law or agreed to in writing, | 
 | 13 |  * software distributed under the License is distributed on an | 
 | 14 |  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
 | 15 |  * KIND, either express or implied. See the License for the | 
 | 16 |  * specific language governing permissions and limitations | 
 | 17 |  * under the License. | 
 | 18 |  */ | 
 | 19 |  | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 20 | # Thrift Tutorial | 
 | 21 | # Mark Slee (mcslee@facebook.com) | 
 | 22 | # | 
 | 23 | # This file aims to teach you how to use Thrift, in a .thrift file. Neato. The | 
 | 24 | # first thing to notice is that .thrift files support standard shell comments. | 
 | 25 | # This lets you make your thrift file executable and include your Thrift build | 
 | 26 | # step on the top line. And you can place comments like this anywhere you like. | 
 | 27 | # | 
 | 28 | # Before running this file, you will need to have installed the thrift compiler | 
 | 29 | # into /usr/local/bin. | 
 | 30 |  | 
 | 31 | /** | 
 | 32 |  * The first thing to know about are types. The available types in Thrift are: | 
 | 33 |  * | 
 | 34 |  *  bool        Boolean, one byte | 
| Nobuaki Sukegawa | 2e00c99 | 2015-12-01 23:46:58 +0900 | [diff] [blame] | 35 |  *  i8 (byte)   Signed 8-bit integer | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 36 |  *  i16         Signed 16-bit integer | 
 | 37 |  *  i32         Signed 32-bit integer | 
 | 38 |  *  i64         Signed 64-bit integer | 
 | 39 |  *  double      64-bit floating point value | 
 | 40 |  *  string      String | 
| Bryan Duxbury | 7003f87 | 2009-02-01 06:21:13 +0000 | [diff] [blame] | 41 |  *  binary      Blob (byte array) | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 42 |  *  map<t1,t2>  Map from one type to another | 
 | 43 |  *  list<t1>    Ordered list of one type | 
 | 44 |  *  set<t1>     Set of unique elements of one type | 
 | 45 |  * | 
 | 46 |  * Did you also notice that Thrift supports C style comments? | 
 | 47 |  */ | 
 | 48 |  | 
 | 49 | // Just in case you were wondering... yes. We support simple C comments too. | 
 | 50 |  | 
 | 51 | /** | 
 | 52 |  * Thrift files can reference other Thrift files to include common struct | 
 | 53 |  * and service definitions. These are found using the current path, or by | 
 | 54 |  * searching relative to any paths specified with the -I compiler flag. | 
 | 55 |  * | 
 | 56 |  * Included objects are accessed using the name of the .thrift file as a | 
 | 57 |  * prefix. i.e. shared.SharedObject | 
 | 58 |  */ | 
 | 59 | include "shared.thrift" | 
 | 60 |  | 
 | 61 | /** | 
 | 62 |  * Thrift files can namespace, package, or prefix their output in various | 
 | 63 |  * target languages. | 
 | 64 |  */ | 
| Tomek Kurcz | e93a901 | 2017-09-19 09:16:43 +0200 | [diff] [blame] | 65 |  | 
| David Reiss | 9a08dc6 | 2008-02-27 01:55:17 +0000 | [diff] [blame] | 66 | namespace cpp tutorial | 
| Jake Farrell | b95b0ff | 2012-03-22 21:49:10 +0000 | [diff] [blame] | 67 | namespace d tutorial | 
| Mark Erickson | 932c470 | 2015-08-29 10:46:51 -0500 | [diff] [blame] | 68 | namespace dart tutorial | 
| David Reiss | 771f8c7 | 2008-02-27 01:55:25 +0000 | [diff] [blame] | 69 | namespace java tutorial | 
| David Reiss | 554ea6f | 2009-02-17 20:28:37 +0000 | [diff] [blame] | 70 | namespace php tutorial | 
| David Reiss | 07ef3a9 | 2008-03-27 21:42:39 +0000 | [diff] [blame] | 71 | namespace perl tutorial | 
| Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 72 | namespace haxe tutorial | 
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 73 | namespace netstd tutorial | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 74 |  | 
 | 75 | /** | 
 | 76 |  * Thrift lets you do typedefs to get pretty names for your types. Standard | 
 | 77 |  * C style here. | 
 | 78 |  */ | 
 | 79 | typedef i32 MyInteger | 
 | 80 |  | 
 | 81 | /** | 
 | 82 |  * Thrift also lets you define constants for use across languages. Complex | 
 | 83 |  * types and structs are specified using JSON notation. | 
 | 84 |  */ | 
 | 85 | const i32 INT32CONSTANT = 9853 | 
 | 86 | const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'} | 
 | 87 |  | 
 | 88 | /** | 
 | 89 |  * You can define enums, which are just 32 bit integers. Values are optional | 
 | 90 |  * and start at 1 if not supplied, C style again. | 
 | 91 |  */ | 
 | 92 | enum Operation { | 
 | 93 |   ADD = 1, | 
 | 94 |   SUBTRACT = 2, | 
 | 95 |   MULTIPLY = 3, | 
 | 96 |   DIVIDE = 4 | 
 | 97 | } | 
 | 98 |  | 
 | 99 | /** | 
 | 100 |  * Structs are the basic complex data structures. They are comprised of fields | 
 | 101 |  * which each have an integer identifier, a type, a symbolic name, and an | 
 | 102 |  * optional default value. | 
| David Reiss | 5ff21a4 | 2008-07-24 19:13:54 +0000 | [diff] [blame] | 103 |  * | 
 | 104 |  * Fields can be declared "optional", which ensures they will not be included | 
 | 105 |  * in the serialized output if they aren't set.  Note that this requires some | 
 | 106 |  * manual management in some languages. | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 107 |  */ | 
 | 108 | struct Work { | 
 | 109 |   1: i32 num1 = 0, | 
 | 110 |   2: i32 num2, | 
| David Reiss | 5ff21a4 | 2008-07-24 19:13:54 +0000 | [diff] [blame] | 111 |   3: Operation op, | 
 | 112 |   4: optional string comment, | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 113 | } | 
 | 114 |  | 
 | 115 | /** | 
 | 116 |  * Structs can also be exceptions, if they are nasty. | 
 | 117 |  */ | 
 | 118 | exception InvalidOperation { | 
| Konrad Grochowski | 3b115df | 2015-05-18 17:58:36 +0200 | [diff] [blame] | 119 |   1: i32 whatOp, | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 120 |   2: string why | 
 | 121 | } | 
 | 122 |  | 
 | 123 | /** | 
 | 124 |  * Ahh, now onto the cool part, defining a service. Services just need a name | 
 | 125 |  * and can optionally inherit from another service using the extends keyword. | 
 | 126 |  */ | 
 | 127 | service Calculator extends shared.SharedService { | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 128 |  | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 129 |   /** | 
 | 130 |    * A method definition looks like C code. It has a return type, arguments, | 
 | 131 |    * and optionally a list of exceptions that it may throw. Note that argument | 
 | 132 |    * lists and exception lists are specified using the exact same syntax as | 
 | 133 |    * field lists in struct or exception definitions. | 
 | 134 |    */ | 
 | 135 |  | 
 | 136 |    void ping(), | 
 | 137 |  | 
 | 138 |    i32 add(1:i32 num1, 2:i32 num2), | 
 | 139 |  | 
 | 140 |    i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch), | 
 | 141 |  | 
 | 142 |    /** | 
| David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 143 |     * This method has a oneway modifier. That means the client only makes | 
 | 144 |     * 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] | 145 |     * must be void. | 
 | 146 |     */ | 
| David Reiss | cecbed8 | 2009-03-24 20:02:22 +0000 | [diff] [blame] | 147 |    oneway void zip() | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 148 |  | 
 | 149 | } | 
 | 150 |  | 
 | 151 | /** | 
 | 152 |  * That just about covers the basics. Take a look in the test/ folder for more | 
 | 153 |  * detailed examples. After you run this file, your generated code shows up | 
 | 154 |  * in folders with names gen-<language>. The generated code isn't too scary | 
 | 155 |  * to look at. It even has pretty indentation. | 
 | 156 |  */ |