Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/PropertyResolvers.Generators/PropertyResolverGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public class PropertyResolverGenerator : IIncrementalGenerator
/// <inheritdoc />
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// // Register the attribute source
// context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
// "GeneratePropertyResolverAttribute.g.cs",
// SourceText.From(AttributeSourceCode, Encoding.UTF8)));

// Get configs and root namespace from compilation
var compilationData = context.CompilationProvider
.Select((compilation, _) => (
Expand Down Expand Up @@ -239,18 +234,12 @@ private static void GenerateResolvers(
.Select(p => (TypeFullName: t.FullName, PropertyName: p.Name, p.IsNullable)))
.ToList();

if (matches.Count == 0)
{
continue;
}

const string returnType = "string?";
var methodName = $"Get{config.PropertyName}";

methods.AppendLine($" public static {returnType} {methodName}(object? obj) => obj switch");
methods.AppendLine(" {");


foreach (var (typeName, propertyName, isNullable) in matches)
{
var toStringCall = isNullable ? "?.ToString()" : ".ToString()";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ public class Order

var generatedCode = generatedFile.GetText().ToString();
Assert.Contains("public static class NonExistentPropertyResolver", generatedCode);
Assert.DoesNotContain("public static string?", generatedCode);
Assert.Contains("public static string? GetNonExistentProperty(object? obj)", generatedCode);
Assert.Contains("_ => null", generatedCode);
}

[Fact]
Expand Down Expand Up @@ -449,7 +450,10 @@ public class AnotherGeneric<TKey, TValue>

var generatedCode = generatedFile.GetText().ToString();
Assert.Contains("public static class AccountIdResolver", generatedCode);
Assert.DoesNotContain("public static string?", generatedCode);
Assert.Contains("public static string? GetAccountId(object? obj)", generatedCode);
Assert.Contains("_ => null", generatedCode);
// No type-specific match arms since all types are generic
Assert.DoesNotContain("global::TestNamespace", generatedCode);
}

[Fact]
Expand Down