flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

[C++] Parser::SupportsVectorOfUnions

Open medwards opened this issue 8 years ago • 14 comments

When creating a flatbuffers::Parser for serializing or deserializing JSON you can get an error stating: Invalid flatbuffers schema: <lineno>: error: only tables can be union elements in the generated language with the default options and the following sample schema:

namespace foo;

struct Checkpoint
{
    value:short;
}

table Marker
{
}

union Position
{
    Start:Marker,
    Anchor:Checkpoint,
    End:Marker,
}

table Racer {
    name:string;
    position:Position;
}

Most of what I've seen about how to deal with JSON and flatbuffers involve just creating a Parser and then calling Parse on your schema as a string then using the resulting schema parser for serialization (with GenerateText) and deserialization (with yet another Parse).

If the original Parser gets a valid language in the lang_to_generate option then everything works as expected, but if it is unitialized (0) which it is if its not being used for generation, then SupportsVectorOfUnions is false ( https://github.com/google/flatbuffers/blob/master/src/idl_parser.cpp#L1722 ). Maybe this is expected behaviour, but I would have expected the lang option to control this. More broadly the distinction between lang and lang_to_generate is pretty unclear to me so maybe there's no bug but just a misunderstanding on my part.

For now we can easily work around this error by setting the lang_to_generate to kCpp but it feels a bit weird since we won't be generating any headers at all.

medwards avatar Oct 15 '18 12:10 medwards

Since it has an explicit check for 0 I guess it was intentional, though I do not remember why.

The difference appears to be that lang is the current language being generated, and lang_to_generate is all languages for the current flatc invocation. I am guessing that was needed because when it parses the schema, it does not know yet which languages it will be used for.

It doesn't look like parsing and generating of vectors of unions is working (just looking at the code)?

aardappel avatar Oct 15 '18 22:10 aardappel

If I correctly understood when converting from FB to JSON the language is set to kJson and when doing inverse conversion language is kBinary. At least when invoking flatc with t/--json and -b/--binary command lines options. I'd expect both of these "languages" support struct as union member. Maybe the fix would be to add another function and replace SupportsVectorOfUnions in this check as in the end this has even nothing to do with vector of unions.

zejal avatar Nov 12 '18 16:11 zejal

Yes, support for structs in unions and vectors of unions should probably be tested seperately. Care to fix this?

aardappel avatar Nov 12 '18 17:11 aardappel

Which languages do not support structs as union members? I do not know that list unfortunately. Also what's the use case lang_to_generate == 0 ?

zejal avatar Nov 12 '18 17:11 zejal

Well, the parser can be called from flatc (in which case we wan't errors for unsupported features) and at runtime by any program that links it in for say JSON generation (in which case we don't want these errors since no such flags will be set). Unless we'd require the latter users to specify the kJson or kBinary flags, but that might be backwards incompatible.

aardappel avatar Nov 12 '18 18:11 aardappel

Sorry but it is not clear at all what the supposedly SupportsStructInUnion() (or other more adéquate name) function might do I am afraid.

zejal avatar Nov 12 '18 18:11 zejal

Can you say what is not clear? I thought my comments here explained it.

aardappel avatar Nov 12 '18 18:11 aardappel

The current code is:

    bool Parser::SupportsVectorOfUnions() const {
      return opts.lang_to_generate != 0 &&
             (opts.lang_to_generate & ~(IDLOptions::kCpp | IDLOptions::kJs |
                                        IDLOptions::kTs | IDLOptions::kPhp |
                                        IDLOptions::kJava | IDLOptions::kCSharp)) == 0;
   }

What is not clear is what's the list of languages which do not support structs in unions? What is not clear too is the case where opts.lang_to_generate == 0.

zejal avatar Nov 12 '18 18:11 zejal

Since the same function call is used for both, I must assume that it is the same set of languages. But that be worth verifying.

As for the == 0, that's what I was referring to here:

Well, the parser can be called from flatc (in which case we wan't errors for unsupported features) and at runtime by any program that links it in for say JSON generation (in which case we don't want these errors since no such flags will be set). Unless we'd require the latter users to specify the kJson or kBinary flags, but that might be backwards incompatible.

aardappel avatar Nov 12 '18 20:11 aardappel

My knowledge in FlatBuffer is not sufficient to verify this. I am pretty sure that kBinary supports structs Inside unions because FlatBuffers supports that. For kJson it should also be the case, but not 100% sure. Where in the code this can checked please?

zejal avatar Nov 12 '18 21:11 zejal

To see if a language supports things other than tables in a union, you'd have to check the corresponding code generator and see if the code it generates for accessing that union deals with multiple types properly.

That, or more simply, see if the language works with the schema in tests/union_vector. It appears that currently that dir contains code for C++/C#/Java/PHP/JS/TS (and pretty sure C supports it too). If the tests for the given language import files in this dir, it is pretty sure the feature is supported.

Since the test is the same, it appears these languages have always supported both "vector of union" and "union of struct/string" at the same time. So it may actually be sufficient renaming SupportsVectorOfUnions into SupportsAdvancedUnionFeatures for now?

aardappel avatar Nov 15 '18 20:11 aardappel

Is there already a PR for supporting vectors of unions in JSON? It seems 1.10 still doesn't support this but there was a reference for adding a change for that back in PR 4143 in Jan 2017.

dalnew avatar Jan 30 '19 18:01 dalnew

@dalnew I don't think there is, no. Can you make one?

https://github.com/google/flatbuffers/pull/4143

aardappel avatar Jan 31 '19 18:01 aardappel

This issue has been automatically marked as stale because it has not had activity for 1 year. It will be automatically closed if no further activity occurs. To keep it open, simply post a new comment. Maintainers will re-open on new activity. Thank you for your contributions.

stale[bot] avatar Jan 31 '20 18:01 stale[bot]