Repository navigation
Feature/generator gaps - #39
Merged
Merged
Conversation
The run-time engine matches enum members by name and only falls back to the number, which is what ByNameThenValue means and what it does unless told otherwise. The generator cast the number, so the two engines returned different members the first time anybody reordered an enum — quietly, with no error anywhere. A test that runs the same pair through both now covers it. Both enums are known while the project compiles, so the correspondence is settled there and what is emitted is a switch over constants, with the cast underneath for the values that have no name in common. Text into an enum is read the same way Enum.TryParse reads it, by name ignoring case and then as a number, with empty text giving the default. Conversions are lifted over nullables in the place the run-time engine lifts them, before anything else is asked about the pair, so an absent value gives the destination's default instead of reaching a cast that throws on it. Enum conversions are now looked at before the declared maps, which is the order the run-time engine uses. That is not a preference: with the maps first, a method declared for a pair of enums was generated as a call to itself. Fixes a run-time bug the comparison turned up: CreateMap<Grade, GradeDto>() on its own returned the destination's zero value whatever it was handed. An enum has nothing to assign into, so the member-by-member path created it and stopped, while the same pair met as a member of something else went through the conversion builder and came out right. The engine disagreed with itself depending on where the pair was met. A map that configures anything of its own is untouched, so a converter declared on the pair still wins.
Flattening is the convention AutoMapper is known for, and the run-time engine does it: a destination called CustomerAddressCity finds Customer.Address.City without being told. The generator only ever looked for a member of that name, so it reported as unmapped what the other engine mapped on its own, and one configuration meant two things depending on which engine read it. Three members deep, the same ceiling the run-time engine uses, and every step that can be null is guarded so a chain that stops short gives the default rather than throwing — which is what the other engine does with it. What is not read is the affixes and the naming conventions. Those are set on a MapperConfiguration, and there is no such thing while the project compiles, so what is implemented here is the default spelling on both sides. A source that writes its members some other way still needs [MapProperty], and the article says so.
The run-time engine copies a dictionary into a dictionary, converting keys and values on the way. The generator had nothing for it, so a member the other engine handled was reported as having no conversion available. Written out as a loop rather than as ToDictionary, because the two disagree about a repeated key: the engine's last entry wins and ToDictionary throws. The helper is an instance method, since the value conversion is often a call to one of the mapping methods beside it. Two more places the engines parted company, both in the same family: A collection the source does not have gave an ArgumentNullException here and an empty collection there. AllowNullCollections is a run-time setting and cannot be seen while compiling, so the generator follows the default, which is the one almost everybody keeps. A collection of the same type on both sides was handed over rather than copied, so the source and the destination shared one list and a change to either was a change to both. Copying now happens before the two types are compared, which is the order the run-time engine uses.
Guid, DateOnly and TimeOnly out of text arrived in the run-time engine in 0.11.0 and never arrived here, so an identifier or a date coming out of a column somebody typed as varchar mapped on one engine and stopped the build on the other. Empty text gives the default and the invariant culture is used, both to match the engine. Two types that both say they can convert themselves now go through Convert.ChangeType with the invariant culture, which is the last thing the run-time engine tries and covers text into a number, a date or a bool. The two still raise different types when the text is rubbish: the run-time engine wraps everything in MappingException and generated code has no such thing, because it emits no handler at all. Both refuse it, which is what the test checks.
Mapping through a base reference where the instance is of a derived type built the base destination and dropped everything the derived type added. The run-time engine hands the work to the derived map, so the same object came out with fewer fields on one engine than the other. [MapperInclude(typeof(CardPayment), typeof(CardPaymentDto))] on the base method is Include, said where the generator can read it. Declared rather than inferred, for the reason it is declared there too: a class that happens to derive from another is not a statement that mapping one should produce the other. Most derived first, so a hierarchy several levels deep picks the closest match. MPR0007 when the included pair has no method on the class, MPR0008 when the pair named does not derive from the one being mapped — both errors, because neither can be what anybody meant.
A value resolver is an object the container hands over, and there is no container while the project compiles. What there is instead is a method beside the mapping ones, named by [MapperResolve(nameof(Total), "Total")] and handed the whole source. Same job, no interface to implement and nothing to register. MPR0009 when no method of that name takes the source type, since a name that resolves to nothing would otherwise fall back to the conventions and fill the member with something else.
The ahead-of-time article listed two attributes and said the generator does not do value resolvers or polymorphic dispatch, which stopped being true. It now lists the four attributes in a table, says members are matched by spelling out a path, and is straight about the two things it still does not read: the affixes and the naming conventions, which live on a MapperConfiguration and so do not exist while the project compiles.
CI reported 6.07x against a baseline of 4.56x on a change that cannot reach that scenario: the only run-time edit in this branch is a branch taken while a plan is compiled, and only where the destination is an enum, which TicketDto is not. Measured rather than assumed. Six short runs: 4.14x, 4.44x and 3.45x here, 3.65x, 4.09x and 3.70x on main. The two sets overlap and each spans about a whole point. The hand-written floor is five switch statements, and it moved seventeen per cent between consecutive runs over code that had not changed. Third scenario to need this, after enums by value and the record through its constructor, and all three for the same reason: a ratio over a floor of a few nanoseconds is mostly a measurement of the floor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.