Skip to content

Commit

Permalink
Merge pull request #83 from AArnott/fix75
Browse files Browse the repository at this point in the history
Fix handling of array types in fields
  • Loading branch information
AArnott authored Jun 28, 2016
2 parents ab50ee2 + 1defef5 commit 23befe7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/ImmutableObjectGraph.Generation.Tests/CodeGenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public async Task NoFieldsAndOneScalarFieldDerived_HasCreateMethod()
Assert.Equal(1, result.DeclaredMethods.Count(m => m.ContainingType.Name == "NotSoEmptyDerived" && m.Name == "Create" && m.Parameters.Length == 1 && m.IsStatic));
}

[Fact]
public async Task ByteArray_CanBuild()
{
var result = await this.GenerateFromStreamAsync("ByteArray");
}

[Fact]
public async Task OneScalarFieldAndEmptyDerived_HasCreateMethod()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="TestSources\AbstractClassMidTypeHierarchyWithRequiredField.cs">
<Generator>MSBuild:GenerateCodeFromAttributes</Generator>
</Compile>
<EmbeddedResource Include="TestSources\ByteArray.cs" />
<Compile Include="TestSources\Generations.cs">
<Generator>MSBuild:GenerateCodeFromAttributes</Generator>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ImmutableObjectGraph.Generation.Tests.TestSources
{
[GenerateImmutable]
partial class ByteArray
{
readonly byte[] secret;
}
}
20 changes: 19 additions & 1 deletion src/ImmutableObjectGraph.Generation/CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,25 @@ private static bool HasAttribute<T>(INamedTypeSymbol type)

private static NameSyntax GetFullyQualifiedSymbolName(INamespaceOrTypeSymbol symbol)
{
if (symbol == null || string.IsNullOrEmpty(symbol.Name))
if (symbol == null)
{
return null;
}

if (symbol.Kind == SymbolKind.ArrayType)
{
var arraySymbol = (IArrayTypeSymbol)symbol;
var elementType = GetFullyQualifiedSymbolName(arraySymbol.ElementType);

// I don't know how to create a NameSyntax with an array inside it,
// so use ParseName as an escape hatch.
////return SyntaxFactory.ArrayType(elementType)
//// .AddRankSpecifiers(SyntaxFactory.ArrayRankSpecifier()
//// .AddSizes(SyntaxFactory.OmittedArraySizeExpression()));
return SyntaxFactory.ParseName(elementType.ToString() + "[]");
}

if (string.IsNullOrEmpty(symbol.Name))
{
return null;
}
Expand Down

0 comments on commit 23befe7

Please sign in to comment.