Correctly parse lists of enums in Dart generated code (#7157)

* Correctly parse lists of enums in Dart generated code

* Add a test for #6869

* Commit generated code

* Fixed missing newline-at-eof
This commit is contained in:
Will Hughes
2022-03-23 17:56:14 +13:00
committed by GitHub
parent 23a7e4e0b0
commit 5a13f622cf
5 changed files with 48 additions and 2 deletions

View File

@@ -308,7 +308,7 @@ class DartGenerator : public BaseGenerator {
"> {\n";
code += " const _" + name + "Reader();\n\n";
code += " @override\n";
code += " int get size => 1;\n\n";
code += " int get size => " + EnumSize(enum_def.underlying_type) + ";\n\n";
code += " @override\n";
code +=
" " + name + " read(" + _kFb + ".BufferContext bc, int offset) =>\n";
@@ -339,6 +339,24 @@ class DartGenerator : public BaseGenerator {
}
}
static std::string EnumSize(const Type &type) {
switch (type.base_type) {
case BASE_TYPE_BOOL:
case BASE_TYPE_CHAR:
case BASE_TYPE_UTYPE:
case BASE_TYPE_UCHAR: return "1";
case BASE_TYPE_SHORT:
case BASE_TYPE_USHORT: return "2";
case BASE_TYPE_INT:
case BASE_TYPE_UINT:
case BASE_TYPE_FLOAT: return "4";
case BASE_TYPE_LONG:
case BASE_TYPE_ULONG:
case BASE_TYPE_DOUBLE: return "8";
default: return "1";
}
}
std::string GenReaderTypeName(const Type &type, Namespace *current_namespace,
const FieldDef &def,
bool parent_is_vector = false, bool lazy = true,