| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 3 | | |
| | | 4 | | namespace NexusLabs.Needlr.Generators.Models; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A single field eligible to become a generated constructor parameter. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// Implements value equality (rather than the default struct member-by-member |
| | | 11 | | /// reflection-based comparison) — in particular <see cref="ExplicitGuards"/> is |
| | | 12 | | /// compared element-by-element rather than by array reference — so that |
| | | 13 | | /// <see cref="GeneratedConstructorModel"/> stays cacheable across incremental |
| | | 14 | | /// generator runs: two separately-built field lists with the same effective content |
| | | 15 | | /// must compare equal even though they are different array instances. |
| | | 16 | | /// </remarks> |
| | | 17 | | internal readonly struct EligibleConstructorField : IEquatable<EligibleConstructorField> |
| | | 18 | | { |
| | | 19 | | public EligibleConstructorField( |
| | | 20 | | string fieldName, |
| | | 21 | | string parameterName, |
| | | 22 | | string parameterTypeName, |
| | | 23 | | bool isNonNullableReferenceType, |
| | | 24 | | ConstructorGuardModel[] explicitGuards) |
| | | 25 | | { |
| | 146 | 26 | | FieldName = fieldName; |
| | 146 | 27 | | ParameterName = parameterName; |
| | 146 | 28 | | ParameterTypeName = parameterTypeName; |
| | 146 | 29 | | IsNonNullableReferenceType = isNonNullableReferenceType; |
| | 146 | 30 | | ExplicitGuards = explicitGuards; |
| | 146 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The original field name, e.g. <c>_repository</c>. |
| | | 35 | | /// </summary> |
| | 168 | 36 | | public string FieldName { get; } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// The normalized constructor parameter name, e.g. <c>repository</c>. |
| | | 40 | | /// </summary> |
| | 384 | 41 | | public string ParameterName { get; } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// The fully qualified parameter type name, preserving nullable-reference and |
| | | 45 | | /// generic type syntax. |
| | | 46 | | /// </summary> |
| | 133 | 47 | | public string ParameterTypeName { get; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// True when the field's type is a reference type with no nullable annotation, |
| | | 51 | | /// making it eligible for the class-level automatic null-guard default. |
| | | 52 | | /// </summary> |
| | 19 | 53 | | public bool IsNonNullableReferenceType { get; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Explicit guards requested for this field, in source declaration order. |
| | | 57 | | /// </summary> |
| | 87 | 58 | | public ConstructorGuardModel[] ExplicitGuards { get; } |
| | | 59 | | |
| | | 60 | | public bool Equals(EligibleConstructorField other) |
| | | 61 | | { |
| | 9 | 62 | | return string.Equals(FieldName, other.FieldName, StringComparison.Ordinal) && |
| | 9 | 63 | | string.Equals(ParameterName, other.ParameterName, StringComparison.Ordinal) && |
| | 9 | 64 | | string.Equals(ParameterTypeName, other.ParameterTypeName, StringComparison.Ordinal) && |
| | 9 | 65 | | IsNonNullableReferenceType == other.IsNonNullableReferenceType && |
| | 9 | 66 | | GuardsEqual(ExplicitGuards, other.ExplicitGuards); |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | public override bool Equals(object? obj) => obj is EligibleConstructorField other && Equals(other); |
| | | 70 | | |
| | | 71 | | public override int GetHashCode() |
| | | 72 | | { |
| | | 73 | | unchecked |
| | | 74 | | { |
| | 0 | 75 | | var hash = FieldName.GetHashCode(); |
| | 0 | 76 | | hash = (hash * 397) ^ ParameterName.GetHashCode(); |
| | 0 | 77 | | hash = (hash * 397) ^ ParameterTypeName.GetHashCode(); |
| | 0 | 78 | | hash = (hash * 397) ^ IsNonNullableReferenceType.GetHashCode(); |
| | | 79 | | |
| | 0 | 80 | | foreach (var guard in ExplicitGuards) |
| | | 81 | | { |
| | 0 | 82 | | hash = (hash * 397) ^ guard.GetHashCode(); |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | return hash; |
| | | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | public static bool operator ==(EligibleConstructorField left, EligibleConstructorField right) => left.Equals(right); |
| | | 90 | | |
| | 0 | 91 | | public static bool operator !=(EligibleConstructorField left, EligibleConstructorField right) => !left.Equals(right) |
| | | 92 | | |
| | | 93 | | private static bool GuardsEqual(ConstructorGuardModel[] left, ConstructorGuardModel[] right) |
| | | 94 | | { |
| | 6 | 95 | | if (ReferenceEquals(left, right)) |
| | 3 | 96 | | return true; |
| | | 97 | | |
| | 3 | 98 | | if (left.Length != right.Length) |
| | 0 | 99 | | return false; |
| | | 100 | | |
| | 3 | 101 | | return left.SequenceEqual(right); |
| | | 102 | | } |
| | | 103 | | } |