-
Notifications
You must be signed in to change notification settings - Fork 189
/
monster_test.fbs
executable file
·365 lines (303 loc) · 8.95 KB
/
monster_test.fbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// test schema file
include "attributes.fbs";
include "include_test1.fbs";
struct InGlobalNamespace { unused: byte; }
namespace MyGame;
table InParentNamespace {}
namespace MyGame.Example2;
table Monster {} // Test having same name as below, but in different namespace.
table Strange {}
// Test schema reserved keywords as identifier
// Can be disabled in config/config.h
// It is still a good idea to use a namespace
// to avoid conflicts with host language names
// especially for enums and structs.
// Reserved names can be important in some JSON
// related use cases.
table S2 {
namespace : int;
table : int;
struct : int;
union : int;
int : int;
}
// Enums fields can also be reserved, and these are also visible in
// JSON, but they cannot be used as table field defaults because
// type expressions do interpret keywords as keywords. For the same
// reason it is of no use to allow reserved names as type names
// such as table names.
enum foo:int { x, y, table }
namespace MyGame.Example;
// Note: parent namespace resolution only works without namespace prefix
//
// Won't work:
// union Foo { Example2.InParentNamespace }
union Foo { InParentNamespace }
namespace MyGame.Example2.SubSystem;
// Note:
//
// parent namespace resolution only works without namespace prefix
// or with a global namespace prefix (a fully qualified name), not
// something in between.
// (There is no really good reason for this, it just isn't implemented,
// and flatc JSON enum parsing works the same way.)
// The more local name wins any conflict.
//
union SubSystemA { Strange }
union SubSystemB { MyGame.Example2.Strange }
// Works in C++ flatc 1.8 but won't work with flatcc:
// union SubSystemC { Example2.Strange }
namespace MyGame.Example;
// test case for negative enum values
enum neg_enum:int {
neg1 = -12,
neg2 = -11,
neg3 = -10,
}
enum int_enum:int {
intneg = -02,
intneg2 = -1,
int1 = 02,
int2 = 42,
}
// test for hex constants
enum hex_enum:int {
hexneg = -0x02,
hex1 = 0x3,
hex2 = 0x7eafbeaf,
}
attribute "priority";
enum Color:byte (bit_flags) { Red = 0, Green, Blue = 3, }
// Note:
// For historic reasons C++ flatc 1.8 does permit conflicting base names
// from different namespaces without explicitly resolving the conflict.
//
// flatcc does not, and cannot, support this because it needs a unique
// base name to assign to the enumaration of union members - otherwise
// these enumerations could not have a namespace prefix - which is used
// in JSON and in default value assignment in the schema.
//
// Wont' work in flatc:
// union Any { Monster, TestSimpleTableWithEnum, MyGame.Example2.Monster }
union Any {
Monster,
TestSimpleTableWithEnum,
Monster2: MyGame.Example2.Monster,
Alt
}
struct Test { a:short; b:byte; }
enum notemptyenum:int { x}
table TestSimpleTableWithEnum (csharp_partial) {
color: Color = Green;
color2: Color = Color.Green;
uc : ubyte = MyGame.Example.Color.Green;
uc2 : ubyte = Color.Green;
// C++ flatc 1.8 dislikes enum values on non-enums
// color2: Color = Green;
// C++ flatc 1.8 dislikes enum values on non-enums
// uc : ubyte = 1;
// C++ flatc 1.8 dislikes enum values on non-enums, and namespace prefix
// uc2 : ubyte = 1;
}
table TestInclude {
global:InGlobalNamespace;
incval:MyGame.OtherNameSpace.FromInclude;
incval2:MyGame.OtherNameSpace.FromInclude = IncludeVal;
incval3 : int (included_attribute);
incval4:MyGame.OtherNameSpace.FromInclude = MyGame.OtherNameSpace.FromInclude.IncludeVal;
incval5: long = MyGame.OtherNameSpace.FromInclude.IncludeVal;
// C++ flatc 1.8 dislikes namespace prefix
// incval4:MyGame.OtherNameSpace.FromInclude = IncludeVal;
// C++ flatc 1.8 dislikes enum values on non-enums, and namespace prefix
// incval5: long = 0;
}
struct Vec3 (force_align: 16) {
x:float;
y:float;
z:float;
test1:double;
test2:Color;
test3:Test;
}
struct Ability {
id:uint(key);
distance:uint;
}
table Stat {
id:string;
val:long;
count:ushort;
}
// fixed length arrays new to flatcc 0.6.0
struct FooBar {
foo:[float:0x10];
bar:[int:10];
col:[Color:3];
tests:[Test:2];
text:[char:5];
}
// `sorted` attribute new to flatcc 0.6.0, not supported by flatc 1.8.
// tables with direct or indirect vector content marked as sorted
// will get a mutable sort operation that recursively sorts all
// such vectors. 'sorted` is only valid for non-union vectors.
//
// attribute "sorted";
//
table Alt {
prefix: TestJSONPrefix;
movie:Fantasy.Movie;
manyany: [Any];
multik: [MultipleKeys] (sorted);
rapunzels:[Fantasy.Rapunzel] (sorted);
names:[string] (sorted);
samples:[float32] (sorted);
fixed_array: FooBar;
}
table TestJSONPrefix {
testjsonprefixparsing:TestJSONPrefixParsing;
testjsonprefixparsing2:TestJSONPrefixParsing2;
testjsonprefixparsing3:TestJSONPrefixParsing3;
}
table TestJSONPrefixParsing
{
aaaa: string;
aaaa12345: uint;
bbbb: string;
bbbb1234: long;
cccc: string;
cccc1234: long;
cccc12345: uint;
dddd1234: long;
dddd12345: uint;
}
// when there are two keys ending in same 8 character group
table TestJSONPrefixParsing2
{
aaaa_bbbb_steps: long;
aaaa_bbbb_start_: uint;
}
// when there are two keys ending in different 8 character group
table TestJSONPrefixParsing3
{
aaaa_bbbb_steps: long;
aaaa_bbbb_start_steps: uint;
}
// C++ flatc 1.8 does not yet support base64 as a built-in
// attribute "base64";
// attribute "base64url";
table TestBase64
{
data:[ubyte] (base64);
urldata:[ubyte] (base64url);
nested:[ubyte] (nested_flatbuffer: "Monster", base64);
}
// 'primary_key' attribute new to flatcc 0.6.0, not supported by flatc 1.8.
// Allow multiple keys and allow one to be the default find and sort key
// even if not listed first. A table with a single key field behaves the
// same as a table with a single primary_key field, so use key for
// compatiblity in that case.
//
// attribute "primary_key";
//
table MultipleKeys
{
hello: string;
world: string (key);
foobar: int64 (primary_key);
}
table Monster {
pos:Vec3 (id: 0);
hp:short = 100 (id: 2);
mana:short = 150 (id: 1);
name:string (id: 3, required, key);
color:Color = Blue (id: 6);
inventory:[ubyte] (id: 5);
friendly:bool = false (deprecated, priority: 1, id: 4);
/// an example documentation comment: this will end up in the generated code
/// multiline too
testarrayoftables:[Monster] (id: 11);
testarrayofstring:[string] (id: 10);
testarrayofstring2:[string] (id: 28);
testarrayofbools:[bool] (id: 24);
testarrayofsortedstruct:[Ability] (id: 29);
enemy:MyGame.Example.Monster (id:12); // Test referring by full namespace.
// id 7 resever for Any_type
test:Any (id: 8);
test4:[Test] (id: 9);
test5:[Test] (id: 31);
testnestedflatbuffer:[ubyte] (id:13, nested_flatbuffer: "Monster");
testempty:Stat (id:14);
testbool:bool = 1 (id:15);
testhashs32_fnv1:int (id:16, hash:"fnv1_32");
testhashu32_fnv1:uint (id:17, hash:"fnv1_32");
testhashs64_fnv1:long (id:18, hash:"fnv1_64");
testhashu64_fnv1:ulong (id:19, hash:"fnv1_64");
testhashs32_fnv1a:int (id:20, hash:"fnv1a_32");
testhashu32_fnv1a:uint (id:21, hash:"fnv1a_32", cpp_type:"Stat");
testhashs64_fnv1a:long (id:22, hash:"fnv1a_64");
testhashu64_fnv1a:ulong (id:23, hash:"fnv1a_64");
// Googles flatc uses Pi as default be we don't because it
// messes up JSON tests because the numeric print format is
// configuration dependent.
//testf:float = 3.14159 (id:25);
testf:float = 3.14159e5 (id:25);
testf2:float = 3 (id:26);
testf3:float (id:27);
flex:[ubyte] (id:30, flexbuffer);
vector_of_longs:[long] (id:32);
vector_of_doubles:[double] (id:33);
parent_namespace_test:InParentNamespace (id:34);
testbase64:TestBase64 (id:35);
}
table TypeAliases {
i8:int8;
u8:uint8;
i16:int16;
u16:uint16;
i32:int32;
u32:uint32;
i64:int64;
u64:uint64;
f32:float32;
f64:float64;
v8:[int8];
vf64:[float64];
}
rpc_service MonsterStorage {
Store(Monster):Stat (streaming: "none");
Retrieve(Stat):Monster (streaming: "server", idempotent);
}
// Demonstrates the ability to have vectors of unions, and also to
// store structs and strings in unions.
namespace Fantasy;
table Attacker {
sword_attack_damage: int;
}
struct Rapunzel {
hair_length: uint16 (key);
travel_points: int (deprecated);
}
struct BookReader {
books_read: int;
}
union Character {
MuLan: Attacker = 2, // Can have name be different from type.
Rapunzel = 8, // Or just both the same, as before.
Belle: Fantasy.BookReader,
BookFan: BookReader,
Other: string,
Unused: string = 255
}
table Movie {
main_character: Character;
antagonist: Character;
side_kick: Character;
cameo: Character;
characters: [Character];
}
root_type MyGame.Example.Monster;
file_identifier "MONS";
file_extension "mon";
// Out of order enums
enum ReorderedEnum: int { rx = 10, ry = 1, rz = 9 }
enum ReorderedColor:byte (bit_flags) { RBlue = 3, RRed = 0, RGreen }