Comments (8)
So you want the types to store their union enum as part of their definition? The problem with that is that a type can be part of multiple unions. Also, the advantage over using the current enums doesn't seem that great to me.
from flatbuffers.
What i meant by type is not a union enum but sort of an ID assigned by the user or the compiler to tables and structs like the field id. Because if two peers are exchanging messages, then there is no way to know what type of message is contained in the data if there is no ID in the data (header).
from flatbuffers.
To distinguish multiple types of messages is what the union feature was designed for.. why not use it?
Alternatively, if you want a more free-form solution, use the file_identifier feature.
from flatbuffers.
Does the union accept structs now? or will it does?
May be i will use the file_identifier or use my own IDs.
from flatbuffers.
It does not support structs (structs are inline, so they are not size-compatible with tables).
from flatbuffers.
I think the file_id works with only one table, so how to identify other tables?
I end up using my own IDs class and removed the wrapping Message table defined above.
So my code now looks as follows
public class MessageType{
public static final int CHALLENGE=1;
public static final int POINT=2;
}
and use the builder as follows:
FlatBufferBuilder fbb = new FlatBufferBuilder(100);
Challenge.startChallenge(fbb);
Challenge.addX(fbb, x);
Challenge.addY(fbb, y);
Challenge.addToken(fbb, t);
int c = Challenge.endChallenge(fbb);
fbb.finish(c);
fbb.putInt(MessageTyte.CHALLENGE)
so after I call fbb.finish() I then call fbb.putInt(type) for example to add the message type;
For parsing, now i use something like:
int t=bb.getInt(0);
int offset=4;
switch(t){
case MessageType.POINT:
Point p=Point.getRootAsPoint(bb, offset);
processPoint(p);
break;
case MessageType.CHALLENGE:
Challenge ch = Challenge.getRootAsChallenge(bb, offset)
processChallenge(ch);
break;
}
The reverse writing in the ByteBuffer is a very good idea as you can see, wee can add header information without the need to copy in new buffer, and use only one write operation later to write the whole message to the socket/file. I am also planning to add the "length" of the message the same way as adding the type of message, like that i will know how many bytes to read before start parsing them, especially if the stream contains many messages.
from flatbuffers.
With file_identifier, you can simply call any number of XBufferHasIdentifier() calls to determine which type it is?
since you cannot use structs as the root of a FlatBuffer either, why not use unions as intended? that is much more compatible than your putInt solution (at least use addInt, as putInt may go out of bounds).
from flatbuffers.
Ok thanks, I will try that.
from flatbuffers.
Related Issues (20)
- Is it binary-compatible to delete (required) attribute?
- [Python] Use 'T' suffix object for serialization
- [swift] distinguish unknown from absent enum value for scheme evolution HOT 5
- undocumented breaking change GenerateText->GenText
- [Java, FlatBuffers 23.5.26, Mac OS 14.3] getByKey returns null while accessing using vector index works
- `get_root_as_*` functions not generated [Rust, 23.5.26, Linux] HOT 1
- Generating schemas with language adapters (as optional add-ons)?
- Streaming flatbuffers in java HOT 1
- [TS] optional escalar not set in the binary when it holds value 0 HOT 13
- Release a new version, please HOT 1
- Default value of 0 for field is not part of [optional] enum (java)
- Buffer generated in python gets rejected by flatcc [Python / C, Flatbuffer 23.5.26]
- Invalid Code generation if Property has same name as Struct [Windows/C#/24.3.7]
- v24.3.7 of Rust crate not released to crates.io HOT 4
- [v24.3.7] Rust: push_u8_u64_alignment test fails on i686 target (Ubuntu 22.04, CentOS 9)
- How to print nested table using FlatBufferToString?
- [Python] Flatc stub compatibility with gRPC asyncio Interface
- https://youtube.com/@Ulfat3215?si=1fAriKOReP56TyxO
- https://youtube.com/@Ulfat3215?si=1fAriKOReP56TyxO
- [Rust] Add better support for "crate-per-schema" HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flatbuffers.