Skip to content

Commit 23befe7

Browse files
authored
Merge pull request #83 from AArnott/fix75
Fix handling of array types in fields
2 parents ab50ee2 + 1defef5 commit 23befe7

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/ImmutableObjectGraph.Generation.Tests/CodeGenTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public async Task NoFieldsAndOneScalarFieldDerived_HasCreateMethod()
6868
Assert.Equal(1, result.DeclaredMethods.Count(m => m.ContainingType.Name == "NotSoEmptyDerived" && m.Name == "Create" && m.Parameters.Length == 1 && m.IsStatic));
6969
}
7070

71+
[Fact]
72+
public async Task ByteArray_CanBuild()
73+
{
74+
var result = await this.GenerateFromStreamAsync("ByteArray");
75+
}
76+
7177
[Fact]
7278
public async Task OneScalarFieldAndEmptyDerived_HasCreateMethod()
7379
{

src/ImmutableObjectGraph.Generation.Tests/ImmutableObjectGraph.Generation.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<Compile Include="TestSources\AbstractClassMidTypeHierarchyWithRequiredField.cs">
5858
<Generator>MSBuild:GenerateCodeFromAttributes</Generator>
5959
</Compile>
60+
<EmbeddedResource Include="TestSources\ByteArray.cs" />
6061
<Compile Include="TestSources\Generations.cs">
6162
<Generator>MSBuild:GenerateCodeFromAttributes</Generator>
6263
</Compile>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ImmutableObjectGraph.Generation.Tests.TestSources
2+
{
3+
[GenerateImmutable]
4+
partial class ByteArray
5+
{
6+
readonly byte[] secret;
7+
}
8+
}

src/ImmutableObjectGraph.Generation/CodeGen.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,25 @@ private static bool HasAttribute<T>(INamedTypeSymbol type)
883883

884884
private static NameSyntax GetFullyQualifiedSymbolName(INamespaceOrTypeSymbol symbol)
885885
{
886-
if (symbol == null || string.IsNullOrEmpty(symbol.Name))
886+
if (symbol == null)
887+
{
888+
return null;
889+
}
890+
891+
if (symbol.Kind == SymbolKind.ArrayType)
892+
{
893+
var arraySymbol = (IArrayTypeSymbol)symbol;
894+
var elementType = GetFullyQualifiedSymbolName(arraySymbol.ElementType);
895+
896+
// I don't know how to create a NameSyntax with an array inside it,
897+
// so use ParseName as an escape hatch.
898+
////return SyntaxFactory.ArrayType(elementType)
899+
//// .AddRankSpecifiers(SyntaxFactory.ArrayRankSpecifier()
900+
//// .AddSizes(SyntaxFactory.OmittedArraySizeExpression()));
901+
return SyntaxFactory.ParseName(elementType.ToString() + "[]");
902+
}
903+
904+
if (string.IsNullOrEmpty(symbol.Name))
887905
{
888906
return null;
889907
}

0 commit comments

Comments
 (0)