Skip to content

Commit

Permalink
Support nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Feb 17, 2024
1 parent 619f6b7 commit bbb4f00
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
28 changes: 24 additions & 4 deletions sourcegeneration/StereologueSourceGenerator/LoggableMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ internal enum DeclarationModifiers
{
None,
AsSpan,
LongCast
LongCast,
AllowNullConditionalOperator,
}

// Contains all information about a loggable member
Expand All @@ -46,16 +47,35 @@ internal static class LoggableMemberExtensions
private static (DeclarationType, DeclarationModifiers)? GetDeclarationType(this ITypeSymbol typeSymbol, LogAttributeInfo attributeInfo, CancellationToken token)
{
token.ThrowIfCancellationRequested();

var modifiers = DeclarationModifiers.None;

if (typeSymbol.IsReferenceType)
{
modifiers = DeclarationModifiers.AllowNullConditionalOperator;
}
else if (typeSymbol.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
{
// Pull out the inner type
var namedTypeSymbol = (INamedTypeSymbol)typeSymbol;
var innerType = namedTypeSymbol.TypeArguments[0];
typeSymbol = innerType;

modifiers = DeclarationModifiers.AllowNullConditionalOperator;
}

token.ThrowIfCancellationRequested();

// If we know we're generating a loggable implementation
if (typeSymbol.GetAttributes().Where(x => x.AttributeClass?.ToDisplayString() == "Stereologue.GenerateLogAttribute").Any())
{
return (DeclarationType.Logged, DeclarationModifiers.None);
return (DeclarationType.Logged, modifiers);
}
token.ThrowIfCancellationRequested();
// If we know we already implement ILogged
if (typeSymbol.AllInterfaces.Where(x => x.ToDisplayString() == "Stereologue.ILogged").Any())
{
return (DeclarationType.Logged, DeclarationModifiers.None);
return (DeclarationType.Logged, modifiers);
}
token.ThrowIfCancellationRequested();
// If we have an UpdateMonologue function
Expand All @@ -79,7 +99,7 @@ private static (DeclarationType, DeclarationModifiers)? GetDeclarationType(this
}
if (parameters[0].Type.SpecialType == SpecialType.System_String && parameters[1].Type.ToDisplayString() == "Stereologue.Stereologuer")
{
return (DeclarationType.Logged, DeclarationModifiers.None);
return (DeclarationType.Logged, modifiers);
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions sourcegeneration/StereologueSourceGenerator/LoggableType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,20 @@ private static void ConstructCall(LoggableMember data, StringBuilder builder, So
if (data.LoggedType == DeclarationType.Logged)
{
// TODO check log type to see if we should actually do this
// TODO nullable check
// TODO arrays of loggables
builder.Append(getOperation);
builder.Append("UpdateStereoLogue($\"{path}/");
if (data.LoggedModifiers == DeclarationModifiers.AllowNullConditionalOperator)
{
builder.Append("?");
}
builder.Append(".UpdateStereologue($\"{path}/");
builder.Append(path);
builder.Append("\", logger, ");
builder.Append(data.AttributeInfo.LogLevel);
builder.Append(");");
builder.Append("\", logger);");
return;
}

var logCall = data.LoggedType switch {
var logCall = data.LoggedType switch
{
DeclarationType.Struct => "LogStruct",
DeclarationType.StructArray => "LogStructArray",
DeclarationType.Protobuf => "LogProto",
Expand Down
10 changes: 10 additions & 0 deletions test/stereologue.test/TestTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,14 @@ public partial class GenerateAllKnownTypes

[Log(UseProtobuf = true)]
public Rotation2d RotationProto => new();

[Log]
public GenerateStruct NonNullStruct;
[Log]
public GenerateStruct? NullStruct;

[Log]
public GenerateClass NonNullClass = null!;
[Log]
public GenerateClass? NullClass;
}

0 comments on commit bbb4f00

Please sign in to comment.