Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

feat: Automatisierte Herleitung von Optional Annotations #473

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e83df6e
Code should be working - Still need to write a test
Apr 22, 2022
a7373dc
feat: Added function for getting optional annotations and added tests
Masara Apr 29, 2022
00f31db
feat: Adjusted tests and function which calculates if a parameter is …
Masara Apr 29, 2022
784d447
Add tests and prepare for merge with issue #441
Apr 29, 2022
7c71888
Merge remote-tracking branch 'origin/main' into 442-automatisierte-he…
Apr 29, 2022
6e24140
Still testing
Apr 29, 2022
34ca1f2
get required test funktioniert jetzt
Apr 29, 2022
5cda2ea
Test done and working
Apr 29, 2022
0359e08
Satisfy Linter
Apr 29, 2022
08429d3
style: apply automatic fixes of linters
GideonKoenig Apr 29, 2022
935cb28
Merge branch 'main' into 442-automatisierte-herleitung-von-required-a…
GideonKoenig Apr 29, 2022
c2b734c
Merge branch '442-automatisierte-herleitung-von-required-annotations'…
Masara Apr 29, 2022
bf6c448
Merge branch 'main' into 442-automatisierte-herleitung-von-required-a…
GideonKoenig Apr 29, 2022
e84e932
Bug fix
Apr 29, 2022
3492d0a
Merge branch '442-automatisierte-herleitung-von-required-annotations'…
Apr 29, 2022
2fede53
Implemented the new AnnotationStore object
Apr 30, 2022
32eb1e4
Update package-parser.iml
GideonKoenig Apr 30, 2022
eccd7c3
Renamed function to better communicate it's function
Apr 30, 2022
744d46e
Delete package-parser.iml
GideonKoenig Apr 30, 2022
a102775
Merge branch 'main' into 442-automatisierte-herleitung-von-required-a…
GideonKoenig Apr 30, 2022
c0558d4
Satisfy Linter -> Fixed typing
Apr 30, 2022
d26a417
style: apply automatic fixes of linters
GideonKoenig Apr 30, 2022
df4a1c5
Remove unnecessary import
Apr 30, 2022
594068b
Merge branch '442-automatisierte-herleitung-von-required-annotations'…
Apr 30, 2022
9b67467
Further seperate the replaceable math part of determining if a parame…
Apr 30, 2022
af41958
Function namechange
Apr 30, 2022
4836672
Merge branch 'main' into 442-automatisierte-herleitung-von-required-a…
GideonKoenig May 6, 2022
7019220
feat: Adjusted tests and functions for optional annotation & removed …
Masara May 6, 2022
8fd691c
Merge branch '442-automatisierte-herleitung-von-required-annotations'…
Masara May 6, 2022
55c2b1a
feat: Fixed optional annotation function and adjusted them according …
Masara May 6, 2022
2f03076
style: apply automatic fixes of linters
Masara May 6, 2022
235f77f
Merge branch 'main' into 443-automatisierte-herleitung-von-optional-a…
Masara May 6, 2022
9d33c90
style: apply automatic fixes of linters
Masara May 6, 2022
11c2f3a
Merge branch 'main' into 443-automatisierte-herleitung-von-optional-a…
lars-reimann May 6, 2022
27dad6a
refactor: changed variable naming and comments
Masara May 6, 2022
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 @@ -8,6 +8,7 @@
from package_parser.models.annotation_models import (
AnnotationStore,
ConstantAnnotation,
OptionalAnnotation,
ParameterInfo,
ParameterType,
RequiredAnnotation,
Expand Down Expand Up @@ -42,6 +43,7 @@ def generate_annotations(
__get_unused_annotations,
__get_constant_annotations,
__get_required_annotations,
__get_optional_annotations,
]

__generate_annotation_dict(api, usages, annotations, annotation_functions)
Expand Down Expand Up @@ -273,9 +275,36 @@ def __add_implicit_usages_of_default_value(usages: UsageStore, api: API) -> None
usages.add_value_usage(parameter_qname, default_value, location)


def __get_optional_annotations(
usages: UsageStore, api: API, annotations: AnnotationStore
) -> None:
"""
Collects all parameters that are currently required but should be optional to be assign a value
:param usages: Usage store
:param api: Description of the API
:param annotations: AnnotationStore, that holds all annotations
"""
parameters = api.parameters()
all_parameter = [(it, parameters[it]) for it in parameters]

for qname, _ in all_parameter:
parameter_info = __get_parameter_info(qname, usages)

if parameter_info.type == ParameterType.Optional:
formatted_name = __qname_to_target_name(api, qname)
annotations.optionals.append(
OptionalAnnotation(
target=formatted_name,
defaultValue=parameter_info.value,
defaultType=parameter_info.value_type,
)
)


def __get_parameter_info(qname: str, usages: UsageStore) -> ParameterInfo:
"""
Returns a ParameterInfo object, that contains the type of the parameter, the value that is associated with it, and the values type
Returns a ParameterInfo object, that contains the type of the parameter, the value that is associated with it,
and the values type.
:param qname: name of the parameter
:param usages: UsageStore
:return ParameterInfo
Expand All @@ -298,6 +327,8 @@ def __get_parameter_info(qname: str, usages: UsageStore) -> ParameterInfo:
value = max(values, key=lambda item: item[1])[0]
if value[0] == "'":
value = value[1:-1]

# If its neither required nor constant, return optional
return ParameterInfo(
ParameterType.Optional, value, __get_default_type_from_value(value)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from package_parser.commands.find_usages import UsageStore
from package_parser.commands.generate_annotations.generate_annotations import (
__get_constant_annotations,
__get_optional_annotations,
__get_required_annotations,
__get_unused_annotations,
__qname_to_target_name,
Expand Down Expand Up @@ -65,7 +66,24 @@
},
}

OPTIONALS_EXPECTED: dict[str, dict[str, str]] = {}
OPTIONALS_EXPECTED: dict[str, dict[str, str]] = {
"test/test/commonly_used_global_required_and_optional_function/required_that_should_be_optional": {
"target": "test/test/commonly_used_global_required_and_optional_function/required_that_should_be_optional",
"defaultType": "string",
"defaultValue": "miau",
},
"test/test/commonly_used_global_required_and_optional_function/optional_that_should_be_optional": {
"target": "test/test/commonly_used_global_required_and_optional_function/optional_that_should_be_optional",
"defaultType": "string",
"defaultValue": "captain_morgan",
},
"test/test/commonly_used_global_required_and_optional_function/commonly_used_almost_required": {
"target": "test/test/commonly_used_global_required_and_optional_function/commonly_used_almost_required",
"defaultType": "string",
"defaultValue": "marvel",
},
}

BOUNDARIES_EXPECTED: dict[str, dict[str, str]] = {}
ENUMS_EXPECTED: dict[str, dict[str, str]] = {}

Expand Down Expand Up @@ -151,6 +169,16 @@ def test_get_required():
} == REQUIREDS_EXPECTED


def test_get_optional():
usages, api, usages_file, api_file, usages_json_path, api_json_path = setup()
annotations = AnnotationStore()
_preprocess_usages(usages, api)
__get_optional_annotations(usages, api, annotations)
assert {
annotation.target: annotation.to_json() for annotation in annotations.optionals
} == OPTIONALS_EXPECTED


def test_generate():
usages, api, usages_file, api_file, usages_json_path, api_json_path = setup()
out_file_path = os.path.join(
Expand Down