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

Improvements to Ignore() processing and Fix MapToTarget behavior for RecordTypes #769

Open
wants to merge 12 commits into
base: development
Choose a base branch
from

Conversation

DocSvartz
Copy link

Fix Issue #456 #707 #723

#456 When explicitly set to ignore():

Instance of a class can be created with default values ​​for parameters.
Explicitly set to ignore() not mark param as not matched when config RequireDestinationMemberSource = true,

#707 #723 RecordTypes and Generated Type to Interface received support Ignore() from Ctor parameters

Fix MapToTarget behavior for RecordTypes (Include using Ignore() )

before from

record TestRecord()
{
    public int X { set; get; }
}
record TestRecordY() 
{
    public int X { set; get; }
    public int Y { set; get; }
}

var source = new TestRecord() { X= 200};
var dest = new TestRecordY() {X = 100, Y= 500}

var adapt = source.Adapt(dest)  // always adapt == { X=200, Y=0}

after

var adapt = source.Adapt(dest)  // adapt == { X=200, Y=500}  Correct MapTotarget

equal behavior when modified using with

var adapt = dest with {X=source.X};

@DocSvartz DocSvartz marked this pull request as ready for review January 25, 2025 11:43
@DocSvartz DocSvartz requested a review from andrerav January 25, 2025 11:43
{
var find = arg.DestinationType.GetFieldsAndProperties(arg.Settings.EnableNonPublicMembers.GetValueOrDefault()).ToArray()
.Where(x => x.Name == destinationMember.Name).FirstOrDefault();

Copy link
Author

@DocSvartz DocSvartz Jan 27, 2025

Choose a reason for hiding this comment

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

This can be simplified on refactoring. Field information is already in the processing pipeline

var find = arg.DestinationType.GetFieldsAndProperties(arg.Settings.EnableNonPublicMembers.GetValueOrDefault()).ToArray()
.Where(x => x.Name == member.DestinationMember.Name).FirstOrDefault();

if (find != null && destination != null)
Copy link
Author

Choose a reason for hiding this comment

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

Here it is also possible, but you need to get a list of Destination fields. Not constructor parameters.


if (arg.MapType == MapType.MapToTarget && arg.DestinationType.IsRecordType())
{
var find = arg.DestinationType.GetFieldsAndProperties(arg.Settings.EnableNonPublicMembers.GetValueOrDefault()).ToArray()
Copy link
Author

Choose a reason for hiding this comment

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

Here it is also possible, but you need to get a list of Destination fields. Not constructor parameters.

var contructorMembers = newInstance.Arguments.OfType<MemberExpression>().Select(me => me.Member).ToArray();
var contructorMembers = arg.DestinationType.GetConstructors()
.OrderByDescending(it => it.GetParameters().Length).FirstOrDefault()
.GetParameters().ToList();
Copy link
Author

Choose a reason for hiding this comment

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

This can be simplified. information is already transferred to installExpr

private List<MemberBinding> RecordIngnoredWithoutConditonRestore(Expression? destination, CompileArgument arg, List<ParameterInfo> contructorMembers)
{
var members = arg.DestinationType
.GetFieldsAndProperties(arg.Settings.EnableNonPublicMembers.GetValueOrDefault())
Copy link
Author

Choose a reason for hiding this comment

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

It is not possible to obtain such a data configuration in the current processing pipeline.
But it is possible to simplify it by obtaining it from a modified version of the install expression in the current solution.

@DocSvartz
Copy link
Author

@andrerav I left the comments for myself. So I wouldn't forget )
Most likely, we also need to add a Null check for the entire Destination object.
But I don't understand where yet ))

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.

1 participant