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

Q: Nested property expressions support #312

Open
vladimir-ilnytskyi opened this issue Oct 16, 2024 · 1 comment
Open

Q: Nested property expressions support #312

vladimir-ilnytskyi opened this issue Oct 16, 2024 · 1 comment

Comments

@vladimir-ilnytskyi
Copy link

vladimir-ilnytskyi commented Oct 16, 2024

Hi!

I'm facing an issue with nested object mapping from excel:

class Address { 
public string City {get ;set;}
 ...
 }

class Product {
public Address ShippingAddress { get; set; }
public Address DeliveryAddress {get; set; }
}

It would be nice to be able to use mappings for shipping address city and Delivery address city like this:

mapper.AddMapping("shipping address city", x => x.ShippingAddress.City); // handle expression with nested property accessors. For sure it is possible to use refrection on nested objects
So now i get "something like cannot convert cell string to type string".

Something like this would help

public static (PropertyInfo propertyInfo, string fullPath) GetPropertyInfo<T>(Expression<Func<T, object>> propertySelector)
    {
        if (propertySelector is not LambdaExpression lambdaExpression)
        {
            throw new ArgumentException($"Unsupported property selector: {propertySelector}", nameof(propertySelector));
        }

        var pathBuilder = new StringBuilder();
        var body = lambdaExpression.Body;

        while (body is MemberExpression or UnaryExpression{ Operand: MemberExpression })
        {
            var memberAccess = body as MemberExpression ?? (MemberExpression)((UnaryExpression)body).Operand;

            if (pathBuilder.Length > 0)
            {
                pathBuilder.Insert(0, ".");
            }

            pathBuilder.Insert(0, memberAccess.Member.Name);
            body = memberAccess.Expression;
        }

        if (pathBuilder.Length > 0)
        {
            pathBuilder.Insert(0, typeof(T).Name + ".");
        }

        var lambdaBody = lambdaExpression.Body.NodeType == ExpressionType.MemberAccess ?
            (MemberExpression)lambdaExpression.Body :
            (MemberExpression)((UnaryExpression)lambdaExpression.Body).Operand; // for nullable value such as int?

        // body.Member will return the MemberInfo of the base class, so we have to get it from T...
        //return (PropertyInfo)body.Member;

        var propertyInfo = lambdaBody.Expression.Type.GetProperty(lambdaBody.Member.Name);
        return (propertyInfo, pathBuilder.ToString());
    }

Having separate mapping for type is making impossible to map different properties of same type to excel, maybe there is a way but i seems like i have stuck with using custom mapping funcs.

Please advise,

Best regards

@mganss
Copy link
Owner

mganss commented Oct 28, 2024

Thanks a lot for mapping out a solution already! Could you make a PR?

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

No branches or pull requests

3 participants
@mganss @vladimir-ilnytskyi and others