< Summary

Information
Class: NexusLabs.Needlr.Generators.ConstructorGuardDefinitionAttribute
Assembly: NexusLabs.Needlr.Generators.Attributes
File(s): /_work/nick-alienware-needlr-1-1784953859-3e8997/needlr/needlr/src/NexusLabs.Needlr.Generators.Attributes/ConstructorGuardDefinitionAttribute.cs
Line coverage
33%
Covered lines: 3
Uncovered lines: 6
Coverable lines: 9
Total lines: 102
Line coverage: 33.3%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%210%
get_GuardType()100%210%
get_MethodName()100%210%

File(s)

/_work/nick-alienware-needlr-1-1784953859-3e8997/needlr/needlr/src/NexusLabs.Needlr.Generators.Attributes/ConstructorGuardDefinitionAttribute.cs

#LineLine coverage
 1using System;
 2
 3namespace 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&lt;Order&gt; _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)]
 58public 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>
 82068    public ConstructorGuardDefinitionAttribute(Type guardType)
 69    {
 82070        GuardType = guardType;
 82071    }
 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>
 086    public ConstructorGuardDefinitionAttribute(Type guardType, string methodName)
 87    {
 088        GuardType = guardType;
 089        MethodName = methodName;
 090    }
 91
 92    /// <summary>
 93    /// Gets the custom guard type associated with the decorated alias attribute.
 94    /// </summary>
 095    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>
 0101    public string? MethodName { get; }
 102}