mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-06 05:27:24 +00:00
Add --require-explicit-ids to require explicit ids (#6277)
* Add --require-explicit-ids to require explicit ids We just got bit by a well intentioned developer forgetting that field order by default is the field index. 3 people missed it in review. I'm looking at ways to make it harder to mess up. We are requesting that developers explicitly id all fields in tables. Automatic (opt in for others) enforcement of this will help the effort succeed. This patch adds a command line flag which lets the user require ids on all fields in tables. * Added docs to Compiler.md as well
This commit is contained in:
@@ -2379,11 +2379,18 @@ CheckedError Parser::ParseDecl() {
|
||||
if ((*it)->attributes.Lookup("id")) num_id_fields++;
|
||||
}
|
||||
// If any fields have ids..
|
||||
if (num_id_fields) {
|
||||
if (num_id_fields || opts.require_explicit_ids) {
|
||||
// Then all fields must have them.
|
||||
if (num_id_fields != fields.size())
|
||||
return Error(
|
||||
"either all fields or no fields must have an 'id' attribute");
|
||||
if (num_id_fields != fields.size()) {
|
||||
if (opts.require_explicit_ids) {
|
||||
return Error(
|
||||
"all fields must have an 'id' attribute when "
|
||||
"--require-explicit-ids is used");
|
||||
} else {
|
||||
return Error(
|
||||
"either all fields or no fields must have an 'id' attribute");
|
||||
}
|
||||
}
|
||||
// Simply sort by id, then the fields are the same as if no ids had
|
||||
// been specified.
|
||||
std::sort(fields.begin(), fields.end(), compareFieldDefs);
|
||||
|
||||
Reference in New Issue
Block a user