-
Notifications
You must be signed in to change notification settings - Fork 3
feat: id validation #404
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
Open
azfoo
wants to merge
2
commits into
dev
Choose a base branch
from
id-validation
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: id validation #404
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -275,28 +275,44 @@ def on_record_state(self, action, no_repeat=False, repeat_identifier=""): | |
| repeat_identifier=repeat_identifier, | ||
| ) | ||
|
|
||
| def get_id(self) -> str: | ||
| def get_id(self, id: str | None = None) -> str: | ||
| """ | ||
| Returns an ID string. | ||
| IDs are unique accross timeline component and timelines. | ||
| IDs are unique across timeline component and timelines. | ||
| Other IDs might contain duplicates. | ||
| """ | ||
| timeline_ids = {c.id for c in self.timelines} | ||
|
|
||
| def invalid_id(id): | ||
| tilia.errors.display(tilia.errors.INVALID_ID, id) | ||
| return str(next(self._id_counter)) | ||
|
|
||
| if id is None: | ||
| return str(next(self._id_counter)) | ||
|
|
||
| if type(id) not in [int, str]: | ||
| return invalid_id(id) | ||
|
|
||
| try: | ||
| int_id = int(id) | ||
| except ValueError: | ||
| return invalid_id(id) | ||
|
|
||
| timeline_ids = {int(c.id) for c in self.timelines} | ||
| component_lists = [ | ||
| tl.components for tl in self.timelines if tl.components is not None | ||
| ] | ||
| component_ids = set() | ||
| for component_list in component_lists: | ||
| component_ids = component_ids.union({c.id for c in component_list}) | ||
| component_ids = { | ||
| int(c.id) for component_list in component_lists for c in component_list | ||
| } | ||
| existing_ids = timeline_ids.union(component_ids) | ||
|
|
||
| next_id = next(self._id_counter) | ||
| # Find the highest existing ID and increment until counter is higher | ||
| if existing_ids: | ||
| highest_id = max(int(id_str) for id_str in existing_ids) | ||
| while next_id <= highest_id: | ||
| next_id = next(self._id_counter) | ||
| return str(next_id) | ||
| if int_id in existing_ids: | ||
| return str(next(self._id_counter)) | ||
|
|
||
| if not existing_ids or int_id > max(existing_ids): | ||
| self._id_counter = itertools.count(int_id + 1) | ||
|
Comment on lines
+312
to
+313
Collaborator
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. Much better than what we had. |
||
|
|
||
| return str(int_id) | ||
|
|
||
| def reset_id_generator(self): | ||
| self._id_counter = itertools.count() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
next(self._id_counter)might already be inexisting_ids, no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, see
test_create_component_with_higher_id_before_lower_id.self._id_counteralways increments from the largest input id, so there's no way it'll be smaller.