Start to support all languages in tutorial

This commit is contained in:
Derek Bailey
2025-01-10 15:10:20 -08:00
parent 34f0728ea2
commit 2cffba28b4
3 changed files with 348 additions and 1 deletions

View File

@@ -35,7 +35,7 @@ data structures, see the [schema](schema.md) documentation for a detail
description. Use the inline code annotations to get a brief synopsis of each
part of the schema.
```c title="monster.fbs" linenums="1"
```proto title="monster.fbs" linenums="1"
// Example IDL file for our monster's schema.
namespace MyGame.Sample; //(1)!
@@ -163,12 +163,103 @@ serializing and deserializing the flatbuffer binary data.
flatc --cpp monster.fbs
```
=== "C"
!!! Note
If you're working in C, you need to use the separate project
[FlatCC](https://github.com/dvidelabs/flatcc) which contains a schema
compiler and runtime library in C for C. See
[flatcc build instructions](https://github.com/dvidelabs/flatcc#building).
Please be aware of the difference between `flatc` and `flatcc` tools.
```sh
cd flatcc
mkdir -p build/tmp/samples/monster
bin/flatcc -a -o build/tmp/samples/monster samples/monster/monster.fbs
# or just
flatcc/samples/monster/build.sh
```
=== "C#"
```sh
flatc --csharp monster.fbs
```
=== "Dart"
```sh
flatc --dart monster.fbs
```
=== "Go"
```sh
flatc --go monster.fbs
```
=== "Java"
```sh
flatc --java monster.fbs
```
=== "JavaScript"
```sh
flatc --js monster.fbs
```
=== "Kotlin"
```sh
flatc --kotlin monster.fbs
```
=== "Lobster"
```sh
flatc --lobster monster.fbs
```
=== "Lua"
```sh
flatc --lua monster.fbs
```
=== "PHP"
```sh
flatc --php monster.fbs
```
=== "Python"
```sh
flatc --python monster.fbs
```
=== "Rust"
```sh
flatc --rust monster.fbs
```
=== "Swift"
```sh
flatc --swift monster.fbs
```
=== "TypeScript"
```sh
flatc --ts monster.fbs
```
You can deserialize flatbuffers in languages that differ from the language that
serialized it. For purpose of this tutorial, we assume one language is used for
both serializing and deserializing.
@@ -192,6 +283,19 @@ generally involves two things:
using namespace MyGame::Sample; // Specified in the schema.
```
=== "C"
```c
#include "monster_builder.h" // Generated by `flatcc`.
// Convenient namespace macro to manage long namespace prefix.
#undef ns
#define ns(x) FLATBUFFERS_WRAP_NAMESPACE(MyGame_Sample, x) // Specified in the schema.
// A helper to simplify creating vectors from C-arrays.
#define c_vec_len(V) (sizeof(V)/sizeof((V)[0]))
```
=== "C#"
```c#
@@ -199,6 +303,156 @@ generally involves two things:
using MyGame.Sample; // The generated files from `flatc`
```
=== "Dart"
```dart
import 'package:flat_buffers/flat_buffers.dart' as fb;
// Generated by `flatc`.
import 'monster_my_game.sample_generated.dart' as myGame;
```
=== "Go"
```go
import (
flatbuffers "github.com/google/flatbuffers/go"
sample "MyGame/Sample"
)
```
=== "Java"
```java
import MyGame.Sample.*; //The `flatc` generated files. (Monster, Vec3, etc.)
import com.google.flatbuffers.FlatBufferBuilder;
```
=== "JavaScript"
```javascript
// The following code is an example - use your desired module flavor by
// transpiling from TS.
var flatbuffers = require('/js/flatbuffers').flatbuffers;
var MyGame = require('./monster_generated').MyGame; // Generated by `flatc`.
//--------------------------------------------------------------------------//
// The following code is for browser-based HTML/JavaScript. Use the above code
// for JavaScript module loaders (e.g. Node.js).
<script src="../js/flatbuffers.js"></script>
<script src="monster_generated.js"></script> // Generated by `flatc`.
```
=== "Kotlin"
```kotlin
import MyGame.Sample.* //The `flatc` generated files. (Monster, Vec3, etc.)
import com.google.flatbuffers.FlatBufferBuilder
```
=== "Lobster"
```lobster
import from "../lobster/" // Where to find flatbuffers.lobster
import monster_generated
```
=== "Lua"
```lua
-- require the flatbuffers module
local flatbuffers = require("flatbuffers")
-- require the generated files from `flatc`.
local color = require("MyGame.Sample.Color")
local equipment = require("MyGame.Sample.Equipment")
local monster = require("MyGame.Sample.Monster")
local vec3 = require("MyGame.Sample.Vec3")
local weapon = require("MyGame.Sample.Weapon")
```
=== "PHP"
```php
// It is recommended that your use PSR autoload when using FlatBuffers in PHP.
// Here is an example from `SampleBinary.php`:
function __autoload($class_name) {
// The last segment of the class name matches the file name.
$class = substr($class_name, strrpos($class_name, "\\") + 1);
// `flatbuffers` root.
$root_dir = join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__))));
// Contains the `*.php` files for the FlatBuffers library and the `flatc`
// generated files.
$paths = array(join(DIRECTORY_SEPARATOR, array($root_dir, "php")),
join(DIRECTORY_SEPARATOR,
array($root_dir, "samples", "MyGame", "Sample")));
foreach ($paths as $path) {
$file = join(DIRECTORY_SEPARATOR, array($path, $class . ".php"));
if (file_exists($file)) {
require($file);
break;
}
}
}
```
=== "Python"
```py
import flatbuffers
# Generated by `flatc`.
import MyGame.Sample.Color
import MyGame.Sample.Equipment
import MyGame.Sample.Monster
import MyGame.Sample.Vec3
import MyGame.Sample.Weapon
```
=== "Rust"
```rust
// import the flatbuffers runtime library
extern crate flatbuffers;
// import the generated code
#[allow(dead_code, unused_imports)]
#[path = "./monster_generated.rs"]
mod monster_generated;
pub use monster_generated::my_game::sample::{root_as_monster,
Color, Equipment,
Monster, MonsterArgs,
Vec3,
Weapon, WeaponArgs};
```
=== "Swift"
```swift
/**
// make sure that monster_generated.swift is included in your project
*/
import Flatbuffers
// typealiases for convenience
typealias Monster = MyGame1_Sample_Monster
typealias Weapon = MyGame1_Sample_Weapon
typealias Color = MyGame1_Sample_Color
typealias Vec3 = MyGame1_Sample_Vec3
```
=== "TypeScript"
```ts
// note: import flatbuffers with your desired import method
import { MyGame } from './monster_generated';
```
For some languages the runtime libraries are just code files you compile into
your application. While other languages provide packaged libraries via their
package managers.