Skip to content

Commit

Permalink
Fixed dumb bug where default enum values wouldn't be set right in gen…
Browse files Browse the repository at this point in the history
…erated readonly code.
  • Loading branch information
MeltyPlayer committed Apr 26, 2024
1 parent d75513f commit a26ce0c
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 23 deletions.
42 changes: 40 additions & 2 deletions Schema Tests/readOnly/DefaultValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace schema.readOnly {
internal class DefaultValueTests {
[Test]
[TestCase("null")]
[TestCase("false")]
[TestCase("true")]
[TestCase("null")]
public void TestSupportsDefaultBools(string boolValue) {
ReadOnlyGeneratorTestUtil.AssertGenerated(
$$"""
Expand Down Expand Up @@ -36,10 +36,10 @@ public interface IReadOnlyWrapper {
}

[Test]
[TestCase("null")]
[TestCase("0")]
[TestCase("-123")]
[TestCase("123")]
[TestCase("null")]
public void TestSupportsDefaultInts(string intValue) {
ReadOnlyGeneratorTestUtil.AssertGenerated(
$$"""
Expand All @@ -66,5 +66,43 @@ public interface IReadOnlyWrapper {

""");
}

[Test]
[TestCase("null", "null")]
[TestCase("SomeType.FOO", "(other.SomeType) 123")]
public void
TestSupportsDefaultEnums(string enumValue, string readonlyValue) {
ReadOnlyGeneratorTestUtil.AssertGenerated(
$$"""
using schema.readOnly;
using foo.bar.other;

namespace foo.bar.other {
public enum SomeType {
FOO = 123,
}
}

namespace foo.bar {
[GenerateReadOnly]
public partial interface IWrapper {
[Const]
public void Foo(SomeType? value = {{enumValue}});
}
}
""",
$$"""
namespace foo.bar {
public partial interface IWrapper : IReadOnlyWrapper {
void IReadOnlyWrapper.Foo(other.SomeType? value) => Foo(value);
}

public interface IReadOnlyWrapper {
public void Foo(other.SomeType? value = {{readonlyValue}});
}
}

""");
}
}
}
11 changes: 11 additions & 0 deletions Schema Tests/readOnly/Enums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace schema.readOnly {
internal enum SomeEnum {
FOO = 123,
}

[GenerateReadOnly]
internal partial interface ISomeWrapper {
[Const]
void Foo(SomeEnum? bar = SomeEnum.FOO);
}
}
2 changes: 1 addition & 1 deletion Schema/src/binary/parser/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private void ParseNullable_(ref ITypeSymbol typeSymbol,
Asserts.True(typeSymbol.IsGeneric(out _, out var genericArguments));
typeSymbol = genericArguments.ToArray()[0];
isNullable = true;
} else if (typeSymbol.IsNullable()) {
} else if (typeSymbol.IsNullable(out _)) {
isNullable = true;
}
}
Expand Down
42 changes: 26 additions & 16 deletions Schema/src/readOnly/ReadOnlyTypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,27 +342,37 @@ var isIndexer
.Write(" ")
.Write(parameterSymbol.Name.EscapeKeyword());



if (interfaceName == null &&
parameterSymbol.HasExplicitDefaultValue) {
var defaultValueType = parameterSymbol.Type.UnwrapNullable();

cbsb.Write(" = ");

var explicitDefaultValue = parameterSymbol.ExplicitDefaultValue;
switch (explicitDefaultValue) {
case null:
cbsb.Write("null");
break;
case char:
cbsb.Write($"'{explicitDefaultValue}'");
break;
case string:
cbsb.Write($"\"{explicitDefaultValue}\"");
break;
case bool boolValue:
cbsb.Write(boolValue ? "true" : "false");
break;
default:
cbsb.Write(explicitDefaultValue.ToString());
break;
if (defaultValueType.IsEnum(out _) &&
explicitDefaultValue != null) {
cbsb.Write(
$"({typeSymbol.GetQualifiedNameFromCurrentSymbol(defaultValueType)}) {explicitDefaultValue}");
} else {
switch (explicitDefaultValue) {
case null:
cbsb.Write("null");
break;
case char:
cbsb.Write($"'{explicitDefaultValue}'");
break;
case string:
cbsb.Write($"\"{explicitDefaultValue}\"");
break;
case bool boolValue:
cbsb.Write(boolValue ? "true" : "false");
break;
default:
cbsb.Write(explicitDefaultValue.ToString());
break;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Schema/src/util/symbols/SymbolTypeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static string GetQualifiedNameFromCurrentSymbol(
$"{sourceSymbol.GetQualifiedNameFromCurrentSymbol(elementType, null, convertName, getNamespaceParts)}[]";
}

if (referencedSymbol.IsNullable()) {
if (referencedSymbol.IsNullable(out _)) {
if (referencedSymbol.IsType(typeof(Nullable<>))) {
var referencedNamedTypeSymbol
= Asserts.AsA<INamedTypeSymbol>(referencedSymbol);
Expand Down
25 changes: 22 additions & 3 deletions Schema/src/util/symbols/TypeSymbolUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

using Microsoft.CodeAnalysis;

Expand All @@ -15,8 +16,26 @@ public static bool IsArray(this ITypeSymbol typeSymbol,
return false;
}

public static bool IsNullable(this ITypeSymbol typeSymbol)
=> typeSymbol.NullableAnnotation == NullableAnnotation.Annotated;
public static ITypeSymbol UnwrapNullable(this ITypeSymbol typeSymbol)
=> typeSymbol.IsNullable(out var nullableType)
? nullableType
: typeSymbol;

public static bool IsNullable(this ITypeSymbol typeSymbol,
out ITypeSymbol nullableType) {
if (typeSymbol.IsType(typeof(Nullable<>))) {
nullableType = (typeSymbol as INamedTypeSymbol).TypeArguments[0];
return true;
}

if (typeSymbol.NullableAnnotation == NullableAnnotation.Annotated) {
nullableType = typeSymbol.OriginalDefinition;
return true;
}

nullableType = default;
return false;
}

public static IEnumerable<INamedTypeSymbol> GetBaseTypes(
this ISymbol symbol) {
Expand Down

0 comments on commit a26ce0c

Please sign in to comment.