mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-10 15:16:28 +00:00
Default-empty vectors of enums (#6505)
* disable clippy * Vector of enum default * swift and tests * git clang format * Rewrite enum parser checks * Remove Voids from more_defaults * vector enum swift * remove vector accessor from swift * clang format Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
@@ -848,7 +848,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
code_.SetValue("ENUM_NAME", NameWrappedInNameSpace(enum_def));
|
||||
code_.SetValue("BASE_TYPE", GenTypeBasic(enum_def.underlying_type, false));
|
||||
GenComment(enum_def.doc_comment);
|
||||
code_ += "{{ACCESS_TYPE}} enum {{ENUM_NAME}}: {{BASE_TYPE}}, Enum { ";
|
||||
code_ += "{{ACCESS_TYPE}} enum {{ENUM_NAME}}: {{BASE_TYPE}}, Enum {";
|
||||
Indent();
|
||||
code_ += "{{ACCESS_TYPE}} typealias T = {{BASE_TYPE}}";
|
||||
code_ +=
|
||||
@@ -1472,7 +1472,9 @@ class SwiftGenerator : public BaseGenerator {
|
||||
auto &value = field.value;
|
||||
FLATBUFFERS_ASSERT(value.type.enum_def);
|
||||
auto &enum_def = *value.type.enum_def;
|
||||
auto enum_val = enum_def.FindByValue(value.constant);
|
||||
// Vector of enum defaults are always "[]" which never works.
|
||||
const std::string constant = IsVector(value.type) ? "0" : value.constant;
|
||||
auto enum_val = enum_def.FindByValue(constant);
|
||||
std::string name;
|
||||
if (enum_val) {
|
||||
name = Name(*enum_val);
|
||||
|
||||
@@ -910,24 +910,32 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
|
||||
}
|
||||
|
||||
if (type.enum_def) {
|
||||
// The type.base_type can only be scalar, union, array or vector.
|
||||
// Table, struct or string can't have enum_def.
|
||||
// Default value of union and vector in NONE, NULL translated to "0".
|
||||
FLATBUFFERS_ASSERT(IsInteger(type.base_type) ||
|
||||
(type.base_type == BASE_TYPE_UNION) || IsVector(type) ||
|
||||
IsArray(type));
|
||||
if (IsVector(type)) {
|
||||
// Vector can't use initialization list.
|
||||
FLATBUFFERS_ASSERT(field->value.constant == "0");
|
||||
// Verify the enum's type and default value.
|
||||
const std::string &constant = field->value.constant;
|
||||
if (type.base_type == BASE_TYPE_UNION) {
|
||||
if (constant != "0") { return Error("Union defaults must be NONE"); }
|
||||
} else if (IsVector(type)) {
|
||||
if (constant != "0" && constant != "[]") {
|
||||
return Error("Vector defaults may only be `[]`.");
|
||||
}
|
||||
} else if (IsArray(type)) {
|
||||
if (constant != "0") {
|
||||
return Error("Array defaults are not supported yet.");
|
||||
}
|
||||
} else {
|
||||
// All unions should have the NONE ("0") enum value.
|
||||
auto in_enum = field->IsOptional() ||
|
||||
type.enum_def->attributes.Lookup("bit_flags") ||
|
||||
type.enum_def->FindByValue(field->value.constant);
|
||||
if (false == in_enum)
|
||||
return Error("default value of " + field->value.constant +
|
||||
" for field " + name + " is not part of enum " +
|
||||
type.enum_def->name);
|
||||
if (!IsInteger(type.base_type)) {
|
||||
return Error("Enums must have integer base types");
|
||||
}
|
||||
// Optional and bitflags enums may have default constants that are not
|
||||
// their specified variants.
|
||||
if (!field->IsOptional() &&
|
||||
type.enum_def->attributes.Lookup("bit_flags") == nullptr) {
|
||||
if (type.enum_def->FindByValue(constant) == nullptr) {
|
||||
return Error("default value of `" + constant + "` for " + "field `" +
|
||||
name + "` is not part of enum `" + type.enum_def->name +
|
||||
"`.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user