mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-04 20:48:59 +00:00
Add advance feature indicators to reflection (#6546)
* Add advance feature indicators to reflection * deserialize too * model advanced features as bitflags * use uint64_t instead of AdvancedFeatures * git clang format * initialize advanced_features_ * remove whitespace Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
@@ -765,10 +765,13 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
|
||||
if (!struct_def.fixed && IsArray(type))
|
||||
return Error("fixed-length array in table must be wrapped in struct");
|
||||
|
||||
if (IsArray(type) && !SupportsAdvancedArrayFeatures()) {
|
||||
return Error(
|
||||
"Arrays are not yet supported in all "
|
||||
"the specified programming languages.");
|
||||
if (IsArray(type)) {
|
||||
advanced_features_ |= reflection::AdvancedArrayFeatures;
|
||||
if (!SupportsAdvancedArrayFeatures()) {
|
||||
return Error(
|
||||
"Arrays are not yet supported in all "
|
||||
"the specified programming languages.");
|
||||
}
|
||||
}
|
||||
|
||||
FieldDef *typefield = nullptr;
|
||||
@@ -778,6 +781,7 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
|
||||
ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(),
|
||||
type.enum_def->underlying_type, &typefield));
|
||||
} else if (IsVector(type) && type.element == BASE_TYPE_UNION) {
|
||||
advanced_features_ |= reflection::AdvancedUnionFeatures;
|
||||
// Only cpp, js and ts supports the union vector feature so far.
|
||||
if (!SupportsAdvancedUnionFeatures()) {
|
||||
return Error(
|
||||
@@ -802,11 +806,16 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
|
||||
return Error(
|
||||
"default values are not supported for struct fields, table fields, "
|
||||
"or in structs.");
|
||||
if ((IsString(type) || IsVector(type)) && field->value.constant != "0" &&
|
||||
field->value.constant != "null" && !SupportsDefaultVectorsAndStrings())
|
||||
return Error(
|
||||
"Default values for strings and vectors are not supported in one of "
|
||||
"the specified programming languages");
|
||||
if (IsString(type) || IsVector(type)) {
|
||||
advanced_features_ |= reflection::DefaultVectorsAndStrings;
|
||||
if (field->value.constant != "0" && field->value.constant != "null" &&
|
||||
!SupportsDefaultVectorsAndStrings()) {
|
||||
return Error(
|
||||
"Default values for strings and vectors are not supported in one "
|
||||
"of the specified programming languages");
|
||||
}
|
||||
}
|
||||
|
||||
if (IsVector(type) && field->value.constant != "0" &&
|
||||
field->value.constant != "[]") {
|
||||
return Error("The only supported default for vectors is `[]`.");
|
||||
@@ -891,6 +900,7 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
|
||||
}
|
||||
|
||||
if (field->IsScalarOptional()) {
|
||||
advanced_features_ |= reflection::OptionalScalars;
|
||||
if (type.enum_def && type.enum_def->Lookup("null")) {
|
||||
FLATBUFFERS_ASSERT(IsInteger(type.base_type));
|
||||
return Error(
|
||||
@@ -3498,7 +3508,8 @@ void Parser::Serialize() {
|
||||
auto serv__ = builder_.CreateVectorOfSortedTables(&service_offsets);
|
||||
auto schema_offset = reflection::CreateSchema(
|
||||
builder_, objs__, enum__, fiid__, fext__,
|
||||
(root_struct_def_ ? root_struct_def_->serialized_location : 0), serv__);
|
||||
(root_struct_def_ ? root_struct_def_->serialized_location : 0), serv__,
|
||||
static_cast<reflection::AdvancedFeatures>(advanced_features_));
|
||||
if (opts.size_prefixed) {
|
||||
builder_.FinishSizePrefixed(schema_offset, reflection::SchemaIdentifier());
|
||||
} else {
|
||||
@@ -3915,7 +3926,7 @@ bool Parser::Deserialize(const reflection::Schema *schema) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
advanced_features_ = schema->advanced_features();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user