| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Generators; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Declares that an application-defined attribute type is an alias for a constructor |
| | | 7 | | /// guard, so it can be applied to fields or participating record properties instead of |
| | | 8 | | /// <see cref="ConstructorGuardAttribute"/>. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// <para> |
| | | 12 | | /// Apply this meta-attribute to your own <see cref="Attribute"/>-derived field or |
| | | 13 | | /// property attribute type. When the generator finds your attribute applied to a |
| | | 14 | | /// supported member, it resolves the guard from this meta-attribute and treats the |
| | | 15 | | /// member exactly as if |
| | | 16 | | /// <see cref="ConstructorGuardAttribute"/> had been applied directly with the same |
| | | 17 | | /// guard type. A positive alias on an eligible field triggers field-based constructor |
| | | 18 | | /// generation; a property alias is emitted only when the property also carries |
| | | 19 | | /// <see cref="RecordConstructorOverloadParameterAttribute"/>. |
| | | 20 | | /// </para> |
| | | 21 | | /// <para> |
| | | 22 | | /// This works whether your alias attribute type is declared in the same project as |
| | | 23 | | /// the field that uses it, or in a referenced assembly. |
| | | 24 | | /// </para> |
| | | 25 | | /// </remarks> |
| | | 26 | | /// <example> |
| | | 27 | | /// <code> |
| | | 28 | | /// [ConstructorGuardDefinition(typeof(CollectionNotEmptyGuard))] |
| | | 29 | | /// [AttributeUsage(AttributeTargets.Field)] |
| | | 30 | | /// public sealed class CollectionNotEmptyAttribute : Attribute |
| | | 31 | | /// { |
| | | 32 | | /// } |
| | | 33 | | /// |
| | | 34 | | /// public partial class OrderService |
| | | 35 | | /// { |
| | | 36 | | /// [CollectionNotEmpty] |
| | | 37 | | /// private readonly IReadOnlyCollection<Order> _orders; |
| | | 38 | | /// } |
| | | 39 | | /// |
| | | 40 | | /// // Generated call: CollectionNotEmptyGuard.Validate(orders, nameof(orders)); |
| | | 41 | | /// </code> |
| | | 42 | | /// <code> |
| | | 43 | | /// [ConstructorGuardDefinition(typeof(ScopeGuard))] |
| | | 44 | | /// [AttributeUsage(AttributeTargets.Property)] |
| | | 45 | | /// public sealed class ValidScopeAttribute : Attribute |
| | | 46 | | /// { |
| | | 47 | | /// } |
| | | 48 | | /// |
| | | 49 | | /// public partial record PreparedRequest(string Query) |
| | | 50 | | /// { |
| | | 51 | | /// [RecordConstructorOverloadParameter] |
| | | 52 | | /// [ValidScope] |
| | | 53 | | /// public PreparedScope? Scope { get; init; } |
| | | 54 | | /// } |
| | | 55 | | /// </code> |
| | | 56 | | /// </example> |
| | | 57 | | [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] |
| | | 58 | | public sealed class ConstructorGuardDefinitionAttribute : Attribute |
| | | 59 | | { |
| | | 60 | | /// <summary> |
| | | 61 | | /// Initializes a new instance of the <see cref="ConstructorGuardDefinitionAttribute"/> |
| | | 62 | | /// class for a custom guard type using the conventional <c>Validate</c> method. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="guardType"> |
| | | 65 | | /// A type exposing an accessible <see langword="static"/> method named |
| | | 66 | | /// <c>Validate</c>, compatible with <c>void Validate(T value, string parameterName)</c>. |
| | | 67 | | /// </param> |
| | 820 | 68 | | public ConstructorGuardDefinitionAttribute(Type guardType) |
| | | 69 | | { |
| | 820 | 70 | | GuardType = guardType; |
| | 820 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Initializes a new instance of the <see cref="ConstructorGuardDefinitionAttribute"/> |
| | | 75 | | /// class for a custom guard type and an explicit validation method name. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="guardType"> |
| | | 78 | | /// A type exposing an accessible <see langword="static"/> method matching |
| | | 79 | | /// <paramref name="methodName"/>, compatible with |
| | | 80 | | /// <c>void Validate(T value, string parameterName)</c>. |
| | | 81 | | /// </param> |
| | | 82 | | /// <param name="methodName"> |
| | | 83 | | /// The name of the static validation method to call, typically supplied via |
| | | 84 | | /// <see langword="nameof"/>. |
| | | 85 | | /// </param> |
| | 0 | 86 | | public ConstructorGuardDefinitionAttribute(Type guardType, string methodName) |
| | | 87 | | { |
| | 0 | 88 | | GuardType = guardType; |
| | 0 | 89 | | MethodName = methodName; |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets the custom guard type associated with the decorated alias attribute. |
| | | 94 | | /// </summary> |
| | 0 | 95 | | public Type GuardType { get; } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Gets the explicit validation method name, or <see langword="null"/> when the |
| | | 99 | | /// conventional <c>Validate</c> method name applies. |
| | | 100 | | /// </summary> |
| | 0 | 101 | | public string? MethodName { get; } |
| | | 102 | | } |