Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dart] fix bug which generated wrong code #8259

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/idl_gen_dart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ class DartGenerator : public BaseGenerator {
DartKeywords()) {}

template<typename T>
void import_generator(const std::vector<T *> &definitions,
void import_generator(const std::string &current_namespace,
const std::vector<T *> &definitions,
const std::string &included,
std::set<std::string> &imports) {
for (const auto &item : definitions) {
Expand All @@ -100,10 +101,11 @@ class DartGenerator : public BaseGenerator {
std::string filename =
namer_.File(filebase + (component.empty() ? "" : "_" + component));

std::string rename_namespace = component == current_namespace ? "" : component;
imports.emplace("import './" + filename + "'" +
(component.empty()
(rename_namespace.empty()
? ";\n"
: " as " + ImportAliasName(component) + ";\n"));
: " as " + ImportAliasName(rename_namespace) + ";\n"));
}
}
}
Expand All @@ -116,20 +118,6 @@ class DartGenerator : public BaseGenerator {
GenerateEnums(namespace_code);
GenerateStructs(namespace_code);

std::set<std::string> imports;

for (const auto &included_file : parser_.GetIncludedFiles()) {
if (included_file.filename == parser_.file_being_parsed_) continue;

import_generator(parser_.structs_.vec, included_file.filename, imports);
import_generator(parser_.enums_.vec, included_file.filename, imports);
}

std::string import_code = "";
for (const auto &file : imports) { import_code += file; }

import_code += import_code.empty() ? "" : "\n";

for (auto kv = namespace_code.begin(); kv != namespace_code.end(); ++kv) {
code.clear();
code = code + "// " + FlatBuffersGeneratedWarning() + "\n";
Expand All @@ -152,8 +140,19 @@ class DartGenerator : public BaseGenerator {
}

code += "\n";
code += import_code;
std::set<std::string> imports;
for (const auto &included_file : parser_.GetIncludedFiles()) {
if (included_file.filename == parser_.file_being_parsed_) continue;

import_generator(kv->first, parser_.structs_.vec,
included_file.filename, imports);
import_generator(kv->first, parser_.enums_.vec, included_file.filename,
imports);
}

for (const auto &import_code : imports) { code += import_code; }

code += "\n";
code += kv->second;

if (!SaveFile(Filename(kv->first).c_str(), code, false)) { return false; }
Expand Down Expand Up @@ -386,7 +385,14 @@ class DartGenerator : public BaseGenerator {
} else if (type.enum_def->is_union) {
return "dynamic";
} else if (type.base_type != BASE_TYPE_VECTOR) {
return namer_.Type(*type.enum_def);
const std::string cur_namespace = namer_.Namespace(*current_namespace);
std::string enum_namespace =
namer_.Namespace(*type.enum_def->defined_namespace);
std::string typeName = namer_.Type(*type.enum_def);
if (enum_namespace != "" && enum_namespace != cur_namespace) {
typeName = enum_namespace + "." + typeName;
}
return typeName;
}
}

Expand Down
Loading