Skip to content

Commit

Permalink
introduce .models files, crash when we can't parse them as JSON
Browse files Browse the repository at this point in the history
Summary:
Mariana Trench has multiple semantically different files it uses as part of its configuration, which all happen to be communicated as JSON. We previously didn't distinguish between these, so we try to parse and silently fail for a lot of our
model JSON files. Let's introduce a new extension (copying other static analysis tool conventions here) that handles model generators and error out if they're invalid.

Reviewed By: anwesht

Differential Revision: D47552197

fbshipit-source-id: 5833bdaa83fe5cf784cb6c1d774f903ca6b449ca
  • Loading branch information
Sinan Cepel authored and facebook-github-bot committed Jul 18, 2023
1 parent b58664e commit 54b5d64
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions source/ModelGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ make_model_generators(Context& context) {
for (const auto& path : context.options->model_generator_search_paths()) {
LOG(3, "Searching for model generators in `{}`...", path);
for (auto& entry : boost::filesystem::recursive_directory_iterator(path)) {
if (entry.path().extension() != ".json") {
if (entry.path().extension() != ".json" &&
entry.path().extension() != ".models") {
continue;
}
bool always_validate_models = entry.path().extension() == ".models";

auto path_copy = entry.path();
auto identifier = path_copy.replace_extension("").filename().string();
Expand All @@ -57,11 +59,16 @@ make_model_generators(Context& context) {
Json::Value json = JsonValidation::parse_json_file(entry.path());

if (!json.isObject()) {
// TODO(T153463464): This means it is most likely not a model
// generator (it could be a rule definition file, lifecycle
// definition file, etc.). Ignore it silently for now. In the future,
// we should use the extension to differentiate between file types.
continue;
// TODO(T153463464): We always validate .models files, but still
// accept legacy .json files that may not parse. (it could be a rule
// definition file, lifecycle definition file, etc.). Ignore it
// silently for now. In the future, we should use the extension to
// differentiate between file types.
if (!always_validate_models) {
continue;
}
throw ModelGeneratorError(fmt::format(
"Unable to parse `{}` as a valid models JSON.", entry.path()));
}

auto [_, inserted] = generators.emplace(
Expand Down

0 comments on commit 54b5d64

Please sign in to comment.