-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add [OptimizedEnumIndex] attribute for generated property lookups #5
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
Changes from all commits
416c8a3
cc2e6e6
c405c87
18712e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| namespace LayeredCraft.OptimizedEnums.Generator.Models; | ||
|
|
||
| /// <summary> | ||
| /// Represents a property decorated with <c>[OptimizedEnumIndex]</c> that the generator | ||
| /// should emit a pre-built dictionary lookup for on the concrete enum class. | ||
| /// </summary> | ||
| /// <param name="PropertyName">The property name as declared (e.g. "SlotValue").</param> | ||
| /// <param name="PropertyTypeFullyQualified">The fully-qualified type of the property.</param> | ||
| /// <param name="IsStringType">True when the property type is <see cref="string"/>.</param> | ||
| /// <param name="StringComparerExpression"> | ||
| /// The <c>StringComparer</c> expression to pass to the dictionary constructor | ||
| /// (e.g. "global::System.StringComparer.Ordinal"). Empty for non-string types. | ||
| /// </param> | ||
| internal sealed record IndexedPropertyInfo( | ||
| string PropertyName, | ||
| string PropertyTypeFullyQualified, | ||
| bool IsStringType, | ||
| string StringComparerExpression | ||
| ) : IEquatable<IndexedPropertyInfo>; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||||||||||||||||||||||||||||||
| #nullable enable | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| namespace LayeredCraft.OptimizedEnums; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||
| /// Marks a property on an intermediate OptimizedEnum base class as a lookup index. | ||||||||||||||||||||||||||||||||||
| /// The source generator will emit <c>From{PropertyName}</c> and <c>TryFrom{PropertyName}</c> | ||||||||||||||||||||||||||||||||||
| /// lookup methods backed by a pre-built dictionary on every concrete subclass. | ||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||
| /// <remarks> | ||||||||||||||||||||||||||||||||||
| /// The property type must implement <see cref="IEquatable{T}"/>; otherwise diagnostic OE0202 | ||||||||||||||||||||||||||||||||||
| /// is emitted and the index is skipped. For string properties, use | ||||||||||||||||||||||||||||||||||
| /// <see cref="StringComparison"/> to control key comparison. | ||||||||||||||||||||||||||||||||||
| /// </remarks> | ||||||||||||||||||||||||||||||||||
| [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] | ||||||||||||||||||||||||||||||||||
| public sealed class OptimizedEnumIndexAttribute : Attribute | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||
| /// For <see cref="string"/> properties, specifies the comparison used when building | ||||||||||||||||||||||||||||||||||
| /// the lookup dictionary. Defaults to <see cref="StringComparison.Ordinal"/>. | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+13
to
+20
|
||||||||||||||||||||||||||||||||||
| /// <see cref="StringComparison"/> to control key comparison. | |
| /// </remarks> | |
| [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] | |
| public sealed class OptimizedEnumIndexAttribute : Attribute | |
| { | |
| /// <summary> | |
| /// For <see cref="string"/> properties, specifies the comparison used when building | |
| /// the lookup dictionary. Defaults to <see cref="StringComparison.Ordinal"/>. | |
| /// <see cref="global::System.StringComparison"/> to control key comparison. | |
| /// </remarks> | |
| [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] | |
| public sealed class OptimizedEnumIndexAttribute : Attribute | |
| { | |
| /// <summary> | |
| /// For <see cref="string"/> properties, specifies the comparison used when building | |
| /// the lookup dictionary. Defaults to <see cref="global::System.StringComparison.Ordinal"/>. |
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.
OE0203 validation only checks for
privateaccessibility. If the indexed property isinternal,private protected(ProtectedAndInternal), or otherwise inaccessible from the concrete enum’s assembly/type (e.g., when the intermediate base class comes from a referenced assembly), the generator can still emits_by{PropertyName}initialization accessing that property, which will fail to compile. Consider validating accessibility using Roslyn’s accessibility APIs (e.g.,compilation.IsSymbolAccessibleWithin(...)for the property/getter, potentially withthroughType= concrete enum type) rather than only rejectingprivate.