Skip to content

Commit

Permalink
Merge pull request #23 from tecAmoRaller/main
Browse files Browse the repository at this point in the history
[fix] filter  return attributes for Property
  • Loading branch information
beakona authored Mar 15, 2024
2 parents e099142 + f9b803f commit 11bf34c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
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

0 comments on commit 11bf34c

Please sign in to comment.