Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why the need for a Root #65

Closed
lbensaad opened this issue Sep 3, 2014 · 10 comments
Closed

Why the need for a Root #65

lbensaad opened this issue Sep 3, 2014 · 10 comments

Comments

@lbensaad
Copy link
Contributor

lbensaad commented Sep 3, 2014

What is the reason of using a root type. If i have Request and Response tables to be sent between two parties, so why we could not create them separately. A buffer could contain either a Request or a Response. In fact any message(Table) should be able to a root in a buffer.

@ghost
Copy link

ghost commented Sep 3, 2014

Any table can be a root in a buffer. The root type is mostly for use by JSON, which doesn't specify any types, so we need to know where to start. The generated GetRoot functions are merely convenience.

@lbensaad
Copy link
Contributor Author

lbensaad commented Sep 4, 2014

Ok, thanks. But in Java,

  1. If i have a ByteBuffer of a non root table how do read that using FlatBuffersBuilder. The only way I know is to use the __init() function, is there another way?
  2. Is there a way to know the type of the table from the binary data?
  3. How to use the addXXXType function, in your JavaTest.java you used: Monster.addTestType(fbb, (byte)1); how do you get the type of a table, in this case 1 which it is hard-coded ?

@ghost
Copy link

ghost commented Sep 4, 2014

  1. a commit was pushed yesterday that adds GetRootAs functions for all tables, not just the root_type.

  2. generally no. this is a strongly types system, meaning you need to know the kind of buffer you're dealing with. If you want to use this in a context where you want to have multiple different root types, you have these options:
    a) make your root type a table that contains a union of all possible sub-roots.
    b) prefix flatbuffers with your own file header
    c) use flatbuffer's built-in file indentification feature, which hasn't been ported to Java yet. I'll get to that.

  3. That's a bug, the 1 should actually read: Any.Monster. I'll fix that.

@lbensaad
Copy link
Contributor Author

lbensaad commented Sep 5, 2014

Thanks

  1. But i think the name should be just "get" or "init" instead of getRootAs

@ghost
Copy link

ghost commented Sep 5, 2014

getRootAs is a bit more descriptive and less likely to clash with other identifiers. I think I'll keep it for now.

To further my response to your issue #2, the built-in file_identifier functionality is now also available in Java. See the Schema docs, Java docs, and Java code.

@ghost ghost closed this as completed Sep 5, 2014
@let4be
Copy link

let4be commented Jan 2, 2016

So... How do I access non root_type buffers, can't find anything like Get*** in c++ generated files and the same problem in go lang.
generated code has huge differencies between languages and it's really hard to work with

@let4be
Copy link

let4be commented Jan 2, 2016

Tried both latest master and latest release versions

@ghost
Copy link

ghost commented Jan 4, 2016

In C++ there is the GetRoot templated function that works with any table type.
Go appears to still be pending on: #363

kakikubo pushed a commit to kakikubo/flatbuffers that referenced this issue Apr 19, 2016
@gaurimaheshwari
Copy link

please explain me how to build flatc executable?

@aardappel
Copy link
Collaborator

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants