Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -106,32 +106,30 @@ def from_json(json: Any) -> AnnotationStore:
valueAnnotations,
)

def add_annotation(self, annotation: AbstractAnnotation):
def add_annotation(self, annotation: AbstractAnnotation) -> None:
if isinstance(annotation, BoundaryAnnotation):
self.boundaryAnnotations.append(annotation)
if isinstance(annotation, BoundaryAnnotation):
self.boundaryAnnotations.append(annotation)
if isinstance(annotation, CalledAfterAnnotation):
elif isinstance(annotation, CalledAfterAnnotation):
self.calledAfterAnnotations.append(annotation)
if isinstance(annotation, CompleteAnnotation):
elif isinstance(annotation, CompleteAnnotation):
self.completeAnnotations.append(annotation)
if isinstance(annotation, DescriptionAnnotation):
elif isinstance(annotation, DescriptionAnnotation):
self.descriptionAnnotations.append(annotation)
if isinstance(annotation, EnumAnnotation):
elif isinstance(annotation, EnumAnnotation):
self.enumAnnotations.append(annotation)
if isinstance(annotation, GroupAnnotation):
elif isinstance(annotation, GroupAnnotation):
self.groupAnnotations.append(annotation)
if isinstance(annotation, MoveAnnotation):
elif isinstance(annotation, MoveAnnotation):
self.moveAnnotations.append(annotation)
if isinstance(annotation, PureAnnotation):
elif isinstance(annotation, PureAnnotation):
self.pureAnnotations.append(annotation)
if isinstance(annotation, RemoveAnnotation):
elif isinstance(annotation, RemoveAnnotation):
self.removeAnnotations.append(annotation)
if isinstance(annotation, RenameAnnotation):
elif isinstance(annotation, RenameAnnotation):
self.renameAnnotations.append(annotation)
if isinstance(annotation, TodoAnnotation):
elif isinstance(annotation, TodoAnnotation):
self.todoAnnotations.append(annotation)
if isinstance(annotation, ValueAnnotation):
elif isinstance(annotation, ValueAnnotation):
self.valueAnnotations.append(annotation)

def to_json(self) -> dict:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
)
from package_parser.processing.api.model import Attribute, Result
from package_parser.processing.migration.annotations import (
migrate_boundary_annotation,
migrate_enum_annotation,
migrate_rename_annotation,
migrate_todo_annotation,
Expand All @@ -31,6 +32,12 @@ def migrate_annotations(
) -> AnnotationStore:
migrated_annotation_store = AnnotationStore()

for boundary_annotation in annotationsv1.boundaryAnnotations:
mapping = _get_mapping_from_annotation(boundary_annotation, mappings)
if mapping is not None:
for annotation in migrate_boundary_annotation(boundary_annotation, mapping):
migrated_annotation_store.add_annotation(annotation)

for enum_annotation in annotationsv1.enumAnnotations:
mapping = _get_mapping_from_annotation(enum_annotation, mappings)
if mapping is not None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ._constants import migration_author
from ._migrate_boundary_annotation import migrate_boundary_annotation
from ._migrate_enum_annotation import migrate_enum_annotation
from ._migrate_rename_annotation import migrate_rename_annotation
from ._migrate_todo_annotation import migrate_todo_annotation
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
from copy import deepcopy
from typing import Optional, Tuple

from package_parser.processing.annotations.model import (
AbstractAnnotation,
BoundaryAnnotation,
EnumReviewResult,
Interval,
TodoAnnotation,
)
from package_parser.processing.api.model import (
AbstractType,
Attribute,
NamedType,
Parameter,
Result,
UnionType,
)
from package_parser.processing.migration.model import (
ManyToManyMapping,
ManyToOneMapping,
Mapping,
OneToManyMapping,
OneToOneMapping,
)

from ._constants import migration_author


def migrate_interval_to_fit_parameter_type(
intervalv1: Interval, is_discrete: bool
) -> Interval:
intervalv2 = deepcopy(intervalv1)
if intervalv2.isDiscrete == is_discrete:
return intervalv2
if is_discrete:
intervalv2.isDiscrete = True
if intervalv1.upperLimitType in (0, 1):
intervalv2.upperIntervalLimit = int(intervalv1.upperIntervalLimit)
intervalv2.upperLimitType = 1
if intervalv2.upperIntervalLimit == intervalv1.upperIntervalLimit:
intervalv2.upperLimitType = intervalv1.upperLimitType
if intervalv1.lowerLimitType in (0, 1):
intervalv2.lowerIntervalLimit = int(intervalv1.lowerIntervalLimit)
intervalv2.lowerLimitType = 1
if intervalv2.lowerIntervalLimit == intervalv1.lowerIntervalLimit:
intervalv2.lowerLimitType = intervalv1.lowerLimitType
else:
intervalv2.lowerIntervalLimit += 1
else:
intervalv2.isDiscrete = False
if intervalv1.upperLimitType in (0, 1):
intervalv2.upperIntervalLimit = float(intervalv1.upperIntervalLimit)
if intervalv1.lowerLimitType in (0, 1):
intervalv2.lowerIntervalLimit = float(intervalv1.lowerIntervalLimit)
return intervalv2


def _contains_number_and_is_discrete(
type_: Optional[AbstractType],
) -> Tuple[bool, bool]:
if type_ is None:
return False, False
if isinstance(type_, NamedType):
return type_.name in ("int", "float"), type_.name == "int"
if isinstance(type_, UnionType):
for element in type_.types:
is_number, is_discrete = _contains_number_and_is_discrete(element)
if is_number:
return is_number, is_discrete
return False, False


# pylint: disable=duplicate-code
def migrate_boundary_annotation(
boundary_annotation: BoundaryAnnotation, mapping: Mapping
) -> list[AbstractAnnotation]:
boundary_annotation = deepcopy(boundary_annotation)
authors = boundary_annotation.authors
authors.append(migration_author)
boundary_annotation.authors = authors

migrate_text = (
"The @Boundary Annotation with the interval '"
+ str(boundary_annotation.interval.to_json())
+ "' from the previous version was at '"
+ boundary_annotation.target
+ "' and the possible alternatives in the new version of the api are: "
+ ", ".join(
map(lambda api_element: api_element.name, mapping.get_apiv2_elements())
)
)

if isinstance(mapping, (OneToOneMapping, ManyToOneMapping)):
parameter = mapping.get_apiv2_elements()[0]
if isinstance(parameter, (Attribute, Result)):
return []
if isinstance(parameter, Parameter):
boundary_annotation.target = parameter.id
(
parameter_expects_number,
parameter_type_is_discrete,
) = _contains_number_and_is_discrete(parameter.type)
if parameter.type is None:
boundary_annotation.reviewResult = EnumReviewResult.UNSURE
return [boundary_annotation]
if parameter_expects_number:
if (
parameter_type_is_discrete
is not boundary_annotation.interval.isDiscrete
):
boundary_annotation.reviewResult = EnumReviewResult.UNSURE
boundary_annotation.interval = (
migrate_interval_to_fit_parameter_type(
boundary_annotation.interval, parameter_type_is_discrete
)
)
return [boundary_annotation]
return [
TodoAnnotation(
parameter.id,
authors,
boundary_annotation.reviewers,
boundary_annotation.comment,
EnumReviewResult.NONE,
migrate_text,
)
]
migrated_annotations: list[AbstractAnnotation] = []
if isinstance(mapping, (OneToManyMapping, ManyToManyMapping)):
for parameter in mapping.get_apiv2_elements():
if isinstance(parameter, Parameter):
is_number, is_discrete = _contains_number_and_is_discrete(
parameter.type
)
if (
parameter.type is not None
and is_number
and is_discrete is boundary_annotation.interval.isDiscrete
):
migrated_annotations.append(
BoundaryAnnotation(
parameter.id,
authors,
boundary_annotation.reviewers,
boundary_annotation.comment,
EnumReviewResult.NONE,
boundary_annotation.interval,
)
)
elif parameter.type is not None and is_number:
migrated_annotations.append(
BoundaryAnnotation(
parameter.id,
authors,
boundary_annotation.reviewers,
boundary_annotation.comment,
EnumReviewResult.UNSURE,
migrate_interval_to_fit_parameter_type(
boundary_annotation.interval,
is_discrete,
),
)
)
elif parameter.type is None:
migrated_annotations.append(
BoundaryAnnotation(
parameter.id,
authors,
boundary_annotation.reviewers,
boundary_annotation.comment,
EnumReviewResult.UNSURE,
boundary_annotation.interval,
)
)
continue
if not isinstance(parameter, (Attribute, Result)):
migrated_annotations.append(
TodoAnnotation(
parameter.id,
authors,
boundary_annotation.reviewers,
boundary_annotation.comment,
EnumReviewResult.UNSURE,
migrate_text,
)
)
return migrated_annotations
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def _default_value_is_in_instance_values_or_is_empty(
)


# pylint: disable=duplicate-code
def migrate_enum_annotation(
enum_annotation: EnumAnnotation, mapping: Mapping
) -> list[AbstractAnnotation]:
Expand Down Expand Up @@ -92,6 +93,7 @@ def migrate_enum_annotation(
return []
else:
enum_annotation.reviewResult = EnumReviewResult.UNSURE
enum_annotation.target = parameter.id
return [enum_annotation]
return [
TodoAnnotation(
Expand Down
Loading