Flatbuffers using Golang Java Guidelines

Source: Internet
Author: User
Tags deprecated
This is a creation in Article, where the information may have evolved or changed.

[TOC]

Defining IDL Model Files

The Flatbuffers model interface definition file suffix is.fbs

FBS syntax

Basic syntax

Statement Use ; End
Structure used {} to qualify
Use [] to specify a custom type

Example

namespace com.my.event;table Event{    touch : [Touch];}table Touch{    x : int(id: 0);    y : int(id: 1);}root_type Event;

Key words

Key Words Description and Purpose
/// Flatbuffers uses "//" to express comments, and this note is brought into the generated source file
Namespace Model Catalog, Package structure
Table The model class identifies a class that generates a corresponding identity for a single model file
bool Short int float string Default data type Keywords
Enum Enumeration class data definition
Union To generate a separate enumeration class
Deprecated Specify deprecated, which can be removed by deleting this field
Priority Set Priority
Root_type A function that generates GETROOTASXXX () in the file generated by its table, except that the field in the table generates a related function.
Required Each field of a struct that is required,table is optional by default, but can be specified as required
Id Sets the order in which the build is specified, requiring that all of this table be written to take effect.
Include Introducing another FBS content

Example

namespace MyGame;attribute "priority";enum Color : byte { Red = 1, Green, Blue }///union Any { Monster, Weapon, Pickup }//union Any { Monster}union Any { Monster, Weapon}struct Vec3 {  x:float;  y:float;  z:float;}/// 注释table Monster {  pos:Vec3;  mana:short = 150;  hp:short = 100;  name:string;  friendly:bool = false (deprecated, priority: 1);  inventory:[ubyte];  color:Color = Blue;  test:Any;}table Weapon {  pos:Vec3;  mana:short = 150;}root_type Monster;root_type Weapon;

FBS tips

///直接嵌套structtable Monster { pos:Vec3; ...}/// 直接指定数据类型及默认值mana:short = 150;/// 指定deprecated,可以删除掉此字段friendly:bool = false (deprecated, priority: 1);/// 加id指定生成顺序mana:short = 150 (id: 3); /// include "include_test1.fbs"; 形式,将其它.fbs文件嵌套进来

If an ID is added, all fields in the table must be ID to be passed

Generate model files for the corresponding language

Using flatc a model file that generates the corresponding language

  --cpp        -c Generate C++ headers for tables/structs.  --go         -g Generate Go files for tables/structs.  --java       -j Generate Java classes for tables/structs.  --js         -s Generate JavaScript code for tables/structs.  --csharp     -n Generate C# classes for tables/structs.  --python     -p Generate Python files for tables/structs.  --php           Generate PHP files for tables/structs.
flatc -g Test.fbs

Automatically update model file scripts

#!/usr/bin/env bashrm -rf flatbuffermkdir -p flatbuffer/javamkdir -p flatbuffer/gocd flatbuffer/javaflatc -j ../SimulationEvent.fbscd ..cd goflatc -g ../SimulationEvent.fbs

Updating the script is an example and suggests modifying it yourself ~

Use in Golang projects

Installing the Golang Support package

go get -u -v google/flatbuffers/go

Golang Official use of flatbuffers documentation http://google.github.io/flatbuffers/index.html

or use it as an example.

Https://github.com/google/flatbuffers/tree/master/samples

Java use (same as in Android)

Introducing a dependent Library

dependencies {  compile 'com.github.davidmoten:flatbuffers-java:1.3.0.1'}

Use as examples
Https://github.com/google/flatbuffers/blob/master/samples/SampleBinary.java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.