Advance Decorators

Conditional Rendering

@ConditionalRendering decorator is used when a Component has to be initialized with some condition. Just simple like an if-else condition if callback condition is satisfied else the component will not be loaded with Application Initializer.

Conditional Rendering Component is loaded only when callback condition returns true, or else the component will not load.

Example

Below snippet is an example of a Condition Rendered Component, TestConditionalAnnotation will only initialise if TEST key is available in environment variables (i.e. os.environ), Similarly TestNotConditionRendering will be loaded when TEST is not available in environment variables.

Code

@Component
@ConditionalRendering(callback=lambda x: "TEST" in os.environ)
class TestConditionalAnnotation:
    def __init__(self):
        # Do some stuff
        print("TestConditionalAnnotation Initialized")

    def postConstruct(self):
        # Do some stuff
        print("Called Post Construct")

@Component
@ConditionalRendering(callback=lambda x:  "TEST" not in os.environ)
class TestNotConditionRendering:
    def __init__(self):
        # Do some stuff
        print("TestNotConditionRendering Initialised")

Profiler Based Component

@Profile decorator expects a list of profile names as String object, if not passed profile will fallback to default profile, in other words Profile decorator is Conditional Rendering where it checks for specific Profile setup

Note

To use Profile decorator make sure RCN_ENVS_CONFIG.active.profile is set in environment variable before starting the script, otherwise a default profile will be used

Example

Below snippet is an example of how to use Profile decorator, LoadDefaultProfile Class is a default profile class which will be loaded when RCN_ENVS_CONFIG.active.profile is not set or value is default. Similarly, LoadDevProfile Class is has a profiler to initialize only when profile is set to dev.

Code

@Component
@Profile() # when RCN_ENVS_CONFIG.active.profile=default or not set
class LoadDefaultProfile:
    def __init__(self):
        # Do some stuff
        print("Default Profile Component Loaded")

@Component
@Profile(profiles=['dev']) # when RCN_ENVS_CONFIG.active.profile=dev
class LoadDevProfile:
    # Do some stuff
    def __init__(self):
        print("Dev Profile Component Loaded")