Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] filter return attributes for Property #23

Merged
merged 1 commit into from
Mar 15, 2024
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
2 changes: 2 additions & 0 deletions AutoInterfaceSample/AutoInterfaceSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

<ItemGroup>
<Analyzer Include="..\BeaKona.AutoInterfaceGenerator\bin\Debug\netstandard2.0\BeaKona.AutoInterfaceGenerator.dll" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="Serilog" Version="3.1.1" />
<ProjectReference Include="..\BeaKona.AutoInterfaceAttributes\BeaKona.AutoInterfaceAttributes.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
</ItemGroup>

Expand Down
41 changes: 35 additions & 6 deletions AutoInterfaceSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
using TestInterfaces.A.B;
using System;
using System.Diagnostics.CodeAnalysis;

namespace AutoInterfaceSample.Test
{
public partial record TestRecord([property: BeaKona.AutoInterface(IncludeBaseInterfaces = true)] ITestable Testable)
{
}
public partial record TestRecord(
[property: BeaKona.AutoInterface(IncludeBaseInterfaces = true)] NLog.ILogger nlog,
[property: BeaKona.AutoInterface(IncludeBaseInterfaces = true)] Serilog.ILogger slog,
[property: BeaKona.AutoInterface(IncludeBaseInterfaces = true)] Microsoft.Extensions.Logging.ILogger melog

) : NLog.ILogger;

public class Program
{
public static void Main()
{
//System.Diagnostics.Debug.WriteLine(BeaKona.Output.Debug_TestRecord.Info);

ITestable p = new TestRecord(new SimpleLogger());
// ITestable p = new TestRecord(new SimpleLogger());

p.Test();
// p.Test();

//p.Log<int?>(LogLevel.Debug, default, 1, null, null);
//var result = p.BindProperty<int>("test", 1, false, out var property, 1, 2, 3);
}
}

partial record TestObsoleteRecord(
[property: BeaKona.AutoInterface(IncludeBaseInterfaces = true)]
ITestInterfaceObsolete Testable) : ITestInterfaceObsolete;
interface ITestInterfaceObsolete
{
[Obsolete]
[return: System.Diagnostics.CodeAnalysis.MaybeNull]
[return: NotNullIfNotNullAttribute("test")]
[return: TestReturnAttribute]
ResObject? TestObsoleteMethod(string? test);

[Obsolete]
[System.Diagnostics.CodeAnalysis.DisallowNull]
ResObject? TestObsoleteProperty { get; }


}

[Obsolete]

class ResObject;

[AttributeUsage(AttributeTargets.All)]
class TestReturnAttribute : Attribute;
}
21 changes: 20 additions & 1 deletion BeaKona.AutoInterfaceGenerator/CSharpCodeTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,25 @@ private IEnumerable<AttributeData> GetForwardAttributes(ISymbol symbol)
}
}

private IEnumerable<AttributeData> GetReturnAttributes(IPropertySymbol method)
{
var attributes = method.GetAttributes().Where(IsPublicAccess).ToList();

foreach (var attribute in attributes)
{
if (attribute.AttributeClass is INamedTypeSymbol attributeClass)
{
foreach (var typeSymbol in this.returnAttributeSymbols)
{
if (attributeClass.Equals(typeSymbol, SymbolEqualityComparer.Default))
{
yield return attribute;
break;
}
}
}
}
}
private IEnumerable<AttributeData> GetReturnAttributes(IMethodSymbol method)
{
var attributes = method.GetReturnTypeAttributes().Where(IsPublicAccess).ToList();
Expand Down Expand Up @@ -654,7 +673,7 @@ public void WritePropertyDefinition(SourceBuilder builder, IPropertySymbol prope
PartialTemplate? setterTemplate = this.GetMatchedTemplates(references, setterTarget, property.IsIndexer ? "this" : property.Name);

this.WriteForwardAttributes(builder, property);
this.WriteReturnAttributes(builder, property.Type.GetAttributes());
this.WriteReturnAttributes(builder, GetReturnAttributes(property));

builder.AppendIndentation();
this.WriteTypeReference(builder, property.Type, scope);
Expand Down