Skip to content
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

[DEV] Annotation Registry #16

Merged
merged 3 commits into from
Nov 23, 2024
Merged

[DEV] Annotation Registry #16

merged 3 commits into from
Nov 23, 2024

Conversation

MatrixEditor
Copy link
Owner

Version 2.2.2 adds a new feature called "Annotation Registry", which will be responsible for storing converters for annotation values.

Example: implementation of automatic struct conversion

from caterpillar import registry

class StructTypeConverter(registry.TypeConverter): # new class to implement custom matching algorithm
    def matches(self, annotation: Any) -> bool:
        # the annotation must be a type and the __struct__ attribute must be present
        return isinstance(annotation, type) and getstruct(annotation) is not None

    def convert(self, annotation: Any, kwargs: dict) -> _StructLike:
        return getstruct(annotation) # won't return None, because of previous condition

# finally, add the new converter to existing ones
registry.annotation_registry.append(StructTypeConverter())

Fixes #15

@MatrixEditor MatrixEditor self-assigned this Nov 23, 2024
@MatrixEditor MatrixEditor merged commit 1908fc0 into master Nov 23, 2024
6 checks passed
@MatrixEditor MatrixEditor deleted the dev/annotation-registry branch November 23, 2024 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Struct as Conditional Field -- caterpillar.exception.StructException
1 participant