Skip to content

Commit

Permalink
Fixed some more bugs, around generics/tuples.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Apr 17, 2024
1 parent 04481aa commit 02b624c
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 51 deletions.
106 changes: 105 additions & 1 deletion Schema Tests/readOnly/MemberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface IReadOnlyWrapper {
}

[Test]
public void TestIEnumerable() {
public void TestGeneric() {
ReadOnlyGeneratorTestUtil.AssertGenerated(
"""
using schema.readOnly;
Expand All @@ -66,5 +66,109 @@ public interface IReadOnlyWrapper {

""");
}

[Test]
public void TestNestedGeneric() {
ReadOnlyGeneratorTestUtil.AssertGenerated(
"""
using schema.readOnly;
using System.Collections.Generic;

namespace foo.bar {
[GenerateReadOnly]
public partial interface IWrapper {
public IEnumerable<IEnumerable<bool>> Value { get; set; }
}
}
""",
"""
namespace foo.bar {
public partial interface IWrapper : IReadOnlyWrapper;

public interface IReadOnlyWrapper {
public System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<bool>> Value { get; }
}
}

""");
}

[Test]
public void TestIndexer() {
ReadOnlyGeneratorTestUtil.AssertGenerated(
"""
using schema.readOnly;
using System.Collections.Generic;

namespace foo.bar {
[GenerateReadOnly]
public partial interface IWrapper {
public bool this[int index] { get; set; }
}
}
""",
"""
namespace foo.bar {
public partial interface IWrapper : IReadOnlyWrapper;

public interface IReadOnlyWrapper {
public bool this[int index] { get; }
}
}

""");
}

[Test]
public void TestNamelessTuple() {
ReadOnlyGeneratorTestUtil.AssertGenerated(
"""
using schema.readOnly;
using System.Collections.Generic;

namespace foo.bar {
[GenerateReadOnly]
public partial interface IWrapper {
public (bool, int) Tuple { get; set; }
}
}
""",
"""
namespace foo.bar {
public partial interface IWrapper : IReadOnlyWrapper;

public interface IReadOnlyWrapper {
public (bool, int) Tuple { get; }
}
}

""");
}

[Test]
public void TestNamedTuple() {
ReadOnlyGeneratorTestUtil.AssertGenerated(
"""
using schema.readOnly;
using System.Collections.Generic;

namespace foo.bar {
[GenerateReadOnly]
public partial interface IWrapper {
public (bool a, int b) Tuple { get; set; }
}
}
""",
"""
namespace foo.bar {
public partial interface IWrapper : IReadOnlyWrapper;

public interface IReadOnlyWrapper {
public (bool a, int b) Tuple { get; }
}
}

""");
}
}
}
2 changes: 1 addition & 1 deletion Schema/Schema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Description>Library for converting classes to and from binary. Provides a C# Roslyn generator that automatically implements conversion logic for simple classes.</Description>
<PackageId>schema</PackageId>
<Title>schema</Title>
<Version>0.4.2</Version>
<Version>0.4.3</Version>
<Authors>MeltyPlayer</Authors>
</PropertyGroup>

Expand Down
71 changes: 45 additions & 26 deletions Schema/src/readOnly/ReadOnlyTypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;

using schema.binary.parser;
using schema.util.asserts;
using schema.util.generators;
using schema.util.symbols;
using schema.util.text;
Expand Down Expand Up @@ -119,13 +120,12 @@ var constMembers
return false;
}

if (memberSymbol is IPropertySymbol {
IsWriteOnly: true
}) {
if (memberSymbol is IPropertySymbol) {
return false;
}

if (memberSymbol is IMethodSymbol &&
!memberSymbol.Name.StartsWith("get_") &&
!memberSymbol.HasAttribute<ConstAttribute>()) {
return false;
}
Expand All @@ -141,13 +141,10 @@ var constMembers
cbsb.EnterBlock(blockPrefix);

foreach (var parsedMember in constMembers) {
var (_, memberSymbol, memberTypeSymbol, _) = parsedMember;
var (_, memberSymbol, _, _) = parsedMember;

{
if (memberSymbol is IMethodSymbol methodSymbol) {
memberTypeSymbol = methodSymbol.ReturnType;
}
}
var methodSymbol = Asserts.AsA<IMethodSymbol>(memberSymbol);
var memberTypeSymbol = methodSymbol.ReturnType;

cbsb.Write(
SymbolTypeUtil.AccessibilityToModifier(
Expand All @@ -158,38 +155,60 @@ var constMembers
cbsb.Write(typeV2.GetQualifiedNameFromCurrentSymbol(memberTypeV2));
cbsb.Write(" ");

cbsb.Write(memberSymbol.Name.EscapeKeyword());

switch (memberSymbol) {
case IMethodSymbol methodSymbol: {
cbsb.Write(methodSymbol.TypeParameters.GetGenericParameters());
cbsb.Write("(");

var memberName = memberSymbol.Name;
// Property
if (memberName.StartsWith("get_")) {
if (methodSymbol.AssociatedSymbol?.Name != "this[]") {
cbsb.Write(memberSymbol.Name.Substring(4).EscapeKeyword());
cbsb.WriteLine(" { get; }");
} else {
for (var i = 0; i < methodSymbol.Parameters.Length; ++i) {
cbsb.Write("this[");

if (i > 0) {
cbsb.Write(", ");
}

var parameterSymbol = methodSymbol.Parameters[i];
var parameterTypeV2 = TypeV2.FromSymbol(parameterSymbol.Type);
var parameterTypeV2
= TypeV2.FromSymbol(parameterSymbol.Type);
cbsb.Write(
typeV2.GetQualifiedNameFromCurrentSymbol(
parameterTypeV2));
cbsb.Write(" ");
cbsb.Write(parameterSymbol.Name.EscapeKeyword());
}

cbsb.Write(")");
cbsb.Write(
methodSymbol.TypeParameters.GetTypeConstraints(typeV2));

cbsb.WriteLine(";");
break;
cbsb.WriteLine("] { get; }");
}
case IPropertySymbol: {
cbsb.WriteLine(" { get; }");
break;
}
// Method
else {
cbsb.Write(memberSymbol.Name.EscapeKeyword());
cbsb.Write(methodSymbol.TypeParameters
.GetGenericParameters());
cbsb.Write("(");

for (var i = 0; i < methodSymbol.Parameters.Length; ++i) {
if (i > 0) {
cbsb.Write(", ");
}

var parameterSymbol = methodSymbol.Parameters[i];
var parameterTypeV2
= TypeV2.FromSymbol(parameterSymbol.Type);
cbsb.Write(
typeV2.GetQualifiedNameFromCurrentSymbol(
parameterTypeV2));
cbsb.Write(" ");
cbsb.Write(parameterSymbol.Name.EscapeKeyword());
}

cbsb.Write(")");
cbsb.Write(
methodSymbol.TypeParameters.GetTypeConstraints(typeV2));

cbsb.WriteLine(";");
}
}

Expand Down
29 changes: 27 additions & 2 deletions Schema/src/util/symbols/SymbolTypeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using schema.binary;
using schema.binary.attributes;
using schema.binary.parser;
using schema.util.asserts;
using schema.util.diagnostics;
using schema.util.types;

Expand Down Expand Up @@ -369,13 +370,37 @@ public static string GetQualifiedNameFromCurrentSymbol(
return overrideName ?? referencedSymbol.Name.EscapeKeyword();
}

var sb = new StringBuilder();

if (referencedSymbol is
{ Name: "ValueTuple", FullyQualifiedNamespace: "System" }) {
sb.Append("(");

var genericArguments = referencedSymbol.GetTupleElements().ToArray();
for (var i = 0; i < genericArguments.Length; ++i) {
if (i > 0) {
sb.Append(", ");
}

var (tupleItemName, tupleItemType) = genericArguments[i];
sb.Append(sourceSymbol
.GetQualifiedNameFromCurrentSymbol(tupleItemType));
if (tupleItemName.Length > 0 && tupleItemName != $"Item{1 + i}") {
sb.Append(" ");
sb.Append(tupleItemName);
}
}

sb.Append(")");

return sb.ToString();
}

var currentNamespace
= sourceSymbol.NamespaceParts.Select(EscapeKeyword).ToArray();
var referencedNamespace =
referencedSymbol.NamespaceParts.Select(EscapeKeyword).ToArray();

var sb = new StringBuilder();

string mergedNamespaceText;
if (currentNamespace.Length == 0 && referencedNamespace.Length == 0) {
mergedNamespaceText = "";
Expand Down
4 changes: 4 additions & 0 deletions Schema/src/util/types/BSymbolTypeV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public abstract bool HasGenericArguments(
public abstract bool IsGenericTypeParameter(
out IEnumerable<ITypeV2> genericConstraints);

public abstract IEnumerable<(string, ITypeV2)> GetTupleElements();


public abstract bool ContainsMemberWithType(ITypeV2 other);

public abstract bool HasAttribute<TAttribute>()
Expand Down Expand Up @@ -69,6 +72,7 @@ public string FullyQualifiedName {
return $"{namespacePortion}{declaringTypesPortion}{name}";
}
}

private bool Matches_(string name,
string? fullyQualifiedNamespace,
int genericArgCount)
Expand Down
1 change: 1 addition & 0 deletions Schema/src/util/types/ITypeV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public interface ITypeV2 {
IEnumerable<ITypeV2> GenericArguments { get; }
bool IsGenericTypeParameter(out IEnumerable<ITypeV2> genericConstraints);
IEnumerable<ITypeV2> GenericConstraints { get; }
IEnumerable<(string, ITypeV2)> GetTupleElements();

bool HasNullableAnnotation { get; }
bool IsAtLeastAsBinaryConvertibleAs(ITypeV2 other);
Expand Down
Loading

0 comments on commit 02b624c

Please sign in to comment.