Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSHARP-2096: Make EnumRepresentationConvention also affect collection… #1535

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

papafe
Copy link
Contributor

@papafe papafe commented Nov 5, 2024

…s of Enums

@papafe papafe requested a review from rstam November 5, 2024 13:32
@papafe
Copy link
Contributor Author

papafe commented Nov 5, 2024

This is still in draft because I need to check what happens with dictionaries

@papafe papafe marked this pull request as ready for review November 5, 2024 17:15
@papafe papafe requested a review from a team as a code owner November 5, 2024 17:15
}
}

// private methods
private bool IsNullableEnum(Type type)
private IBsonSerializer Reconfigure(IBsonSerializer serializer)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method here is recursively trying to fin an EnumSerializer. If it finds it, it changes its representation and sets it as the new child serializer.
If no EnumSerializer "leaf" is found, it will return null.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Searching for an EnumSerializer is a little stricter than what we used to do, which was search for a serializer for an enum value type that was representation configurable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, and this is also why I think it would make sense to do this for next major version, as this is effectively a behaviour breaking change. What do you think?


if (memberTypeInfo.IsEnum)
if (memberMap.MemberType.IsEnum && serializer is IRepresentationConfigurable representationConfigurableSerializer)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part here of the code could be removed, as it is a special case of Reconfigure. I decided to keep it as maybe it's slightly faster than Reconfigure and it's possibly a much more common case.

I've removed the "nullable enum" case though, but we can decide it needs to have the same treatment as this.

@@ -14,8 +14,7 @@
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is effectively a beahviour breaking change. Should we keep this for next major version?

}

var serializerType = serializer.GetType();
if (serializerType.IsGenericType && serializerType.GetGenericTypeDefinition() == typeof(EnumSerializer<>))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if there is an easier/more efficient way of doing this here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about?

if (serializer.ValueType.IsEnum && serializer is IRepresentationConfigurable representationConfigurable)
{                                                                                                       
    return representationConfigurable.WithRepresentation(_representation);                              
}                                                                                                       

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's perfect! I did not think about this 🤦

return;
}

if (IsNullableEnum(memberType))
var reconfiguredSerializer = Reconfigure(serializer);
if (reconfiguredSerializer is not null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you prefer is not null to != null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think sometimes it reads a little bit better, but in this case it probably does not make much of a difference. So I can definitely use != if we prefer it

}
}

// private methods
private bool IsNullableEnum(Type type)
private IBsonSerializer Reconfigure(IBsonSerializer serializer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Searching for an EnumSerializer is a little stricter than what we used to do, which was search for a serializer for an enum value type that was representation configurable.

}

var serializerType = serializer.GetType();
if (serializerType.IsGenericType && serializerType.GetGenericTypeDefinition() == typeof(EnumSerializer<>))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about?

if (serializer.ValueType.IsEnum && serializer is IRepresentationConfigurable representationConfigurable)
{                                                                                                       
    return representationConfigurable.WithRepresentation(_representation);                              
}                                                                                                       

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.

2 participants