Skip to content

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

  1. NativeRuntimeToken - Already uses RuntimeValueOps mixin ✓
  2. ResourceOutput - Has map() method, doesn't use RuntimeValueOps
  3. ResourceOutputWithContext - Same as ResourceOutput
  4. VariableReference - Has map() method, doesn't use RuntimeValueOps
  5. LocalReference - Has map() method, doesn't use RuntimeValueOps
  6. NetherframeFunction - Function calls, doesn't use RuntimeValueOps
  7. ConditionalExpression - Conditional logic, doesn't use RuntimeValueOps
  8. ConstantValue - Wrapper for constants, has map() method

Usage Patterns Found

  • .map() calls mainly in tests and for simple transformations
  • combine_deferred() used for combining multiple runtime values
  • Only NativeRuntimeToken currently uses RuntimeValueOps mixin

Migration Strategy

Phase 1: Add RuntimeValueOps to All RuntimeValue Implementations

  1. 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):
    

  2. Add missing type detection methods for specialized types:

  3. NetherframeFunction: Add _is_function_type()
  4. ConditionalExpression: Add _is_conditional_type()

Phase 2: Add Missing Operators to RuntimeValueOps

  1. 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))
    

  2. 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)
    

  3. 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:

  1. 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']
    

  2. 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:

  1. 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

  1. Add RuntimeValueOps to all implementations (1 day)
  2. Update class definitions
  3. Ensure proper generic type handling
  4. Add orig_class support where needed

  5. Add missing operators (1 day)

  6. Implement str, format, call_method
  7. Add tests for new operators

  8. Migrate existing usage (2 days)

  9. Update test files first
  10. Update core library usage
  11. Update examples

  12. Deprecate old features (1 day)

  13. Add deprecation warnings to map() methods
  14. Add deprecation warnings to combine_deferred()
  15. Update documentation

  16. Remove deprecated code (after 1 release cycle)

  17. Remove MappedValue, CombinedValue classes
  18. Remove map() methods
  19. Remove combine_deferred()

Benefits

  1. Cleaner API: Natural Python operators instead of method calls
  2. Better IDE Support: Operators are recognized by IDEs
  3. Less Code: Remove ~150 lines of redundant implementation
  4. Type Safety: Operators only available for appropriate types
  5. 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

  1. Add operator tests before removing old functionality
  2. Ensure all existing tests pass with new implementation
  3. Add migration tests that compare old vs new approaches
  4. 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