Skip to content

Conversation

konard
Copy link
Member

@konard konard commented Sep 14, 2025

Summary

Features Added

  • ConversionConflictResolutionStrategy enum with 8 different resolution strategies
  • ConfigurableConverter<TSource, TTarget> for runtime strategy configuration
  • IConfigurableConverter interface for type-safe converter creation
  • ConversionHelper internal class for numeric type clamping logic

Available Strategies

  • ThrowException: Throw overflow exceptions (default behavior)
  • ResetToDefault: Always return target type's default value
  • ClampToMax: Use target type's MaxValue (reproduces old To.cs behavior from issue)
  • ClampToMin: Use target type's MinValue
  • ClampToNearest: Clamp to nearest valid bound based on source value
  • AllowOverflow: Use unchecked arithmetic allowing wraparound
  • SystemConvert: Use System.Convert logic with IConvertible
  • UseIConvertible: Use explicit IConvertible interface implementations

Example Usage

// Create converter with clamping to max value (old To.cs behavior)
var clampConverter = ConfigurableConverter<ulong, int>.Create(ConversionConflictResolutionStrategy.ClampToMax);
var result = clampConverter.Convert((ulong)int.MaxValue + 1000); // Returns int.MaxValue

// Create converter that allows overflow
var overflowConverter = ConfigurableConverter<ulong, int>.Create(ConversionConflictResolutionStrategy.AllowOverflow);
var wrappedResult = overflowConverter.Convert((ulong)int.MaxValue + 1000); // Returns wrapped value

// Switch strategies at runtime
var throwConverter = ConfigurableConverter<ulong, int>.Default; // ThrowException by default
var newConverter = throwConverter.WithStrategy(ConversionConflictResolutionStrategy.ClampToMax);

Test Plan

  • All existing tests pass
  • Comprehensive unit tests for all 8 strategies
  • Performance benchmarks comparing with existing converters
  • Edge cases tested (same-type conversion, non-numeric types)
  • Version updated to 0.5.0 for release

🤖 Generated with Claude Code


Resolves #15

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #15
@konard konard self-assigned this Sep 14, 2025
This addresses issue #15 by providing a mechanism for different resolution
of conversion conflicts when source type values exceed target type limits.

Features added:
- ConversionConflictResolutionStrategy enum with 8 different strategies
- ConfigurableConverter<TSource, TTarget> with runtime strategy selection
- IConfigurableConverter interface for type-safe converter creation
- ConversionHelper for numeric type clamping logic

Available strategies:
- ThrowException: Throw overflow exceptions (default)
- ResetToDefault: Always return target type's default value
- ClampToMax: Use target type's MaxValue (reproduces old To.cs behavior)
- ClampToMin: Use target type's MinValue
- ClampToNearest: Clamp to nearest valid bound (min/max)
- AllowOverflow: Use unchecked arithmetic (allows wraparound)
- SystemConvert: Use System.Convert logic
- UseIConvertible: Use explicit IConvertible interface

Includes comprehensive unit tests and benchmarks for performance comparison.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Think of a mechanism for different resolution of conversion conflicts Implement configurable conversion conflict resolution strategies Sep 14, 2025
@konard konard marked this pull request as ready for review September 14, 2025 09:20
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Think of a mechanism for different resolution of conversion conflicts
1 participant