Skip to content

Commit 29cf663

Browse files
committed
add support acceptor abstract class
1 parent 6302953 commit 29cf663

File tree

14 files changed

+111
-92
lines changed

14 files changed

+111
-92
lines changed

.github/workflows/dotnet.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464
uses: actions/upload-artifact@v3
6565
with:
6666
name: nupkg
67-
path: artifacts/*.nupkg
67+
path: nupkg/*.nupkg
6868

6969
- name: NuGet add souce
7070
id: nuget_add_source
@@ -73,8 +73,8 @@ jobs:
7373

7474
- name: Publish GitHub Packages
7575
if: ${{ steps.nuget_add_source.conclusion == 'success' }}
76-
run: dotnet nuget push "artifacts/*.nupkg" -k ${{ secrets.GITHUB_TOKEN }} -s "github" --skip-duplicate
76+
run: dotnet nuget push "nupkg/*.nupkg" -k ${{ secrets.GITHUB_TOKEN }} -s "github" --skip-duplicate
7777

7878
- name: Publish NuGet
7979
if: ${{ github.event.inputs.publish_nuget }}
80-
run: dotnet nuget push "artifacts/*.nupkg" -k ${{ secrets.NUGET_OLG_API_KEY }} -s "nuget" --skip-duplicate
80+
run: dotnet nuget push "nupkg/*.nupkg" -k ${{ secrets.NUGET_OLG_API_KEY }} -s "nuget" --skip-duplicate

Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</PropertyGroup>
1414

1515
<PropertyGroup>
16-
<Version>0.0.1</Version>
16+
<Version>0.0.2</Version>
1717
<Authors>hikarin522</Authors>
1818
<Copyright>(c) 2023 hikarin522.</Copyright>
1919
<PackageLicenseExpression>MIT</PackageLicenseExpression>

Directory.Build.targets

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
<Target Name="CopyPackage" AfterTargets="Pack" Condition="$(IsPackable)" >
88
<Copy SourceFiles="$(OutputPath)..\$(PackageId).$(PackageVersion).nupkg" DestinationFolder="$(MSBuildThisFileDirectory)nupkg" />
9-
</Target>
10-
11-
<Target Name="CleanNugetPackageCache" AfterTargets="Clean" Condition="$(IsPackable)" >
12-
<Exec Command="dotnet nuget locals global-packages -c" UseUtf8Encoding="Always" />
9+
<RemoveDir Directories="$(MSBuildThisFileDirectory).nuget\packages\$(PackageId.ToLower())\$(PackageVersion)" />
1310
</Target>
1411

1512
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// <auto-generated/>
2+
3+
namespace Sample
4+
{
5+
partial class SampleBase
6+
{
7+
public abstract System.Threading.Tasks.Task<int> AcceptAsync(Sample.ISampleVisitor visitor);
8+
}
9+
}
10+
11+
namespace Sample
12+
{
13+
partial interface ISampleVisitor
14+
{
15+
System.Threading.Tasks.Task<int> VisitAsync(Sample.Sample1 value);
16+
System.Threading.Tasks.Task<int> VisitAsync(Sample.Sample2 value);
17+
}
18+
}
19+
20+
namespace Sample
21+
{
22+
partial class Sample1: Sample.SampleBase, Sample.IVisitorResult<int>
23+
{
24+
public sealed override async System.Threading.Tasks.Task<int> AcceptAsync(Sample.ISampleVisitor visitor) => await visitor.VisitAsync(this);
25+
}
26+
}
27+
28+
namespace Sample
29+
{
30+
partial class Sample2: Sample.SampleBase, Sample.IVisitorResult<int>
31+
{
32+
public sealed override async System.Threading.Tasks.Task<int> AcceptAsync(Sample.ISampleVisitor visitor) => await visitor.VisitAsync(this);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// <auto-generated/>
2+
3+
namespace Sample
4+
{
5+
[MessagePack.Union(1, typeof(Sample.Sample1))]
6+
[MessagePack.Union(2, typeof(Sample.Sample2))]
7+
partial class SampleBase { }
8+
}

Sample/Sample/SampleBase.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
using VisitorPatternGenerator;
3+
4+
namespace Sample;
5+
6+
[Acceptor(AcceptorOptions.MessagePackUnion)]
7+
public abstract partial class SampleBase { }
8+
9+
[TaskVisitor<SampleBase, int>]
10+
public partial interface ISampleVisitor { }
11+
12+
13+
[Acceptor<SampleBase>]
14+
public partial class Sample1 { }
15+
16+
[Acceptor<SampleBase>]
17+
public partial class Sample2 { }

VisitorPatternGenerator.Tests/ISampleRequest.cs

-28
This file was deleted.

VisitorPatternGenerator.Tests/ISampleResponse.cs

-14
This file was deleted.

VisitorPatternGenerator.Tests/VisitorPatternGenerator.Tests.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<RootNamespace>$(MSBuildProjectName)</RootNamespace>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<Compile Include="..\Sample\Sample\*.cs" LinkBase="Sample" />
10+
</ItemGroup>
11+
812
<ItemGroup>
913
<PackageReference Include="MessagePack.Annotations" Version="2.4.59" />
1014
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />

VisitorPatternGenerator/Templates/AcceptorTemplate.ctor.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ partial class AcceptorTemplate
99
{
1010
internal AcceptorOptions Options { get; }
1111

12-
internal INamedTypeSymbol AcceptorInterface { get; }
12+
internal INamedTypeSymbol Acceptor { get; }
1313

14-
internal ImmutableSortedSet<INamedTypeSymbol> AcceptorTypes { get; }
14+
internal ImmutableSortedSet<INamedTypeSymbol> Acceptors { get; }
1515

1616
internal AcceptorTemplate(
1717
AcceptorOptions options,
18-
INamedTypeSymbol acceptorInterface,
19-
ImmutableSortedSet<INamedTypeSymbol> acceptorTypes
18+
INamedTypeSymbol acceptor,
19+
ImmutableSortedSet<INamedTypeSymbol> acceptors
2020
)
2121
{
2222
this.Options = options;
23-
this.AcceptorInterface = acceptorInterface;
24-
this.AcceptorTypes = acceptorTypes;
23+
this.Acceptor = acceptor;
24+
this.Acceptors = acceptors;
2525
}
2626
}

VisitorPatternGenerator/Templates/AcceptorTemplate.tt

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
<#@ import namespace="Microsoft.CodeAnalysis" #>
44
// <auto-generated/>
55

6-
<# if(!AcceptorInterface.ContainingNamespace.IsGlobalNamespace) { #>
7-
namespace <#= AcceptorInterface.ContainingNamespace.ToDisplayString() #>
6+
<# if(!Acceptor.ContainingNamespace.IsGlobalNamespace) { #>
7+
namespace <#= Acceptor.ContainingNamespace.ToDisplayString() #>
88
{
99
<# PushIndent(" "); } #>
10-
<# if (Options.HasFlag(AcceptorOptions.MessagePackUnion)) { for (var i = 0; i < AcceptorTypes.Count; ++i) { #>
11-
[MessagePack.Union(<#= i + 1 #>, typeof(<#= AcceptorTypes[i].ToDisplayString() #>))]
10+
<# if (Options.HasFlag(AcceptorOptions.MessagePackUnion)) { for (var i = 0; i < Acceptors.Count; ++i) { #>
11+
[MessagePack.Union(<#= i + 1 #>, typeof(<#= Acceptors[i].ToDisplayString() #>))]
1212
<# } } #>
13-
partial interface <#= AcceptorInterface.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat.WithGenericsOptions(SymbolDisplayGenericsOptions.IncludeTypeParameters | SymbolDisplayGenericsOptions.IncludeVariance)) #> { }
14-
<# if(!AcceptorInterface.ContainingNamespace.IsGlobalNamespace) { PopIndent(); #>
13+
partial <#= Acceptor.TypeKind == TypeKind.Interface ? "interface" : "class" #> <#= Acceptor.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat.WithGenericsOptions(SymbolDisplayGenericsOptions.IncludeTypeParameters | SymbolDisplayGenericsOptions.IncludeVariance)) #> { }
14+
<# if(!Acceptor.ContainingNamespace.IsGlobalNamespace) { PopIndent(); #>
1515
}
1616
<# } #>

VisitorPatternGenerator/Templates/Annotations.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ internal enum AcceptorOptions
77
MessagePackUnion = 0x100,
88
}
99

10-
[System.AttributeUsage(System.AttributeTargets.Interface, AllowMultiple = false, Inherited = false)]
10+
[System.AttributeUsage(System.AttributeTargets.Interface | System.AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
1111
internal sealed class AcceptorAttribute: System.Attribute
1212
{
1313
public AcceptorOptions Options { get; }

VisitorPatternGenerator/Templates/VisitorTemplate.ctor.cs

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.Generic;
22
using System.Collections.Immutable;
33
using System.Linq;
4-
using System.Net;
54
using System.Text.RegularExpressions;
65

76
using Microsoft.CodeAnalysis;
@@ -14,36 +13,36 @@ partial class VisitorTemplate
1413

1514
public string TaskName { get; }
1615

17-
public ITypeSymbol? BaseResultType { get; }
16+
public ITypeSymbol? BaseResult { get; }
1817

1918
public INamedTypeSymbol VisitorInterface { get; }
2019

21-
public INamedTypeSymbol AcceptorInterface { get; }
20+
public INamedTypeSymbol Acceptor { get; }
2221

23-
public ImmutableArray<(INamedTypeSymbol Type, INamedTypeSymbol? ResultType)> AcceptorTypes { get; }
22+
public ImmutableArray<(INamedTypeSymbol Type, INamedTypeSymbol? ResultType)> Acceptors { get; }
2423

2524
public VisitorTemplate(
2625
string rootNamespace,
2726
string taskName,
28-
ITypeSymbol? baseResultType,
27+
ITypeSymbol? baseResult,
2928
INamedTypeSymbol visitorInterface,
30-
INamedTypeSymbol acceptorInterface,
29+
INamedTypeSymbol acceptor,
3130
ImmutableArray<(INamedTypeSymbol Type, INamedTypeSymbol? ResultType)> acceptors
3231
)
3332
{
3433
this.RootNamespace = rootNamespace;
3534
this.TaskName = taskName;
36-
this.BaseResultType = baseResultType;
35+
this.BaseResult = baseResult;
3736
this.VisitorInterface = visitorInterface;
38-
this.AcceptorInterface = acceptorInterface;
39-
this.AcceptorTypes = acceptors;
37+
this.Acceptor = acceptor;
38+
this.Acceptors = acceptors;
4039
}
4140

4241
public bool IsAsync => !string.IsNullOrWhiteSpace(this.TaskName);
4342

4443
public string IfAsync(string str) => this.IsAsync ? str : string.Empty;
4544

46-
public bool IsGeneric => this.BaseResultType is ITypeParameterSymbol;
45+
public bool IsGeneric => this.BaseResult is ITypeParameterSymbol;
4746

4847
public string GetTypeParamStr()
4948
{
@@ -96,7 +95,9 @@ public string GetReturnType(ITypeSymbol? resultType)
9695
return resultName is null ? taskName ?? "void" : taskName is null ? resultName : $"{taskName}<{resultName}>";
9796
}
9897

99-
public static string WrapAngle(ITypeSymbol type, ITypeSymbol? resultType = null)
100-
=> $"<{type.ToDisplayString()}" + (resultType is null ? ">" : $", {resultType.ToDisplayString()}>");
98+
public static string GetNamespace(INamedTypeSymbol type)
99+
=> type.ContainingNamespace.ToDisplayString();
101100

101+
public static string GetTypeIdentifier(INamedTypeSymbol type)
102+
=> type.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat.WithGenericsOptions(SymbolDisplayGenericsOptions.IncludeTypeParameters | SymbolDisplayGenericsOptions.IncludeVariance));
102103
}

VisitorPatternGenerator/Templates/VisitorTemplate.tt

+17-17
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,42 @@
33
<#@ import namespace="Microsoft.CodeAnalysis" #>
44
// <auto-generated/>
55

6-
<# if(!AcceptorInterface.ContainingNamespace.IsGlobalNamespace) { #>
7-
namespace <#= AcceptorInterface.ContainingNamespace.ToDisplayString() #>
6+
<# if (!Acceptor.ContainingNamespace.IsGlobalNamespace) { #>
7+
namespace <#= GetNamespace(Acceptor) #>
88
{
99
<# PushIndent(" "); } #>
10-
partial interface <#= AcceptorInterface.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat.WithGenericsOptions(SymbolDisplayGenericsOptions.IncludeTypeParameters | SymbolDisplayGenericsOptions.IncludeVariance)) #>
10+
partial <#= Acceptor.TypeKind == TypeKind.Interface ? "interface" : "class" #> <#= GetTypeIdentifier(Acceptor) #>
1111
{
12-
<#= GetReturnType(BaseResultType) #> Accept<#= IfAsync("Async") #><#= GetTypeParamStr() #>(<#= VisitorInterface.ToDisplayString() #> visitor<#= GetTypeArgListStr() #>)<#= GetTypeConstraintList() #>;
12+
<#= Acceptor.TypeKind == TypeKind.Interface ? "" : "public abstract " #><#= GetReturnType(BaseResult) #> Accept<#= IfAsync("Async") #><#= GetTypeParamStr() #>(<#= VisitorInterface.ToDisplayString() #> visitor<#= GetTypeArgListStr() #>)<#= GetTypeConstraintList() #>;
1313
}
14-
<# if(!AcceptorInterface.ContainingNamespace.IsGlobalNamespace) { PopIndent(); #>
14+
<# if (!Acceptor.ContainingNamespace.IsGlobalNamespace) { PopIndent(); #>
1515
}
1616
<# } #>
1717

18-
<# if(!VisitorInterface.ContainingNamespace.IsGlobalNamespace) { #>
19-
namespace <#= VisitorInterface.ContainingNamespace.ToDisplayString() #>
18+
<# if (!VisitorInterface.ContainingNamespace.IsGlobalNamespace) { #>
19+
namespace <#= GetNamespace(VisitorInterface) #>
2020
{
2121
<# PushIndent(" "); } #>
22-
partial interface <#= VisitorInterface.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat.WithGenericsOptions(SymbolDisplayGenericsOptions.IncludeTypeParameters | SymbolDisplayGenericsOptions.IncludeVariance)) #>
22+
partial interface <#= GetTypeIdentifier(VisitorInterface) #>
2323
{
24-
<# foreach(var (acceptor, result) in AcceptorTypes) { #>
25-
<#= GetReturnType(IsGeneric ? BaseResultType : result ?? BaseResultType) #> Visit<#= IfAsync("Async") #>(<#= acceptor.ToDisplayString() #> value<#= GetTypeArgListStr() #>);
24+
<# foreach(var (acceptor, result) in Acceptors) { #>
25+
<#= GetReturnType(IsGeneric ? BaseResult : result ?? BaseResult) #> Visit<#= IfAsync("Async") #>(<#= acceptor.ToDisplayString() #> value<#= GetTypeArgListStr() #>);
2626
<# } #>
2727
}
28-
<# if(!VisitorInterface.ContainingNamespace.IsGlobalNamespace) { PopIndent(); #>
28+
<# if (!VisitorInterface.ContainingNamespace.IsGlobalNamespace) { PopIndent(); #>
2929
}
3030
<# } #>
31-
<# foreach(var (acceptor, result) in AcceptorTypes) { #>
31+
<# foreach(var (acceptor, result) in Acceptors) { #>
3232

33-
<# if(!VisitorInterface.ContainingNamespace.IsGlobalNamespace) { #>
34-
namespace <#= acceptor.ContainingNamespace.ToDisplayString() #>
33+
<# if (!acceptor.ContainingNamespace.IsGlobalNamespace) { #>
34+
namespace <#= GetNamespace(acceptor) #>
3535
{
3636
<# PushIndent(" "); } #>
37-
partial class <#= acceptor.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat.WithGenericsOptions(SymbolDisplayGenericsOptions.IncludeTypeParameters | SymbolDisplayGenericsOptions.IncludeVariance)) #>: <#= AcceptorInterface.ToDisplayString() #><#= BaseResultType is null || IsGeneric ? "" : $", {GetBaseInterfaceName("IVisitorResult")}<{(result ?? BaseResultType).ToDisplayString()}>" #>
37+
partial class <#= GetTypeIdentifier(acceptor) #>: <#= Acceptor.ToDisplayString() #><#= BaseResult is null || IsGeneric ? "" : $", {GetBaseInterfaceName("IVisitorResult")}<{(result ?? BaseResult).ToDisplayString()}>" #>
3838
{
39-
<#= IfAsync("async ") #><#= GetReturnType(BaseResultType) #> <#= AcceptorInterface.ToDisplayString() #>.Accept<#= IfAsync("Async") #><#= GetTypeParamStr() #>(<#= VisitorInterface.ToDisplayString() #> visitor<#= GetTypeArgListStr() #>) => <#= BaseResultType is null || result is null || IsGeneric ? "" : $"({BaseResultType.ToDisplayString()})" #><#= IfAsync("await ") #>visitor.Visit<#= IfAsync("Async") #>(this<#= GetArgListStr() #>);
39+
<#= Acceptor.TypeKind == TypeKind.Interface ? "" : "public sealed override " #><#= IfAsync("async ") #><#= GetReturnType(BaseResult) #> <#= Acceptor.TypeKind == TypeKind.Interface ? $"{Acceptor.ToDisplayString()}." : "" #>Accept<#= IfAsync("Async") #><#= GetTypeParamStr() #>(<#= VisitorInterface.ToDisplayString() #> visitor<#= GetTypeArgListStr() #>) => <#= BaseResult is null || result is null || IsGeneric ? "" : $"({BaseResult.ToDisplayString()})" #><#= IfAsync("await ") #>visitor.Visit<#= IfAsync("Async") #>(this<#= GetArgListStr() #>);
4040
}
41-
<# if(!AcceptorInterface.ContainingNamespace.IsGlobalNamespace) { PopIndent(); #>
41+
<# if (!acceptor.ContainingNamespace.IsGlobalNamespace) { PopIndent(); #>
4242
}
4343
<# } #>
4444
<# } #>

0 commit comments

Comments
 (0)