Skip to content

Commit bbb4f00

Browse files
committed
Support nullable
1 parent 619f6b7 commit bbb4f00

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

sourcegeneration/StereologueSourceGenerator/LoggableMember.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ internal enum DeclarationModifiers
3535
{
3636
None,
3737
AsSpan,
38-
LongCast
38+
LongCast,
39+
AllowNullConditionalOperator,
3940
}
4041

4142
// Contains all information about a loggable member
@@ -46,16 +47,35 @@ internal static class LoggableMemberExtensions
4647
private static (DeclarationType, DeclarationModifiers)? GetDeclarationType(this ITypeSymbol typeSymbol, LogAttributeInfo attributeInfo, CancellationToken token)
4748
{
4849
token.ThrowIfCancellationRequested();
50+
51+
var modifiers = DeclarationModifiers.None;
52+
53+
if (typeSymbol.IsReferenceType)
54+
{
55+
modifiers = DeclarationModifiers.AllowNullConditionalOperator;
56+
}
57+
else if (typeSymbol.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
58+
{
59+
// Pull out the inner type
60+
var namedTypeSymbol = (INamedTypeSymbol)typeSymbol;
61+
var innerType = namedTypeSymbol.TypeArguments[0];
62+
typeSymbol = innerType;
63+
64+
modifiers = DeclarationModifiers.AllowNullConditionalOperator;
65+
}
66+
67+
token.ThrowIfCancellationRequested();
68+
4969
// If we know we're generating a loggable implementation
5070
if (typeSymbol.GetAttributes().Where(x => x.AttributeClass?.ToDisplayString() == "Stereologue.GenerateLogAttribute").Any())
5171
{
52-
return (DeclarationType.Logged, DeclarationModifiers.None);
72+
return (DeclarationType.Logged, modifiers);
5373
}
5474
token.ThrowIfCancellationRequested();
5575
// If we know we already implement ILogged
5676
if (typeSymbol.AllInterfaces.Where(x => x.ToDisplayString() == "Stereologue.ILogged").Any())
5777
{
58-
return (DeclarationType.Logged, DeclarationModifiers.None);
78+
return (DeclarationType.Logged, modifiers);
5979
}
6080
token.ThrowIfCancellationRequested();
6181
// If we have an UpdateMonologue function
@@ -79,7 +99,7 @@ private static (DeclarationType, DeclarationModifiers)? GetDeclarationType(this
7999
}
80100
if (parameters[0].Type.SpecialType == SpecialType.System_String && parameters[1].Type.ToDisplayString() == "Stereologue.Stereologuer")
81101
{
82-
return (DeclarationType.Logged, DeclarationModifiers.None);
102+
return (DeclarationType.Logged, modifiers);
83103
}
84104
}
85105
}

sourcegeneration/StereologueSourceGenerator/LoggableType.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,20 @@ private static void ConstructCall(LoggableMember data, StringBuilder builder, So
171171
if (data.LoggedType == DeclarationType.Logged)
172172
{
173173
// TODO check log type to see if we should actually do this
174-
// TODO nullable check
175174
// TODO arrays of loggables
176175
builder.Append(getOperation);
177-
builder.Append("UpdateStereoLogue($\"{path}/");
176+
if (data.LoggedModifiers == DeclarationModifiers.AllowNullConditionalOperator)
177+
{
178+
builder.Append("?");
179+
}
180+
builder.Append(".UpdateStereologue($\"{path}/");
178181
builder.Append(path);
179-
builder.Append("\", logger, ");
180-
builder.Append(data.AttributeInfo.LogLevel);
181-
builder.Append(");");
182+
builder.Append("\", logger);");
182183
return;
183184
}
184185

185-
var logCall = data.LoggedType switch {
186+
var logCall = data.LoggedType switch
187+
{
186188
DeclarationType.Struct => "LogStruct",
187189
DeclarationType.StructArray => "LogStructArray",
188190
DeclarationType.Protobuf => "LogProto",

test/stereologue.test/TestTree.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,14 @@ public partial class GenerateAllKnownTypes
162162

163163
[Log(UseProtobuf = true)]
164164
public Rotation2d RotationProto => new();
165+
166+
[Log]
167+
public GenerateStruct NonNullStruct;
168+
[Log]
169+
public GenerateStruct? NullStruct;
170+
171+
[Log]
172+
public GenerateClass NonNullClass = null!;
173+
[Log]
174+
public GenerateClass? NullClass;
165175
}

0 commit comments

Comments
 (0)