Skip to content

Commit 42098f9

Browse files
authored
2.0.1: Fixed a bug where arrays in (Wrapper)ValueObjects would trip the generator. (#19)
1 parent 2917733 commit 42098f9

File tree

6 files changed

+52
-10
lines changed

6 files changed

+52
-10
lines changed

DomainModeling.Generator/NamespaceSymbolExtensions.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.CodeAnalysis;
1+
using Microsoft.CodeAnalysis;
22

33
namespace Architect.DomainModeling.Generator;
44

@@ -12,25 +12,28 @@ internal static class NamespaceSymbolExtensions
1212
/// </summary>
1313
public static bool IsInSystemNamespace(this INamespaceSymbol namespaceSymbol)
1414
{
15-
while (namespaceSymbol.ContainingNamespace is not null)
15+
while (namespaceSymbol?.ContainingNamespace is not null)
1616
namespaceSymbol = namespaceSymbol.ContainingNamespace;
1717

18-
return namespaceSymbol.Name == "System";
18+
return namespaceSymbol?.Name == "System";
1919
}
2020

2121
/// <summary>
2222
/// Returns whether the given <see cref="INamedTypeSymbol"/> has the given <paramref name="fullName"/>.
2323
/// </summary>
24-
public static bool HasFullName(this INamespaceSymbol namespaceSymbol, string fullName)
24+
public static bool HasFullName(this INamespaceSymbol? namespaceSymbol, string fullName)
2525
{
2626
return namespaceSymbol.HasFullName(fullName.AsSpan());
2727
}
2828

2929
/// <summary>
3030
/// Returns whether the given <see cref="INamedTypeSymbol"/> has the given <paramref name="fullName"/>.
3131
/// </summary>
32-
public static bool HasFullName(this INamespaceSymbol namespaceSymbol, ReadOnlySpan<char> fullName)
32+
public static bool HasFullName(this INamespaceSymbol? namespaceSymbol, ReadOnlySpan<char> fullName)
3333
{
34+
if (namespaceSymbol is null)
35+
return false;
36+
3437
do
3538
{
3639
var length = namespaceSymbol.Name.Length;

DomainModeling.Generator/TypeSymbolExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static bool IsType(this ITypeSymbol typeSymbol, ITypeSymbol comparand)
4343

4444
chars = chars.Slice(0, chars.Length - freeBuffer.Length);
4545
if (containingNamespace?.IsGlobalNamespace != false)
46-
chars = typeSymbol.ContainingNamespace.ToString().AsSpan();
46+
chars = (typeSymbol.ContainingNamespace?.ToString() ?? "").AsSpan();
4747

4848
if (!typeSymbol.IsType(typeSymbol.Name.AsSpan(), chars))
4949
return false;
@@ -333,6 +333,11 @@ public static bool IsEnumerable(this ITypeSymbol typeSymbol, out INamedTypeSymbo
333333
if (!typeSymbol.IsOrImplementsInterface(type => type.IsType("IEnumerable", "System.Collections", generic: false), out var nonGenericEnumerableInterface))
334334
return false;
335335

336+
if (typeSymbol.Kind == SymbolKind.ArrayType)
337+
{
338+
elementType = ((IArrayTypeSymbol)typeSymbol).ElementType as INamedTypeSymbol;
339+
return true;
340+
}
336341
if (typeSymbol.IsOrImplementsInterface(type => type.IsType("IList", "System.Collections.Generic", generic: true), out var interf))
337342
{
338343
elementType = interf.TypeArguments[0] as INamedTypeSymbol;

DomainModeling.Generator/ValueObjectGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private static bool FilterSyntaxNode(SyntaxNode node, CancellationToken cancella
148148
{
149149
dataMemberHashCode = tuple.Member.Name.GetStableHashCode64(dataMemberHashCode);
150150
dataMemberHashCode = ":".GetStableHashCode64(dataMemberHashCode);
151-
dataMemberHashCode = tuple.Type.ContainingNamespace.ToString().GetStableHashCode64(dataMemberHashCode);
151+
dataMemberHashCode = (tuple.Type.ContainingNamespace?.ToString() ?? "").GetStableHashCode64(dataMemberHashCode); // Arrays have no namespace
152152
dataMemberHashCode = ".".GetStableHashCode64(dataMemberHashCode);
153153
dataMemberHashCode = tuple.Type.Name.GetStableHashCode64(dataMemberHashCode);
154154
dataMemberHashCode = "&".GetStableHashCode64(dataMemberHashCode);
@@ -288,7 +288,7 @@ public bool Equals({typeName}? other)
288288
{(existingComponents.HasFlags(ValueObjectTypeComponents.EqualsMethod) ? " */" : "")}
289289
290290
/// <summary>
291-
/// Provides type inference when comparing types that are completed source-generated. The current code's source generator does not know the appropriate namespace, because the type is being generated at the same time, thus necessitating type inference.
291+
/// Provides type inference when comparing types that are entirely source-generated. The current code's source generator does not know the appropriate namespace, because the type is being generated at the same time, thus necessitating type inference.
292292
/// </summary>
293293
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
294294
private static bool Equals<T>(T left, T right)
@@ -307,7 +307,7 @@ public int CompareTo({typeName}? other)
307307
}}
308308
309309
/// <summary>
310-
/// Provides type inference when comparing types that are completed source-generated. The current code's source generator does not know the appropriate namespace, because the type is being generated at the same time, thus necessitating type inference.
310+
/// Provides type inference when comparing types that are entirely source-generated. The current code's source generator does not know the appropriate namespace, because the type is being generated at the same time, thus necessitating type inference.
311311
/// </summary>
312312
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
313313
private static int Compare<T>(T left, T right)

DomainModeling.Tests/ValueObjectTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,25 @@ public ImmutableArrayValueObject(IEnumerable<string> values)
10861086
}
10871087
}
10881088
1089+
/// <summary>
1090+
/// Should merely compile.
1091+
/// </summary>
1092+
[Obsolete("Should merely compile.", error: true)]
1093+
[SourceGenerated]
1094+
public sealed partial class ArrayValueObject : ValueObject
1095+
{
1096+
protected override StringComparison StringComparison => StringComparison.OrdinalIgnoreCase;
1097+
1098+
public string?[]? StringValues { get; }
1099+
public int?[] IntValues { get; }
1100+
1101+
public ArrayValueObject(string?[]? stringValues, int?[] intValues)
1102+
{
1103+
this.StringValues = stringValues;
1104+
this.IntValues = intValues;
1105+
}
1106+
}
1107+
10891108
[SourceGenerated]
10901109
public sealed partial class CustomCollectionValueObject : ValueObject
10911110
{

DomainModeling.Tests/WrapperValueObjectTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,18 @@ public CustomCollection(string value)
508508
}
509509
}
510510
511+
[SourceGenerated]
512+
[Obsolete("Should merely compile.", error: true)]
513+
public sealed partial class StringArrayValue : WrapperValueObject<string?[]>
514+
{
515+
}
516+
517+
[SourceGenerated]
518+
[Obsolete("Should merely compile.", error: true)]
519+
public sealed partial class DecimalArrayValue : WrapperValueObject<decimal?[]>
520+
{
521+
}
522+
511523
/// <summary>
512524
/// Should merely compile.
513525
/// </summary>

DomainModeling/DomainModeling.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
</PropertyGroup>
1414

1515
<PropertyGroup>
16-
<VersionPrefix>2.0.0</VersionPrefix>
16+
<VersionPrefix>2.0.1</VersionPrefix>
1717
<Description>
1818
A complete Domain-Driven Design (DDD) toolset for implementing domain models, including base types and source generators.
1919

2020
https://github.com/TheArchitectDev/Architect.DomainModeling
2121

2222
Release notes:
2323

24+
2.0.1:
25+
- Fixed a bug where arrays in (Wrapper)ValueObjects would trip the generator.
26+
2427
2.0.0:
2528
- BREAKING: Generated DummyBuilders now use UTC datetimes for generated defaults and for interpreting datetime strings.
2629
- Semi-breaking: Generated types no longer add [Serializable] attribute, since there would be no way to remove it.

0 commit comments

Comments
 (0)