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
- Cleaner code: Each backend has dedicated factory function
- Extensibility: Add backends without modifying core
- Plugin system: External packages can register backends
- Type safety: Smartswitch validates argument types
- Performance: O(1) dispatch with pre-compiled rules
- Testability: Test each backend factory independently
Implementation Plan
- Add
smartswitch to dependencies in pyproject.toml
- Refactor
StorageManager._create_backend() to use Switcher
- Create decorators for backend registration
- Update tests (should mostly pass unchanged)
- Add examples to documentation
Acceptance Criteria
Priority
Medium - Improves code quality and extensibility
Dependencies
References
Problem
Current implementation uses long if/elif chains for dispatching operations:
This approach has several issues:
Proposed Solution
Integrate smartswitch (from Genro-Libs family) for elegant rule-based dispatch:
Current Pattern
With Smartswitch
Benefits
Implementation Plan
smartswitchto dependencies in pyproject.tomlStorageManager._create_backend()to use SwitcherAcceptance Criteria
Priority
Medium - Improves code quality and extensibility
Dependencies
smartswitch>=0.1.0(Genro-Libs family)References