Skip to content

Commit

Permalink
Add proto
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Feb 17, 2024
1 parent 4a5da8f commit f91cbdf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
9 changes: 5 additions & 4 deletions sourcegeneration/StereologueSourceGenerator/LoggableMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ internal enum DeclarationKind
ReadOnlyMemory,
Memory,
Array,
Nullable,
NullableValueType,
NullableReferenceType
}

// Contains all information about a loggable member
Expand All @@ -58,7 +59,7 @@ private static DeclarationKind GetInnerType(this ITypeSymbol typeSymbol, out ITy
{
namedTypeSymbol = (INamedTypeSymbol)typeSymbol;
innerType = namedTypeSymbol.TypeArguments[0];
return DeclarationKind.Nullable;
return DeclarationKind.NullableValueType;
}

var fmt = new SymbolDisplayFormat(typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces);
Expand All @@ -83,7 +84,7 @@ private static DeclarationKind GetInnerType(this ITypeSymbol typeSymbol, out ITy
}

innerType = typeSymbol;
return innerType.IsReferenceType ? DeclarationKind.Nullable : DeclarationKind.None;
return innerType.IsReferenceType ? DeclarationKind.NullableReferenceType : DeclarationKind.None;
}

private static (DeclarationType, DeclarationKind)? GetDeclarationType(this ITypeSymbol typeSymbol, LogAttributeInfo attributeInfo, CancellationToken token)
Expand Down Expand Up @@ -162,7 +163,7 @@ private static (DeclarationType, DeclarationKind)? GetDeclarationType(this IType
}

// Special case non nested and nullables
if (nestedKind == DeclarationKind.None || nestedKind == DeclarationKind.Nullable)
if (nestedKind == DeclarationKind.None || nestedKind == DeclarationKind.NullableReferenceType || nestedKind == DeclarationKind.NullableValueType)
{
return (fullTypeName switch
{
Expand Down
21 changes: 19 additions & 2 deletions sourcegeneration/StereologueSourceGenerator/LoggableType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,35 @@ private static void ConstructCall(LoggableMember data, StringBuilder builder, So

if (data.LoggedType == DeclarationType.Protobuf)
{
if (data.LoggedKind != DeclarationKind.None && data.LoggedKind != DeclarationKind.NullableValueType && data.LoggedKind != DeclarationKind.NullableReferenceType)
{
builder.Append("Cannot log an array of protobufs");
}
if (data.LoggedKind == DeclarationKind.NullableValueType)
{
getOperation = $"{getOperation}.GetValueOrDefault()";
}
builder.Append("logger.LogProto($\"{path}/");
builder.Append(path);
builder.Append("\", ");
builder.Append(data.AttributeInfo.LogType);
builder.Append(", ");
builder.Append(getOperation);
builder.Append(", ");
builder.Append(data.AttributeInfo.LogLevel);
builder.Append(");");
return;
}

string logMethod;

if (data.LoggedKind == DeclarationKind.None || data.LoggedKind == DeclarationKind.Nullable)
if (data.LoggedKind == DeclarationKind.None || data.LoggedKind == DeclarationKind.NullableReferenceType || data.LoggedKind == DeclarationKind.NullableValueType)
{
if (data.LoggedType == DeclarationType.String)
{
getOperation = $"{getOperation}.AsSpan()";
}
else if (data.LoggedKind == DeclarationKind.Nullable)
else if (data.LoggedKind == DeclarationKind.NullableValueType)
{
getOperation = $"{getOperation}.GetValueOrDefault()";
}
Expand Down
3 changes: 3 additions & 0 deletions test/stereologue.test/TestTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ public partial class GenerateAllKnownTypes
[Log(UseProtobuf = true)]
public Rotation2d RotationProto => new();

[Log(UseProtobuf = true)]
public Rotation2d? RotationProtoNullable => null;

[Log()]
public Rotation2d[] RotationArray = [];

Expand Down

0 comments on commit f91cbdf

Please sign in to comment.