Skip to content

Added enum to int conversion for unmapped side of binary expressions #163

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

Merged
merged 6 commits into from
Mar 13, 2023

Conversation

ErikGjers
Copy link
Contributor

Fixes #162

Was not able to do anything that would fix the issue at the ConstantExpression part of the visitor that was pointed out, but instead edited the BinaryExpression visiting part to have a conversion expression added.

Feedback appreciated.

@BlaiseD
Copy link
Member

BlaiseD commented Feb 13, 2023

Doing it in VisitConstant makes the change in the context to the TypeMappings dictionary. Something like the following:

            if (newType != node.Type)
            {
                if (node.Value == null)
                    return base.VisitConstant(Expression.Constant(null, newType));

                if (ConfigurationProvider.CanMapConstant(node.Type, newType))
                    return base.VisitConstant(Expression.Constant(Mapper.MapObject(node.Value, node.Type, newType), newType));
                else if (BothEnumOrLiteral())//this would be the new piece
                {
                    //Expression nodeValue = node.ConvertTypeIfNecessary(newType);
                    return node.ConvertTypeIfNecessary(newType);
                }

                bool BothEnumOrLiteral()
                    => (node.Type.IsLiteralType() || node.Type.IsEnumType()) && (newType.IsLiteralType() || newType.IsEnumType());
            }

The IsEnumType covers the null case.

        public static bool IsEnumType(this Type type)
        {
            if (type.IsNullableType())
                type = Nullable.GetUnderlyingType(type);

            return type.IsEnum();
        }

Your method works but to me the purpose is less clear. ConvertTypeIfNecessary removes existing conversions first.

@ErikGjers
Copy link
Contributor Author

The changes I made to the PR cover the requirements I believe. What you had written worked. Just dropping a message here in case the change went unnoticed. Feedback is always appreciated.

@BlaiseD
Copy link
Member

BlaiseD commented Feb 23, 2023

Not unnoticed - will post further feedback - or just add changes. Maybe the logic will have to be more targeted than BothEnumOrLiteral(). Enums do not always derive form Int32.

        public enum MyEnum : short
        {
            Value1 = 1,
            Value2 = 2,
            Value3 = 3
        }

Also the tests should include:

  • The reverse - i.e. enum to number AND number to enum.
  • Expressions where you expect only part of the expression should be converted x => x.IntToEnum == 1 && x.OtherInt == 2; - not sure if that's possible

Need to look closer before merging.

@ErikGjers
Copy link
Contributor Author

I'll add some tests and potential changes in the next couple of days.

@ErikGjers
Copy link
Contributor Author

I tried to include some string -> enum and enum -> string conversions, but these were failing. Since they weren't specifically part of this issue, though I figured it would be nice to have, I removed them for the time being.

Should I expand further on these tests, or do they cover the needs for the feature?

Sorry for the delay, travel and work got in the way.

@BlaiseD
Copy link
Member

BlaiseD commented Mar 3, 2023

No hurry - correct that string conversions don't apply here.

@BlaiseD BlaiseD merged commit 8266800 into AutoMapper:master Mar 13, 2023
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.

Consider conversions between Integers and Enums
3 participants