blob: 5b8db20b27d1b688ab0e104268b82508f6017d19 [file] [log] [blame] [view]
Roger Meierc5026aa2015-02-09 12:03:59 +01001## Thrift interface description language
2The Thrift interface definition language (IDL) allows for the definition of [Thrift Types](/docs/types). A Thrift IDL file is processed by the Thrift code generator to produce code for the various target languages to support the defined structs and services in the IDL file.
3
4## Description
5
6*Under construction*
7
8Here is a description of the Thrift IDL.
9
10## Document
11
12Every Thrift document contains 0 or more headers followed by 0 or more definitions.
13
14 [1] Document ::= Header* Definition*
15
16## Header
17
18A header is either a Thrift include, a C++ include, or a namespace declaration.
19
20 [2] Header ::= Include | CppInclude | Namespace
21
22### Thrift Include
23
24An include makes all the symbols from another file visible (with a prefix) and adds corresponding include statements into the code generated for this Thrift document.
25
26 [3] Include ::= 'include' Literal
27
28### C++ Include
29
30A C++ include adds a custom C++ include to the output of the C++ code generator for this Thrift document.
31
32 [4] CppInclude ::= 'cpp_include' Literal
33
34### Namespace
35
36A namespace declares which namespaces/package/module/etc. the type definitions in this file will be declared in for the target languages. The namespace scope indicates which language the namespace applies to; a scope of '*' indicates that the namespace applies to all target languages.
37
38 [5] Namespace ::= ( 'namespace' ( NamespaceScope Identifier ) |
39 ( 'smalltalk.category' STIdentifier ) |
40 ( 'smalltalk.prefix' Identifier ) ) |
41 ( 'php_namespace' Literal ) |
42 ( 'xsd_namespace' Literal )
43
44 [6] NamespaceScope ::= '*' | 'cpp' | 'java' | 'py' | 'perl' | 'rb' | 'cocoa' | 'csharp'
45
46N.B.: Smalltalk has two distinct types of namespace commands.
47*Can someone who knows Smalltalk explain why Smalltalk needs two different kinds of namespaces?*
48
49N.B.: The `php_namespace` directive will be deprecated at some point in the future in favor of the scoped syntax, but the scoped syntax is not yet supported for PHP.
50
51N.B.: The `xsd_namespace` directive has some purpose internal to Facebook but serves no purpose in Thrift itself. Use of this feature is strongly discouraged
52
53## Definition
54
55 [7] Definition ::= Const | Typedef | Enum | Senum | Struct | Union | Exception | Service
56
57### Const
58
59 [8] Const ::= 'const' FieldType Identifier '=' ConstValue ListSeparator?
60
61### Typedef
62
63A typedef creates an alternate name for a type.
64
65 [9] Typedef ::= 'typedef' DefinitionType Identifier
66
67### Enum
68
69An enum creates an enumerated type, with named values. If no constant value is supplied, the value is either 0 for the first element, or one greater than the preceding value for any subsequent element. Any constant value that is supplied must be non-negative.
70
71 [10] Enum ::= 'enum' Identifier '{' (Identifier ('=' IntConstant)? ListSeparator?)* '}'
72
73### Senum
74
75Senum (and Slist) are now deprecated and should both be replaced with String.
76
77 [11] Senum ::= 'senum' Identifier '{' (Literal ListSeparator?)* '}'
78
79### Struct
80
81Structs are the fundamental compositional type in Thrift. The name of each field must be unique within the struct.
82
83 [12] Struct ::= 'struct' Identifier 'xsd_all'? '{' Field* '}'
84
85N.B.: The `xsd_all` keyword has some purpose internal to Facebook but serves no purpose in Thrift itself. Use of this feature is strongly discouraged
86
87### Union
88
89Unions are similar to structs, except that they provide a means to transport exactly one field of a possible set of fields, just like union {} in C++. Consequently, union members cannot be required fields.
90
91 [13] Union ::= 'union' Identifier 'xsd_all'? '{' Field* '}'
92
93N.B.: The `xsd_all` keyword has some purpose internal to Facebook but serves no purpose in Thrift itself. Use of this feature is strongly discouraged
94
95### Exception
96
97Exceptions are similar to structs except that they are intended to integrate with the native exception handling mechanisms in the target languages. The name of each field must be unique within the exception.
98
99 [14] Exception ::= 'exception' Identifier '{' Field* '}'
100
101### Service
102
103A service provides the interface for a set of functionality provided by a Thrift server. The interface is simply a list of functions. A service can extend another service, which simply means that it provides the functions of the extended service in addition to its own.
104
105 [15] Service ::= 'service' Identifier ( 'extends' Identifier )? '{' Function* '}'
106
107## Field
108
109 [16] Field ::= FieldID? FieldReq? FieldType Identifier ('= ConstValue)? XsdFieldOptions ListSeparator?
110
111### Field ID
112
113 [17] FieldID ::= IntConstant ':'
114
115### Field Requiredness
116
117 [18] FieldReq ::= 'required' | 'optional'
118
119### XSD Options
120
121N.B.: These have some internal purpose at Facebook but serve no current purpose in Thrift. Use of these options is strongly discouraged.
122
123 [19] XsdFieldOptions ::= 'xsd_optional'? 'xsd_nillable'? XsdAttrs?
124
125 [20] XsdAttrs ::= 'xsd_attrs' '{' Field* '}'
126
127## Functions
128
129 [21] Function ::= 'oneway'? FunctionType Identifier '(' Field* ')' Throws? ListSeparator?
130
131 [22] FunctionType ::= FieldType | 'void'
132
133 [23] Throws ::= 'throws' '(' Field* ')'
134
135## Types
136
137 [24] FieldType ::= Identifier | BaseType | ContainerType
138
139 [25] DefinitionType ::= BaseType | ContainerType
140
141 [26] BaseType ::= 'bool' | 'byte' | 'i16' | 'i32' | 'i64' | 'double' | 'string' | 'binary' | 'slist'
142
143 [27] ContainerType ::= MapType | SetType | ListType
144
145 [28] MapType ::= 'map' CppType? '<' FieldType ',' FieldType '>'
146
147 [29] SetType ::= 'set' CppType? '<' FieldType '>'
148
149 [30] ListType ::= 'list' '<' FieldType '>' CppType?
150
151 [31] CppType ::= 'cpp_type' Literal
152
153## Constant Values
154
155 [32] ConstValue ::= IntConstant | DoubleConstant | Literal | Identifier | ConstList | ConstMap
156
157 [33] IntConstant ::= ('+' | '-')? Digit+
158
159 [34] DoubleConstant ::= ('+' | '-')? Digit* ('.' Digit+)? ( ('E' | 'e') IntConstant )?
160
161 [35] ConstList ::= '[' (ConstValue ListSeparator?)* ']'
162
163 [36] ConstMap ::= '{' (ConstValue ':' ConstValue ListSeparator?)* '}'
164
165## Basic Definitions
166
167### Literal
168
169 [37] Literal ::= ('"' [^"]* '"') | ("'" [^']* "'")
170
171### Identifier
172
173 [38] Identifier ::= ( Letter | '_' ) ( Letter | Digit | '.' | '_' )*
174
175 [39] STIdentifier ::= ( Letter | '_' ) ( Letter | Digit | '.' | '_' | '-' )*
176
177### List Separator
178
179 [40] ListSeparator ::= ',' | ';'
180
181### Letters and Digits
182
183 [41] Letter ::= ['A'-'Z'] | ['a'-'z']
184
185 [42] Digit ::= ['0'-'9']
186
187## Examples
188
189Here are some examples of Thrift definitions, using the Thrift IDL:
190
191 * [ThriftTest.thrift][] used by the Thrift TestFramework
192 * Thrift [tutorial][]
193 * Facebook's [fb303.thrift][]
194 * [Apache Cassandra's][] Thrift IDL: [cassandra.thrift][]
195 * [Evernote API][]
196
197 [ThriftTest.thrift]: https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=test/ThriftTest.thrift;hb=HEAD
198 [tutorial]: /tutorial/
199 [fb303.thrift]: https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=contrib/fb303/if/fb303.thrift;hb=HEAD
200 [Apache Cassandra's]: http://cassandra.apache.org/
201 [cassandra.thrift]: http://svn.apache.org/viewvc/cassandra/trunk/interface/cassandra.thrift?view=co
202 [Evernote API]: http://www.evernote.com/about/developer/api/
203
204## To Do/Questions
205
206Initialization of Base Types for all Languages?
207
208 * Do all Languages initialize them to 0, bool=false and string=""? or null, undefined?
209
210Why does position of `CppType` vary between `SetType` and `ListType`?
211
212 * std::set does sort the elements automatically, that's the design. see [Thrift Types](/docs/types) or the [C++ std:set reference][] for further details
213 * The question is, how other languages are doing that? What about custom objects, do they have a Compare function the set the order correctly?
214
215 [C++ std:set reference]: http://www.cplusplus.com/reference/stl/set/
216
217Why can't `DefinitionType` be the same as `FieldType` (i.e. include `Identifier`)?
218
219Examine the `smalltalk.prefix` and `smalltalk.category` status (esp `smalltalk.category`, which takes `STIdentifier` as its argument)...
220
221What to do about `ListSeparator`? Do we really want to be as lax as we currently are?
222
223Should `Field*` really be `Field+` in `Struct`, `Enum`, etc.?
224