Skip to content

Commit f4d8cb9

Browse files
authored
Merge pull request #7 from altasoft/bugfix/nullablewarningsForImplicitOperators
Bump version to 1.3.2 and refactor nullable handling across core files
2 parents 3015b32 + 1a02b6e commit f4d8cb9

8 files changed

Lines changed: 40 additions & 70 deletions

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<Product>Choice generator</Product>
1010
<Company>ALTA Software llc.</Company>
1111
<Copyright>Copyright © 2024 ALTA Software llc.</Copyright>
12-
<Version>1.3.1</Version>
12+
<Version>1.3.2</Version>
1313
</PropertyGroup>
1414

1515
<PropertyGroup>

src/AltaSoft.Choice.Generator/Executor.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,14 @@ private static SourceCodeBuilder Process(INamedTypeSymbol typeSymbol, List<IProp
104104
if (p.Summary is not null)
105105
sb.AppendSummary(p.Summary);
106106

107-
sb.AppendLine("[DisallowNull]");
108-
109107
var isDateOnly = p.IsDateOnly();
110108
if (isDateOnly)
111109
{
112110
sb.AppendLine("[XmlIgnore]");
113111
}
114112
else
115113
{
114+
sb.AppendLine("[DisallowNull]");
116115
sb.Append("[XmlElement(\"").Append(p.XmlNameValue).AppendLine("\")]");
117116
}
118117

@@ -234,10 +233,15 @@ private static bool ProcessImplicitOperators(SourceCodeBuilder sb, string typeNa
234233
{
235234
sb.AppendSummary($"Implicitly converts an <see cref=\"{property.TypeName}\"/> to an <see cref=\"{typeName}\"/>.");
236235
sb.AppendParamDescription("value", $"The <see cref=\"{property.TypeName}\"/> to convert.");
237-
sb.AppendBlock("returns", $"<see cref=\"{typeName}\"/> instance representing the code.");
236+
sb.AppendBlock("returns", $"<see cref=\"{typeName}\"/> instance representing the code.").NewLine();
237+
sb.AppendLine("[return: NotNullIfNotNull(parameterName: nameof(value))]");
238+
239+
sb.Append("public static implicit operator ").Append(typeName).Append("? (")
240+
.Append(property.TypeName).AppendLine("? value) ")
241+
.OpenBracket()
242+
.Append("return value is null ? null : CreateAs").Append(property.Name).Append("(value").AppendIf(property.TypeSymbol.IsValueType, ".Value").AppendLine(");")
243+
.CloseBracket();
238244

239-
sb.Append("public static implicit operator ").Append(typeName).Append("(")
240-
.Append(property.TypeName).Append(" value) => CreateAs").Append(property.Name).AppendLine("(value);");
241245
sb.NewLine();
242246
}
243247

src/AltaSoft.Choice.Generator/Extensions/CompilationExt.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static string GetClassNameWithArguments(this INamedTypeSymbol? type)
143143

144144
return builder.ToString();
145145
}
146-
146+
#pragma warning disable S1643
147147
public static string GetFullName(this ITypeSymbol type)
148148
{
149149
var ns = type.ContainingNamespace?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.OmittedAsContaining))!;
@@ -175,12 +175,14 @@ public static string GetFullName(this ITypeSymbol type)
175175
for (var i = 0; i < typeParameters.Length; ++i)
176176
{
177177
var typeParamName = typeParameters[i].ToString();
178+
178179
friendlyName += i == 0 ? typeParamName : "," + typeParamName;
180+
179181
}
180182
friendlyName += ">";
181183
return ns + '.' + friendlyName;
182184
}
183-
185+
#pragma warning restore S1643
184186
/// <summary>
185187
/// Determines whether the specified type symbol is a Nullable&lt;T&gt; value type and, if so, provides the underlying value type symbol.
186188
/// </summary>

src/AltaSoft.Choice.Generator/Extensions/RoslynExt.cs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,6 @@ namespace AltaSoft.Choice.Generator.Extensions;
99
/// </summary>
1010
internal static class RoslynExt
1111
{
12-
///// <summary>
13-
///// Gets the location of the attribute data within the source code.
14-
///// </summary>
15-
///// <param name="self">The attribute data to retrieve the location for.</param>
16-
///// <returns>The location of the attribute data in the source code, or null if not found.</returns>
17-
//public static Location? GetAttributeLocation(this AttributeData self)
18-
//{
19-
// var syntaxReference = self.ApplicationSyntaxReference;
20-
21-
// var syntax = (AttributeSyntax?)syntaxReference?.GetSyntax();
22-
23-
// return syntax?.GetLocation();
24-
//}
25-
26-
///// <summary>
27-
///// Checks if the symbol has a default constructor (parameterless constructor) defined and retrieves its location.
28-
///// </summary>
29-
///// <param name="self">The symbol to check for a default constructor.</param>
30-
///// <param name="location">When this method returns, contains the location of the default constructor, if found; otherwise, null.</param>
31-
///// <returns>True if a default constructor is found; otherwise, false.</returns>
32-
//public static bool HasDefaultConstructor(this ISymbol? self, out Location? location)
33-
//{
34-
// var constructors = self.GetConstructorsFromSyntaxTree();
35-
36-
// var ctor = constructors?.Find(x => x.ParameterList.Parameters.Count == 0);
37-
// location = ctor?.GetLocation();
38-
// return ctor is not null;
39-
//}
40-
41-
///// <summary>
42-
///// Retrieves a list of constructor declarations associated with the symbol from the syntax tree.
43-
///// </summary>
44-
///// <param name="self">The symbol for which to retrieve constructor declarations.</param>
45-
///// <returns>A list of constructor declarations or null if none are found.</returns>
46-
//public static List<ConstructorDeclarationSyntax>? GetConstructorsFromSyntaxTree(this ISymbol? self)
47-
//{
48-
// var declaringSyntaxReferences = self?.DeclaringSyntaxReferences;
49-
50-
// if (self is null || declaringSyntaxReferences is null or { Length: 0 })
51-
// return null;
52-
53-
// List<ConstructorDeclarationSyntax>? result = null;
54-
55-
// foreach (var syntax in declaringSyntaxReferences)
56-
// {
57-
// if (syntax.GetSyntax() is TypeDeclarationSyntax classDeclaration && string.Equals(classDeclaration.GetClassFullName(), self.ToString(), System.StringComparison.Ordinal))
58-
// {
59-
// var constructors = classDeclaration.Members.OfType<ConstructorDeclarationSyntax>();
60-
61-
// result ??= [];
62-
// result.AddRange(constructors);
63-
// }
64-
// }
65-
// return result;
66-
//}
67-
6812
/// <summary>
6913
/// Gets the namespace of the specified type declaration syntax.
7014
/// </summary>

src/AltaSoft.Choice.Generator/Helpers/SourceCodeBuilder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,7 @@ public SourceCodeBuilder AppendLines(IEnumerable<string> lines)
367367
{
368368
_sb.AppendLine();
369369
}
370-
else
371-
if (line[0] == '#')
370+
else if (line[0] == '#')
372371
{
373372
_sb.AppendLine(line);
374373
}

tests/AltaSoft.Choice.Generator.SnapshotTests/Snapshots/ChoiceGeneratorTest.ChoiceTypeShouldGenerateAllMethodsAndCompileCorrectly#Authorisation1Choice.g.verified.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ public void Switch(
143143
/// <returns>
144144
/// <see cref="Authorisation1Choice"/> instance representing the code.
145145
/// </returns>
146-
public static implicit operator Authorisation1Choice(TestNamespace.OtherNamespace.Authorisation1Code value) => CreateAsCode(value);
146+
147+
[return: NotNullIfNotNull(parameterName: nameof(value))]
148+
public static implicit operator Authorisation1Choice? (TestNamespace.OtherNamespace.Authorisation1Code? value)
149+
{
150+
return value is null ? null : CreateAsCode(value.Value);
151+
}
147152

148153
/// <summary>
149154
/// Implicitly converts an <see cref="string"/> to an <see cref="Authorisation1Choice"/>.
@@ -152,7 +157,12 @@ public void Switch(
152157
/// <returns>
153158
/// <see cref="Authorisation1Choice"/> instance representing the code.
154159
/// </returns>
155-
public static implicit operator Authorisation1Choice(string value) => CreateAsProprietary(value);
160+
161+
[return: NotNullIfNotNull(parameterName: nameof(value))]
162+
public static implicit operator Authorisation1Choice? (string? value)
163+
{
164+
return value is null ? null : CreateAsProprietary(value);
165+
}
156166

157167
/// <summary>
158168
/// Determines whether the <see cref="Code"/> property should be serialized.

tests/AltaSoft.Choice.Generator.SnapshotTests/Snapshots/ChoiceGeneratorTest.ChoiceTypeShouldNotGenerateImplicitMethodsAndCompileCorrectly#Authorisation1Choice.g.verified.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ public void Switch(
143143
/// <returns>
144144
/// <see cref="Authorisation1Choice"/> instance representing the code.
145145
/// </returns>
146-
public static implicit operator Authorisation1Choice(string value) => CreateAsCode(value);
146+
147+
[return: NotNullIfNotNull(parameterName: nameof(value))]
148+
public static implicit operator Authorisation1Choice? (string? value)
149+
{
150+
return value is null ? null : CreateAsCode(value);
151+
}
147152

148153
/// <summary>
149154
/// Implicitly converts an <see cref="TestNamespace.OtherNamespace.Authorisation1Code"/> to an <see cref="Authorisation1Choice"/>.
@@ -152,7 +157,12 @@ public void Switch(
152157
/// <returns>
153158
/// <see cref="Authorisation1Choice"/> instance representing the code.
154159
/// </returns>
155-
public static implicit operator Authorisation1Choice(TestNamespace.OtherNamespace.Authorisation1Code value) => CreateAsProprietary(value);
160+
161+
[return: NotNullIfNotNull(parameterName: nameof(value))]
162+
public static implicit operator Authorisation1Choice? (TestNamespace.OtherNamespace.Authorisation1Code? value)
163+
{
164+
return value is null ? null : CreateAsProprietary(value.Value);
165+
}
156166

157167
/// <summary>
158168
/// Determines whether the <see cref="Proprietary"/> property should be serialized.

tests/AltaSoft.ChoiceGenerator.Tests/ChoiceGeneratorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,4 @@ public void ImplicitConversions_ShouldConvertCorrectly()
300300

301301
}
302302
}
303+

0 commit comments

Comments
 (0)