Skip to content

Commit 2cffba2

Browse files
committed
Start to support all languages in tutorial
1 parent 34f0728 commit 2cffba2

File tree

3 files changed

+348
-1
lines changed

3 files changed

+348
-1
lines changed

docs/mkdocs.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ markdown_extensions:
108108
slugify: !!python/object/apply:pymdownx.slugs.slugify
109109
kwds:
110110
case: lower
111+
- pymdownx.highlight:
112+
extend_pygments_lang:
113+
# PHP wasn't highlighting correctly. This is a work around found
114+
# https://github.com/squidfunk/mkdocs-material/issues/138#issuecomment-2294025627
115+
- name: php
116+
lang: php
117+
options:
118+
startinline: true
111119
- tables
112120

113121

@@ -131,6 +139,7 @@ nav:
131139
- Go: "languages/go.md"
132140
- Java: "languages/java.md"
133141
- JavasScript: "languages/javascript.md"
142+
- Kotlin: "languages/kotlin.md"
134143
- Lobster: "languages/lobster.md"
135144
- Lua: "languages/lua.md"
136145
- PHP: "languages/php.md"

docs/source/languages/kotlin.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
Use in Kotlin {#flatbuffers_guide_use_kotlin}
2+
==============
3+
4+
## Before you get started
5+
6+
Before diving into the FlatBuffers usage in Kotlin, it should be noted that
7+
the [Tutorial](../tutorial.md) page has a complete guide to
8+
general FlatBuffers usage in all of the supported languages (including K).
9+
10+
This page is designed to cover the nuances of FlatBuffers usage, specific to Kotlin.
11+
12+
You should also have read the [Building](../building.md)
13+
documentation to build `flatc` and should be familiar with
14+
[Using the schema compiler](../flatc.md) and
15+
[Writing a schema](../schema.md).
16+
17+
## Kotlin and FlatBuffers Java code location
18+
19+
Code generated for Kotlin currently uses the flatbuffers java runtime library. That means that Kotlin generated code can only have Java virtual machine as target architecture (which includes Android). Kotlin Native and Kotlin.js are currently not supported.
20+
21+
The code for the FlatBuffers Java library can be found at
22+
`flatbuffers/java/com/google/flatbuffers`. You can browse the library on the
23+
[FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/
24+
java/com/google/flatbuffers).
25+
26+
## Testing FlatBuffers Kotlin
27+
28+
The test code for Java is located in [KotlinTest.java](https://github.com/google
29+
/flatbuffers/blob/master/tests/KotlinTest.kt).
30+
31+
To run the tests, use [KotlinTest.sh](https://github.com/google/
32+
flatbuffers/blob/master/tests/KotlinTest.sh) shell script.
33+
34+
*Note: These scripts require that [Kotlin](https://kotlinlang.org/) is installed.*
35+
36+
## Using the FlatBuffers Kotlin library
37+
38+
*Note: See [Tutorial](../tutorial.md) for a more in-depth
39+
example of how to use FlatBuffers in Kotlin.*
40+
41+
FlatBuffers supports reading and writing binary FlatBuffers in Kotlin.
42+
43+
To use FlatBuffers in your own code, first generate Java classes from your
44+
schema with the `--kotlin` option to `flatc`.
45+
Then you can include both FlatBuffers and the generated code to read
46+
or write a FlatBuffer.
47+
48+
For example, here is how you would read a FlatBuffer binary file in Kotlin:
49+
First, import the library and generated code. Then, you read a FlatBuffer binary
50+
file into a `ByteArray`. You then turn the `ByteArray` into a `ByteBuffer`, which you
51+
pass to the `getRootAsMyRootType` function:
52+
53+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.kt}
54+
import MyGame.Example.*
55+
import com.google.flatbuffers.FlatBufferBuilder
56+
57+
// This snippet ignores exceptions for brevity.
58+
val data = RandomAccessFile(File("monsterdata_test.mon"), "r").use {
59+
val temp = ByteArray(it.length().toInt())
60+
it.readFully(temp)
61+
temp
62+
}
63+
64+
val bb = ByteBuffer.wrap(data)
65+
val monster = Monster.getRootAsMonster(bb)
66+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67+
68+
Now you can access the data from the `Monster monster`:
69+
70+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.kt}
71+
val hp = monster.hp
72+
val pos = monster.pos!!;
73+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74+
75+
76+
77+
## Differences between Kotlin and Java code
78+
79+
Kotlin generated code was designed to be as close as possible to the java counterpart, as for now, we only support kotlin on java virtual machine. So the differences in implementation and usage are basically the ones introduced by the Kotlin language itself. You can find more in-depth information [here](https://kotlinlang.org/docs/reference/comparison-to-java.html).
80+
81+
The most obvious ones are:
82+
83+
* Fields as accessed as Kotlin [properties](https://kotlinlang.org/docs/reference/properties.html)
84+
* Static methods are accessed in [companion object](https://kotlinlang.org/docs/reference/classes.html#companion-objects)

docs/source/tutorial.md

Lines changed: 255 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ data structures, see the [schema](schema.md) documentation for a detail
3535
description. Use the inline code annotations to get a brief synopsis of each
3636
part of the schema.
3737

38-
```c title="monster.fbs" linenums="1"
38+
```proto title="monster.fbs" linenums="1"
3939
// Example IDL file for our monster's schema.
4040
4141
namespace MyGame.Sample; //(1)!
@@ -163,12 +163,103 @@ serializing and deserializing the flatbuffer binary data.
163163
flatc --cpp monster.fbs
164164
```
165165

166+
=== "C"
167+
168+
!!! Note
169+
170+
If you're working in C, you need to use the separate project
171+
[FlatCC](https://github.com/dvidelabs/flatcc) which contains a schema
172+
compiler and runtime library in C for C. See
173+
[flatcc build instructions](https://github.com/dvidelabs/flatcc#building).
174+
175+
Please be aware of the difference between `flatc` and `flatcc` tools.
176+
177+
```sh
178+
cd flatcc
179+
mkdir -p build/tmp/samples/monster
180+
bin/flatcc -a -o build/tmp/samples/monster samples/monster/monster.fbs
181+
# or just
182+
flatcc/samples/monster/build.sh
183+
```
184+
166185
=== "C#"
167186

168187
```sh
169188
flatc --csharp monster.fbs
170189
```
171190

191+
=== "Dart"
192+
193+
```sh
194+
flatc --dart monster.fbs
195+
```
196+
197+
=== "Go"
198+
199+
```sh
200+
flatc --go monster.fbs
201+
```
202+
203+
=== "Java"
204+
205+
```sh
206+
flatc --java monster.fbs
207+
```
208+
209+
=== "JavaScript"
210+
211+
```sh
212+
flatc --js monster.fbs
213+
```
214+
215+
=== "Kotlin"
216+
217+
```sh
218+
flatc --kotlin monster.fbs
219+
```
220+
221+
=== "Lobster"
222+
223+
```sh
224+
flatc --lobster monster.fbs
225+
```
226+
227+
=== "Lua"
228+
229+
```sh
230+
flatc --lua monster.fbs
231+
```
232+
233+
=== "PHP"
234+
235+
```sh
236+
flatc --php monster.fbs
237+
```
238+
239+
=== "Python"
240+
241+
```sh
242+
flatc --python monster.fbs
243+
```
244+
245+
=== "Rust"
246+
247+
```sh
248+
flatc --rust monster.fbs
249+
```
250+
251+
=== "Swift"
252+
253+
```sh
254+
flatc --swift monster.fbs
255+
```
256+
257+
=== "TypeScript"
258+
259+
```sh
260+
flatc --ts monster.fbs
261+
```
262+
172263
You can deserialize flatbuffers in languages that differ from the language that
173264
serialized it. For purpose of this tutorial, we assume one language is used for
174265
both serializing and deserializing.
@@ -192,13 +283,176 @@ generally involves two things:
192283
using namespace MyGame::Sample; // Specified in the schema.
193284
```
194285

286+
=== "C"
287+
288+
```c
289+
#include "monster_builder.h" // Generated by `flatcc`.
290+
291+
// Convenient namespace macro to manage long namespace prefix.
292+
#undef ns
293+
#define ns(x) FLATBUFFERS_WRAP_NAMESPACE(MyGame_Sample, x) // Specified in the schema.
294+
295+
// A helper to simplify creating vectors from C-arrays.
296+
#define c_vec_len(V) (sizeof(V)/sizeof((V)[0]))
297+
```
298+
195299
=== "C#"
196300

197301
```c#
198302
using Google.FlatBuffers; // The runtime library for C#
199303
using MyGame.Sample; // The generated files from `flatc`
200304
```
201305

306+
=== "Dart"
307+
308+
```dart
309+
import 'package:flat_buffers/flat_buffers.dart' as fb;
310+
311+
// Generated by `flatc`.
312+
import 'monster_my_game.sample_generated.dart' as myGame;
313+
```
314+
315+
=== "Go"
316+
317+
```go
318+
import (
319+
flatbuffers "github.com/google/flatbuffers/go"
320+
sample "MyGame/Sample"
321+
)
322+
```
323+
324+
=== "Java"
325+
326+
```java
327+
import MyGame.Sample.*; //The `flatc` generated files. (Monster, Vec3, etc.)
328+
329+
import com.google.flatbuffers.FlatBufferBuilder;
330+
```
331+
332+
=== "JavaScript"
333+
334+
```javascript
335+
// The following code is an example - use your desired module flavor by
336+
// transpiling from TS.
337+
var flatbuffers = require('/js/flatbuffers').flatbuffers;
338+
var MyGame = require('./monster_generated').MyGame; // Generated by `flatc`.
339+
340+
//--------------------------------------------------------------------------//
341+
342+
// The following code is for browser-based HTML/JavaScript. Use the above code
343+
// for JavaScript module loaders (e.g. Node.js).
344+
<script src="../js/flatbuffers.js"></script>
345+
<script src="monster_generated.js"></script> // Generated by `flatc`.
346+
```
347+
348+
=== "Kotlin"
349+
350+
```kotlin
351+
import MyGame.Sample.* //The `flatc` generated files. (Monster, Vec3, etc.)
352+
353+
import com.google.flatbuffers.FlatBufferBuilder
354+
```
355+
356+
=== "Lobster"
357+
358+
```lobster
359+
import from "../lobster/" // Where to find flatbuffers.lobster
360+
import monster_generated
361+
```
362+
363+
=== "Lua"
364+
365+
```lua
366+
-- require the flatbuffers module
367+
local flatbuffers = require("flatbuffers")
368+
369+
-- require the generated files from `flatc`.
370+
local color = require("MyGame.Sample.Color")
371+
local equipment = require("MyGame.Sample.Equipment")
372+
local monster = require("MyGame.Sample.Monster")
373+
local vec3 = require("MyGame.Sample.Vec3")
374+
local weapon = require("MyGame.Sample.Weapon")
375+
```
376+
377+
=== "PHP"
378+
379+
```php
380+
// It is recommended that your use PSR autoload when using FlatBuffers in PHP.
381+
// Here is an example from `SampleBinary.php`:
382+
function __autoload($class_name) {
383+
// The last segment of the class name matches the file name.
384+
$class = substr($class_name, strrpos($class_name, "\\") + 1);
385+
// `flatbuffers` root.
386+
$root_dir = join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__))));
387+
388+
// Contains the `*.php` files for the FlatBuffers library and the `flatc`
389+
// generated files.
390+
$paths = array(join(DIRECTORY_SEPARATOR, array($root_dir, "php")),
391+
join(DIRECTORY_SEPARATOR,
392+
array($root_dir, "samples", "MyGame", "Sample")));
393+
foreach ($paths as $path) {
394+
$file = join(DIRECTORY_SEPARATOR, array($path, $class . ".php"));
395+
if (file_exists($file)) {
396+
require($file);
397+
break;
398+
}
399+
}
400+
}
401+
```
402+
403+
=== "Python"
404+
405+
```py
406+
import flatbuffers
407+
408+
# Generated by `flatc`.
409+
import MyGame.Sample.Color
410+
import MyGame.Sample.Equipment
411+
import MyGame.Sample.Monster
412+
import MyGame.Sample.Vec3
413+
import MyGame.Sample.Weapon
414+
```
415+
416+
=== "Rust"
417+
418+
```rust
419+
// import the flatbuffers runtime library
420+
extern crate flatbuffers;
421+
422+
// import the generated code
423+
#[allow(dead_code, unused_imports)]
424+
#[path = "./monster_generated.rs"]
425+
mod monster_generated;
426+
pub use monster_generated::my_game::sample::{root_as_monster,
427+
Color, Equipment,
428+
Monster, MonsterArgs,
429+
Vec3,
430+
Weapon, WeaponArgs};
431+
```
432+
433+
=== "Swift"
434+
435+
```swift
436+
/**
437+
// make sure that monster_generated.swift is included in your project
438+
*/
439+
import Flatbuffers
440+
441+
// typealiases for convenience
442+
typealias Monster = MyGame1_Sample_Monster
443+
typealias Weapon = MyGame1_Sample_Weapon
444+
typealias Color = MyGame1_Sample_Color
445+
typealias Vec3 = MyGame1_Sample_Vec3
446+
```
447+
448+
=== "TypeScript"
449+
450+
```ts
451+
// note: import flatbuffers with your desired import method
452+
453+
import { MyGame } from './monster_generated';
454+
```
455+
202456
For some languages the runtime libraries are just code files you compile into
203457
your application. While other languages provide packaged libraries via their
204458
package managers.

0 commit comments

Comments
 (0)