Flatbuffers: Required all fields

Created on 23 Jul 2018  路  6Comments  路  Source: google/flatbuffers

I couldn't find the reason why only non-scalars can be required.
I need to require all fields as protobuf can do.
This greatly helps developing a better product.
Please add this feature

stale

Most helpful comment

I want to ensure that when FlatBuffers is created all fields are set.
In Protobuf if I forgot to set any field, I get an exception. I want the similar behavior in FB.

All 6 comments

As I understand, non-scalars if not written, will require to be nullptr checked before reading. These checks are overhead during reading. A simple solution can be to mandate non-scalars fields using Required during write.

Scalars on the other hand already provides a default value during read in case they are not written. This removes the need of mandating scalars during write.

Still if you want everything to be required in your schema, just wrap the basic scalars in struct, and use these structs with Required attribute. (There won't be overhead of structs as they are inlined).

For e.g.

struct Int
{
data: int;
}

table MyTable
{
intTest: Int (Required);
}

That's nice solution but I would prefer if it was built-in
Let's say standard type required_int32.
Or special syntax (scalar_required) which will do essentially the same.

Like @shivendra14 said there is no need to specify scalars to be required, since they are always readable (and thus @shivendra14 workaround doesn't buy you anything either).

What exactly are you trying to do?

I want to ensure that when FlatBuffers is created all fields are set.
In Protobuf if I forgot to set any field, I get an exception. I want the similar behavior in FB.

@NN I'm afraid there's currently no way to do that, and I see no easy way to implement that, as you can't tell the difference between an unset value and a set value that happens to be equal to the default (by design).

You can use features like ForceDefaults() and IsFieldPresent() in C++ to work around this, at the cost of bigger binaries, but that is at read time, not construction time.

API-wise, if you use the Create functions for a table type, those will force you to supply a parameter for each field, as opposed to using the add_field functions individually.

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.

Was this page helpful?
0 / 5 - 0 ratings