RuntimeValue System Migration Plan
Overview
This document outlines the plan to modernize the RuntimeValue system by eliminating redundant features (MappedValue, CombinedValue) and leveraging the RuntimeValueOps mixin for cleaner, more Pythonic code.
Current State Analysis
RuntimeValue Implementations
- NativeRuntimeToken - Already uses RuntimeValueOps mixin ✓
- ResourceOutput - Has map() method, doesn't use RuntimeValueOps
- ResourceOutputWithContext - Same as ResourceOutput
- VariableReference - Has map() method, doesn't use RuntimeValueOps
- LocalReference - Has map() method, doesn't use RuntimeValueOps
- NetherframeFunction - Function calls, doesn't use RuntimeValueOps
- ConditionalExpression - Conditional logic, doesn't use RuntimeValueOps
- ConstantValue - Wrapper for constants, has map() method
Usage Patterns Found
.map()calls mainly in tests and for simple transformationscombine_deferred()used for combining multiple runtime values- Only NativeRuntimeToken currently uses RuntimeValueOps mixin
Migration Strategy
Phase 1: Add RuntimeValueOps to All RuntimeValue Implementations
-
Update base classes to inherit RuntimeValueOps:
class ResourceOutput(Generic[T], RuntimeValueOps): class VariableReference(Generic[T], RuntimeValueOps): class LocalReference(Generic[T], RuntimeValueOps): class ConstantValue(Generic[T], RuntimeValueOps): -
Add missing type detection methods for specialized types:
- NetherframeFunction: Add
_is_function_type() - ConditionalExpression: Add
_is_conditional_type()
Phase 2: Add Missing Operators to RuntimeValueOps
-
String conversion operators:
def __str__(self) -> str: """Convert to string representation for synthesis.""" # For ResourceOutput: "${resource.id.attribute}" # For VariableReference: "${var.name}" # For others: resolve and convert def __format__(self, format_spec: str) -> RuntimeValue[str]: """Format the runtime value.""" return native_runtime(lambda: format(self.resolve(None), format_spec)) -
Attribute access for dict-like values:
def __getattr__(self, name: str) -> RuntimeValue[Any]: """Access attributes on dict-like runtime values.""" if self._is_dict_type(): return self[name] return super().__getattribute__(name) -
Method calling support:
def call_method(self, method_name: str, *args, **kwargs) -> RuntimeValue[Any]: """Call a method on the runtime value.""" return native_runtime(lambda: getattr(self.resolve(None), method_name)(*args, **kwargs))
Phase 3: Migration Rules
For .map() calls:
-
Simple transformations:
# OLD: output.map(str.upper) # NEW: output.call_method('upper') # OLD: output.map(lambda x: x + 1) # NEW: output + 1 # OLD: output.map(lambda x: x['field']) # NEW: output['field'] -
Complex transformations - keep map() for now but deprecate:
# OLD: output.map(lambda x: complex_function(x, other_args)) # NEW: native_runtime(lambda: complex_function(output.resolve(None), other_args))
For combine_deferred() calls:
- Simple combinations:
# OLD: combine_deferred(lambda a, b: a + b, val1, val2) # NEW: val1 + val2 # OLD: combine_deferred(lambda a, b: f"{a}-{b}", val1, val2) # NEW: native_runtime(lambda: f"{val1.resolve(None)}-{val2.resolve(None)}")
Phase 4: Implementation Order
- Add RuntimeValueOps to all implementations (1 day)
- Update class definitions
- Ensure proper generic type handling
-
Add orig_class support where needed
-
Add missing operators (1 day)
- Implement str, format, call_method
-
Add tests for new operators
-
Migrate existing usage (2 days)
- Update test files first
- Update core library usage
-
Update examples
-
Deprecate old features (1 day)
- Add deprecation warnings to map() methods
- Add deprecation warnings to combine_deferred()
-
Update documentation
-
Remove deprecated code (after 1 release cycle)
- Remove MappedValue, CombinedValue classes
- Remove map() methods
- Remove combine_deferred()
Benefits
- Cleaner API: Natural Python operators instead of method calls
- Better IDE Support: Operators are recognized by IDEs
- Less Code: Remove ~150 lines of redundant implementation
- Type Safety: Operators only available for appropriate types
- Performance: Direct operator calls instead of wrapper objects
Backwards Compatibility
- Keep map() method with deprecation warning for 1 release cycle
- Keep combine_deferred() with deprecation warning
- Provide migration guide with examples
Testing Strategy
- Add operator tests before removing old functionality
- Ensure all existing tests pass with new implementation
- Add migration tests that compare old vs new approaches
- Performance benchmarks to ensure no regression
Success Criteria
- All RuntimeValue implementations use RuntimeValueOps
- No usage of MappedValue or CombinedValue in codebase
- All tests pass with new operators
- Documentation updated with new patterns
- Performance maintained or improved