Skip to content

Commit

Permalink
Merge branch 'releases/7.x.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelGerr committed Oct 24, 2024
2 parents 3217eb2 + 3ad1376 commit e7947f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ private static Expression<Func<TKey, T>> GetConverterFromKey<T, TKey>(bool useCo
return (Expression<Func<TKey, T>>)(factoryMethod ?? throw new InvalidOperationException($"A value converter cannot be created for the type '{typeof(T).Name}' because it has no factory methods."));
}

private static T Convert<T>(object value)
{
return (T)System.Convert.ChangeType(value, typeof(T));
}

private sealed class ValueObjectValueConverter<T, TKey> : ValueConverter<T, TKey>
where T : IValueObjectFactory<TKey>, IValueObjectConvertable<TKey>
where TKey : notnull
Expand All @@ -110,7 +115,8 @@ public ValueObjectValueConverter(bool useConstructor)
{
null => null,
TKey key => key, // useful for comparisons of value objects with its key types
_ => ((T)o).ToValue()
T obj => obj.ToValue(),
_ => Convert<TKey>(o)
};
}
}
Expand All @@ -130,13 +136,15 @@ public ValidatableEnumValueConverter(bool validateOnWrite)
{
null => null,
TKey key => key, // useful for comparisons of value objects with its key types
_ => GetKeyIfValid((TEnum)o)
TEnum obj => GetKeyIfValid(obj),
_ => Convert<TKey>(o)
}
: o => o switch
{
null => null,
TKey key => key, // useful for comparisons of value objects with its key types
_ => ((TEnum)o).ToValue()
TEnum obj => obj.ToValue(),
_ => Convert<TKey>(o)
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,43 @@ UPDATE TestEntities_with_Enum_and_ValueObjects
[Fact]
public async Task Should_not_roundtrip_convert_underlying_type_to_value_object_and_back_to_underlying_type()
{
var item = TestSmartEnum_Struct_IntBased.Value1;
TestSmartEnum_Struct_IntBased? nullableItem = item;

var valueObj = IntBasedStructValueObject.Create(42);
IntBasedStructValueObject? nullableValueObj = valueObj;

long int64 = 42;
long? nullableInt64 = int64;
int int32 = 42;
int? nullableInt32 = int32;
short int16 = 42;
short? nullableInt16 = int16;
decimal deci = 42;
decimal? nullableDecimal = deci;

await _ctx.TestEntities_with_Enum_and_ValueObjects
.Where(e => e.IntBasedStructValueObject == 42
&& e.TestSmartEnum_Struct_IntBased == 42)
.Where(e => e.TestSmartEnum_Struct_IntBased == item
&& e.TestSmartEnum_Struct_IntBased == nullableItem
&& e.IntBasedStructValueObject == valueObj
&& e.IntBasedStructValueObject == nullableValueObj
&& e.IntBasedStructValueObject == int64
&& e.TestSmartEnum_Struct_IntBased == int64
&& e.IntBasedStructValueObject == nullableInt64
&& e.TestSmartEnum_Struct_IntBased == nullableInt64
&& e.IntBasedStructValueObject == int32
&& e.TestSmartEnum_Struct_IntBased == int32
&& e.IntBasedStructValueObject == nullableInt32
&& e.TestSmartEnum_Struct_IntBased == nullableInt32
&& e.IntBasedStructValueObject == int16
&& e.TestSmartEnum_Struct_IntBased == int16
&& e.IntBasedStructValueObject == nullableInt16
&& e.TestSmartEnum_Struct_IntBased == nullableInt16
&& e.IntBasedStructValueObject == deci
&& e.TestSmartEnum_Struct_IntBased == deci
&& e.IntBasedStructValueObject == nullableDecimal
&& e.TestSmartEnum_Struct_IntBased == nullableDecimal
)
.ToListAsync();
}

Expand Down

0 comments on commit e7947f5

Please sign in to comment.