Skip to content

Integration: Add smartswitch for rule-based dispatch #53

@genro

Description

@genro

Problem

Current implementation uses long if/elif chains for dispatching operations:

if backend_type == 's3':
    return S3Backend(...)
elif backend_type == 'local':
    return LocalBackend(...)
elif backend_type == 'memory':
    return MemoryBackend(...)
elif backend_type == 'http':
    return HTTPBackend(...)
# ... many more elif branches

This approach has several issues:

  • Verbosity: Long chains hard to read and maintain
  • Performance: Linear search through conditions
  • Extensibility: Adding backends requires modifying core code
  • Type safety: No compile-time validation

Proposed Solution

Integrate smartswitch (from Genro-Libs family) for elegant rule-based dispatch:

Current Pattern

def _create_backend(self, backend_type, config):
    if backend_type == 's3':
        return S3Backend(config)
    elif backend_type == 'local':
        return LocalBackend(config)
    elif backend_type == 'memory':
        return MemoryBackend(config)
    # ... 10+ more elif branches

With Smartswitch

from smartswitch import Switcher

backend_factory = Switcher()

@backend_factory.typerule(backend_type=str)
def create_backend(backend_type: str, config: dict):
    """Default fallback - use fsspec."""
    return FsspecBackend(config)

@backend_factory.valrule(lambda backend_type, config: backend_type == 's3')
def create_s3_backend(backend_type: str, config: dict):
    return S3Backend(config)

@backend_factory.valrule(lambda backend_type, config: backend_type == 'local')
def create_local_backend(backend_type: str, config: dict):
    return LocalBackend(config)

Benefits

  1. Cleaner code: Each backend has dedicated factory function
  2. Extensibility: Add backends without modifying core
  3. Plugin system: External packages can register backends
  4. Type safety: Smartswitch validates argument types
  5. Performance: O(1) dispatch with pre-compiled rules
  6. Testability: Test each backend factory independently

Implementation Plan

  1. Add smartswitch to dependencies in pyproject.toml
  2. Refactor StorageManager._create_backend() to use Switcher
  3. Create decorators for backend registration
  4. Update tests (should mostly pass unchanged)
  5. Add examples to documentation

Acceptance Criteria

  • Remove all if/elif chains for backend selection
  • All existing tests pass
  • Performance maintained or improved
  • Plugin system documented with examples
  • Integration tested with custom backends

Priority

Medium - Improves code quality and extensibility

Dependencies

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions