|
| 1 | +from copy import deepcopy |
| 2 | +from typing import Optional, Tuple |
| 3 | + |
| 4 | +from package_parser.processing.annotations.model import ( |
| 5 | + AbstractAnnotation, |
| 6 | + BoundaryAnnotation, |
| 7 | + EnumReviewResult, |
| 8 | + Interval, |
| 9 | + TodoAnnotation, |
| 10 | +) |
| 11 | +from package_parser.processing.api.model import ( |
| 12 | + AbstractType, |
| 13 | + Attribute, |
| 14 | + NamedType, |
| 15 | + Parameter, |
| 16 | + Result, |
| 17 | + UnionType, |
| 18 | +) |
| 19 | +from package_parser.processing.migration.model import ( |
| 20 | + ManyToManyMapping, |
| 21 | + ManyToOneMapping, |
| 22 | + Mapping, |
| 23 | + OneToManyMapping, |
| 24 | + OneToOneMapping, |
| 25 | +) |
| 26 | + |
| 27 | +from ._constants import migration_author |
| 28 | + |
| 29 | + |
| 30 | +def migrate_interval_to_fit_parameter_type( |
| 31 | + intervalv1: Interval, is_discrete: bool |
| 32 | +) -> Interval: |
| 33 | + intervalv2 = deepcopy(intervalv1) |
| 34 | + if intervalv2.isDiscrete == is_discrete: |
| 35 | + return intervalv2 |
| 36 | + if is_discrete: |
| 37 | + intervalv2.isDiscrete = True |
| 38 | + if intervalv1.upperLimitType in (0, 1): |
| 39 | + intervalv2.upperIntervalLimit = int(intervalv1.upperIntervalLimit) |
| 40 | + intervalv2.upperLimitType = 1 |
| 41 | + if intervalv2.upperIntervalLimit == intervalv1.upperIntervalLimit: |
| 42 | + intervalv2.upperLimitType = intervalv1.upperLimitType |
| 43 | + if intervalv1.lowerLimitType in (0, 1): |
| 44 | + intervalv2.lowerIntervalLimit = int(intervalv1.lowerIntervalLimit) |
| 45 | + intervalv2.lowerLimitType = 1 |
| 46 | + if intervalv2.lowerIntervalLimit == intervalv1.lowerIntervalLimit: |
| 47 | + intervalv2.lowerLimitType = intervalv1.lowerLimitType |
| 48 | + else: |
| 49 | + intervalv2.lowerIntervalLimit += 1 |
| 50 | + else: |
| 51 | + intervalv2.isDiscrete = False |
| 52 | + if intervalv1.upperLimitType in (0, 1): |
| 53 | + intervalv2.upperIntervalLimit = float(intervalv1.upperIntervalLimit) |
| 54 | + if intervalv1.lowerLimitType in (0, 1): |
| 55 | + intervalv2.lowerIntervalLimit = float(intervalv1.lowerIntervalLimit) |
| 56 | + return intervalv2 |
| 57 | + |
| 58 | + |
| 59 | +def _contains_number_and_is_discrete( |
| 60 | + type_: Optional[AbstractType], |
| 61 | +) -> Tuple[bool, bool]: |
| 62 | + if type_ is None: |
| 63 | + return False, False |
| 64 | + if isinstance(type_, NamedType): |
| 65 | + return type_.name in ("int", "float"), type_.name == "int" |
| 66 | + if isinstance(type_, UnionType): |
| 67 | + for element in type_.types: |
| 68 | + is_number, is_discrete = _contains_number_and_is_discrete(element) |
| 69 | + if is_number: |
| 70 | + return is_number, is_discrete |
| 71 | + return False, False |
| 72 | + |
| 73 | + |
| 74 | +# pylint: disable=duplicate-code |
| 75 | +def migrate_boundary_annotation( |
| 76 | + boundary_annotation: BoundaryAnnotation, mapping: Mapping |
| 77 | +) -> list[AbstractAnnotation]: |
| 78 | + boundary_annotation = deepcopy(boundary_annotation) |
| 79 | + authors = boundary_annotation.authors |
| 80 | + authors.append(migration_author) |
| 81 | + boundary_annotation.authors = authors |
| 82 | + |
| 83 | + migrate_text = ( |
| 84 | + "The @Boundary Annotation with the interval '" |
| 85 | + + str(boundary_annotation.interval.to_json()) |
| 86 | + + "' from the previous version was at '" |
| 87 | + + boundary_annotation.target |
| 88 | + + "' and the possible alternatives in the new version of the api are: " |
| 89 | + + ", ".join( |
| 90 | + map(lambda api_element: api_element.name, mapping.get_apiv2_elements()) |
| 91 | + ) |
| 92 | + ) |
| 93 | + |
| 94 | + if isinstance(mapping, (OneToOneMapping, ManyToOneMapping)): |
| 95 | + parameter = mapping.get_apiv2_elements()[0] |
| 96 | + if isinstance(parameter, (Attribute, Result)): |
| 97 | + return [] |
| 98 | + if isinstance(parameter, Parameter): |
| 99 | + boundary_annotation.target = parameter.id |
| 100 | + ( |
| 101 | + parameter_expects_number, |
| 102 | + parameter_type_is_discrete, |
| 103 | + ) = _contains_number_and_is_discrete(parameter.type) |
| 104 | + if parameter.type is None: |
| 105 | + boundary_annotation.reviewResult = EnumReviewResult.UNSURE |
| 106 | + return [boundary_annotation] |
| 107 | + if parameter_expects_number: |
| 108 | + if ( |
| 109 | + parameter_type_is_discrete |
| 110 | + is not boundary_annotation.interval.isDiscrete |
| 111 | + ): |
| 112 | + boundary_annotation.reviewResult = EnumReviewResult.UNSURE |
| 113 | + boundary_annotation.interval = ( |
| 114 | + migrate_interval_to_fit_parameter_type( |
| 115 | + boundary_annotation.interval, parameter_type_is_discrete |
| 116 | + ) |
| 117 | + ) |
| 118 | + return [boundary_annotation] |
| 119 | + return [ |
| 120 | + TodoAnnotation( |
| 121 | + parameter.id, |
| 122 | + authors, |
| 123 | + boundary_annotation.reviewers, |
| 124 | + boundary_annotation.comment, |
| 125 | + EnumReviewResult.NONE, |
| 126 | + migrate_text, |
| 127 | + ) |
| 128 | + ] |
| 129 | + migrated_annotations: list[AbstractAnnotation] = [] |
| 130 | + if isinstance(mapping, (OneToManyMapping, ManyToManyMapping)): |
| 131 | + for parameter in mapping.get_apiv2_elements(): |
| 132 | + if isinstance(parameter, Parameter): |
| 133 | + is_number, is_discrete = _contains_number_and_is_discrete( |
| 134 | + parameter.type |
| 135 | + ) |
| 136 | + if ( |
| 137 | + parameter.type is not None |
| 138 | + and is_number |
| 139 | + and is_discrete is boundary_annotation.interval.isDiscrete |
| 140 | + ): |
| 141 | + migrated_annotations.append( |
| 142 | + BoundaryAnnotation( |
| 143 | + parameter.id, |
| 144 | + authors, |
| 145 | + boundary_annotation.reviewers, |
| 146 | + boundary_annotation.comment, |
| 147 | + EnumReviewResult.NONE, |
| 148 | + boundary_annotation.interval, |
| 149 | + ) |
| 150 | + ) |
| 151 | + elif parameter.type is not None and is_number: |
| 152 | + migrated_annotations.append( |
| 153 | + BoundaryAnnotation( |
| 154 | + parameter.id, |
| 155 | + authors, |
| 156 | + boundary_annotation.reviewers, |
| 157 | + boundary_annotation.comment, |
| 158 | + EnumReviewResult.UNSURE, |
| 159 | + migrate_interval_to_fit_parameter_type( |
| 160 | + boundary_annotation.interval, |
| 161 | + is_discrete, |
| 162 | + ), |
| 163 | + ) |
| 164 | + ) |
| 165 | + elif parameter.type is None: |
| 166 | + migrated_annotations.append( |
| 167 | + BoundaryAnnotation( |
| 168 | + parameter.id, |
| 169 | + authors, |
| 170 | + boundary_annotation.reviewers, |
| 171 | + boundary_annotation.comment, |
| 172 | + EnumReviewResult.UNSURE, |
| 173 | + boundary_annotation.interval, |
| 174 | + ) |
| 175 | + ) |
| 176 | + continue |
| 177 | + if not isinstance(parameter, (Attribute, Result)): |
| 178 | + migrated_annotations.append( |
| 179 | + TodoAnnotation( |
| 180 | + parameter.id, |
| 181 | + authors, |
| 182 | + boundary_annotation.reviewers, |
| 183 | + boundary_annotation.comment, |
| 184 | + EnumReviewResult.UNSURE, |
| 185 | + migrate_text, |
| 186 | + ) |
| 187 | + ) |
| 188 | + return migrated_annotations |
0 commit comments