-
Notifications
You must be signed in to change notification settings - Fork 667
Add schema aliases. Use aliases in rename-only deprecations. #6310
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
base: main
Are you sure you want to change the base?
Changes from all commits
f8343c4
3209848
a798cc2
faba1a2
7575f39
285ca8a
69be8b7
8fb4cd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,11 +27,20 @@ | |
|
|
||
| namespace dali { | ||
|
|
||
| std::map<string, OpSchema, std::less<>> &SchemaRegistry::registry() { | ||
| namespace { | ||
|
|
||
| std::map<string, OpSchema, std::less<>> ®istry() { | ||
| static std::map<string, OpSchema, std::less<>> schema_map; | ||
| return schema_map; | ||
| } | ||
|
|
||
| std::map<string, string, std::less<>> &aliases() { | ||
| static std::map<string, string, std::less<>> alias_map; | ||
| return alias_map; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| OpSchema &SchemaRegistry::RegisterSchema(std::string_view name) { | ||
| auto &schema_map = registry(); | ||
|
|
||
|
|
@@ -46,20 +55,52 @@ OpSchema &SchemaRegistry::RegisterSchema(std::string_view name) { | |
| } | ||
|
|
||
| const OpSchema &SchemaRegistry::GetSchema(std::string_view name) { | ||
| auto &schema_map = registry(); | ||
| auto it = schema_map.find(name); | ||
| if (it == schema_map.end()) | ||
| if (auto *schema = TryGetSchema(name)) | ||
| return *schema; | ||
| else | ||
| throw invalid_key("Schema for operator '" + std::string(name) + "' not registered"); | ||
|
|
||
| return it->second; | ||
| } | ||
|
|
||
| const OpSchema *SchemaRegistry::TryGetSchema(std::string_view name) { | ||
| auto &schema_map = registry(); | ||
| auto it = schema_map.find(name); | ||
| return it != schema_map.end() ? &it->second : nullptr; | ||
| if (it == schema_map.end() || !it->second.AliasFor().empty()) { | ||
| auto &alias_map = aliases(); | ||
| auto alias_it = alias_map.find(name); | ||
| if (alias_it != alias_map.end()) { | ||
| name = alias_it->second; | ||
| it = schema_map.find(name); | ||
| } | ||
| } | ||
|
|
||
| if (it == schema_map.end()) | ||
| return nullptr; | ||
|
|
||
| return &it->second; | ||
| } | ||
|
mzient marked this conversation as resolved.
|
||
|
|
||
| void SchemaRegistry::AddAlias(std::string_view alias_name, std::string_view actual_name) { | ||
| if (alias_name == actual_name) | ||
| throw std::invalid_argument("Schema name self-aliasing is forbidden"); | ||
|
|
||
| auto &alias_map = aliases(); | ||
| auto previous_target_it = alias_map.find(alias_name); | ||
| if (previous_target_it != alias_map.end()) | ||
| throw std::invalid_argument(make_string("\"", alias_name, | ||
| "\" is already used as a schema alias name for \"", previous_target_it->second, "\"")); | ||
|
|
||
| for (;;) { | ||
| auto redir = alias_map.find(actual_name); | ||
| if (redir == alias_map.end()) | ||
| break; | ||
| if (redir->second == alias_name) | ||
| throw std::invalid_argument("Cycle detected while adding schema alias."); | ||
| actual_name = redir->second; | ||
| } | ||
|
Comment on lines
+92
to
+99
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The This is not a problem today because
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @greptileai Path compression might fail - but will cycle detection still work?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, cycle detection still works correctly regardless of insertion order. The loop traverses Consider the failure case for path compression:
In step 3, the loop does:
The loop doesn't rely on pre-compressed paths — it re-walks The only real outstanding issue is that |
||
| alias_map[std::string(alias_name)] = actual_name; | ||
| } | ||
|
|
||
|
|
||
| const OpSchema &OpSchema::Default() { | ||
| static OpSchema default_schema(DefaultSchemaTag{}); | ||
| return default_schema; | ||
|
|
@@ -380,6 +421,14 @@ OpSchema &OpSchema::Deprecate(std::string version, std::string in_favor_of, | |
| return *this; | ||
| } | ||
|
|
||
| OpSchema &OpSchema::AliasFor(std::string_view actual_name) { | ||
| DALI_ENFORCE(alias_for_.empty(), make_string( | ||
| "The schema \"", name_, "\" is already an alias for \"", alias_for_, "\"")); | ||
|
|
||
| alias_for_ = actual_name; | ||
| SchemaRegistry::AddAlias(name_, actual_name); | ||
| return *this; | ||
| } | ||
|
|
||
| OpSchema &OpSchema::Unserializable() { | ||
| serializable_ = false; | ||
|
|
@@ -826,6 +875,9 @@ const std::string &OpSchema::DeprecatedInFavorOf() const { | |
| return deprecated_in_favor_of_; | ||
| } | ||
|
|
||
| const std::string &OpSchema::AliasFor() const { | ||
| return alias_for_; | ||
| } | ||
|
|
||
| const std::string &OpSchema::DeprecationMessage() const { | ||
| return deprecation_message_; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.