Skip to content

Commit

Permalink
Drop behavior for behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel Nobel committed May 8, 2023
1 parent 8d3555b commit 69b094e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
16 changes: 15 additions & 1 deletion src/Qowaiv.Bson.MongoDB/QowaivBsonSerializationProvider.cs
Expand Up @@ -16,7 +16,7 @@ public sealed class QowaivBsonSerializationProvider : IBsonSerializationProvider
{
IBsonSerializer? serializer = null;

if (TypeHelper.GetCandidateType(type) is not { } tp
if (GetCandidateType(type) is not { } tp
|| NotSupported.Contains(tp)
|| Serializers.TryGetValue(tp, out serializer))
{
Expand Down Expand Up @@ -56,6 +56,20 @@ private static bool IsSupported(IBsonSerializer converter)
.GetProperty(nameof(QowaivBsonSerializer<object>.TypeIsSupported), NonPublicInstance)!
.GetValue(converter, Array.Empty<object>())!;

[Pure]
private static Type? GetCandidateType(Type? type)
{
return TypeHelper.NotNullable(type) is { } tp && IsSupported(tp)
? tp
: null;

static bool IsSupported(Type type)
=> !type.IsAbstract
&& !type.IsGenericTypeDefinition
&& !type.ContainsGenericParameters
&& !type.IsGenericTypeDefinition;
}

#pragma warning disable S3011

// Reflection should not be used to increase accessibility of classes, methods, or fields
Expand Down
Expand Up @@ -21,7 +21,7 @@ public void nullable_FromJson_string_only_SVO()
.Should().BeOfType<QowaivBsonSerializer<SvoWithFromJsonStringOnly>>();

[Test]
public void FromJson_string_only_SVO_class()
public void FromJson_SVO_class()
=> provider.GetSerializer(typeof(SvoWithFromJsonClass))
.Should().BeOfType<QowaivBsonSerializer<SvoWithFromJsonClass>>();

Expand All @@ -30,21 +30,10 @@ public void Qowaiv_ID()
=> provider.GetSerializer(typeof(Id<SomeIdBehavior>))
.Should().BeOfType<QowaivBsonSerializer<Id<SomeIdBehavior>>>();


[Test]
public void Qowaiv_ID_via_behavior()
=> provider.GetSerializer(typeof(SomeIdBehavior))
.Should().BeOfType<QowaivBsonSerializer<Id<SomeIdBehavior>>>();

[Test]
public void Qowaiv_generic_SVO()
=> provider.GetSerializer(typeof(Svo<SomeSvoBehavior>))
.Should().BeOfType<QowaivBsonSerializer<Svo<SomeSvoBehavior>>>();

[Test]
public void Qowaiv_generic_SVO_via_behavior()
=> provider.GetSerializer(typeof(SomeSvoBehavior))
.Should().BeOfType<QowaivBsonSerializer<Svo<SomeSvoBehavior>>>();
}

public class Gets_null
Expand All @@ -68,7 +57,7 @@ public void DTO()
=> provider.GetSerializer(typeof(DtoClass)).Should().BeNull();
}

class Cashes_for_performance_reasons
class Caches_for_performance_reasons
{
[Test]
public void serializers_already_requested()
Expand Down

0 comments on commit 69b094e

Please sign in to comment.