-
-
Notifications
You must be signed in to change notification settings - Fork 201
Add SnakeCase strategy to PropertyNameMappingStrategy #2011
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
base: main
Are you sure you want to change the base?
Conversation
- Added SnakeCase enum value to PropertyNameMappingStrategy with XML documentation - Implemented snake_case to PascalCase conversion for property matching - Updated MemberPathCandidateBuilder with new overload supporting naming strategies - Modified MembersMappingBuilderContext to use SnakeCase strategy when configured - Updated PublicAPI snapshot to reflect new enum value - Added test case for SnakeCase property mapping validation This allows developers to map properties between PascalCase source objects and snake_case target objects (e.g., FirstName -> first_name) by setting PropertyNameMappingStrategy = PropertyNameMappingStrategy.SnakeCase
latonz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this contribution. I added my feedback. IMO we should also support auto flattening (see docs) and UpperSnakeCase.
|
Please also update the documentation. |
|
Hi @latonz, |
…ing' into feature/snake-case-property-mapping
latonz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates!
| private static List<string> BuildPermutationList(string source, int[] indices, int indicesCount, int mask, bool skipDelimiter) | ||
| { | ||
| // Pre-calculate exact capacity to avoid internal List resizing | ||
| // 1 base segment + 1 for every split | ||
| var capacity = 1 + CountSetBits(mask); | ||
| var list = new List<string>(capacity); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you attempted to optimize for performance. Have you benchmarked this yet?
From my perspective the previous code was easier to follow, and I might be biased, so I would still like to double check whether the added complexity is justified.
There is probably still room for significant optimization by using (ReadOnly)Spans, stackalloc and/or array pools. I'm not sure whether we should move these performance optimizations into a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you attempted to optimize for performance
Yes and no. The main goal was to unify the BuildPermutationList method. I wanted it to work for any notation, so I didn't focus much on optimization, such as using (ReadOnly)Spans, stackalloc and/or array pools.
I was wondering, though, if there will be any other notations in the future. KebabCase is not possible as a notation in C#.
If you are confused by this code, I can revert it.
|
Hi @latonz, Thank you for your review. I fixed the issues based on your reviews. The main point that is still unresolved is the complex login in MemberPathCandidateBuilder.cs. |
Add SnakeCase Property Name Mapping Strategy
Description
This PR adds support for
SnakeCaseas a new property name mapping strategy in Mapperly, enabling automatic mapping between PascalCase properties in C# source objects and snake_case properties in target objects.Motivation
In modern C# development, it's common to work with external systems or code generators that produce DTOs with snake_case property names (e.g., from JSON APIs, database schemas, or code generation tools). Currently, mapping between C# PascalCase properties and snake_case properties requires manual
[MapProperty]attributes for each property, which is tedious and error-prone for large objects.This feature is particularly valuable when:
Changes Made
SnakeCaseenum value toPropertyNameMappingStrategywith XML documentationMemberPathCandidateBuilderMemberPathCandidateBuilderwith new overload supporting naming strategiesMembersMappingBuilderContextto use SnakeCase strategy when configuredUsage Example
This allows developers to map properties between PascalCase source objects and snake_case target objects (e.g.,
FirstName->first_name) by simply settingPropertyNameMappingStrategy = PropertyNameMappingStrategy.SnakeCase.Fixes # (issue - if applicable)
Checklist