From f5b32f84ea6af403ac85057d33b5553dcd80ebed Mon Sep 17 00:00:00 2001 From: James Courtney Date: Wed, 25 Jan 2023 11:39:58 -0800 Subject: [PATCH] Add C# 8 Build Step (#362) Validate C#8 compatibility --- .github/workflows/charp8build.yml | 55 +++++++++++++++++++ .../SchemaModel/RpcServiceSchemaModel.cs | 11 +++- src/Tests/CompileTests/CSharp8/CSharp8.csproj | 24 ++++++++ 3 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/charp8build.yml create mode 100644 src/Tests/CompileTests/CSharp8/CSharp8.csproj diff --git a/.github/workflows/charp8build.yml b/.github/workflows/charp8build.yml new file mode 100644 index 00000000..35073a1f --- /dev/null +++ b/.github/workflows/charp8build.yml @@ -0,0 +1,55 @@ +name: C# 8 Build Verification + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + workflow_dispatch: + +jobs: + build: + runs-on: windows-2019 + env: + AppVeyorBuild: true + steps: + - uses: actions/checkout@v2 + + - name: Setup .NET 7 + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 7.0.x + + - name: Setup .NET 6 + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 6.0.x + + - name: Restore dependencies + working-directory: src + run: dotnet restore + + - name: Build FlatSharp.Compiler + working-directory: src/FlatSharp.Compiler + run: dotnet build -c Release + + - name: Run FlatSharp.Compiler + # You may pin to the exact commit or the version. + # uses: Amadevus/pwsh-script@97a8b211a5922816aa8a69ced41fa32f23477186 + uses: Amadevus/pwsh-script@v2.0.3 + with: + # PowerShell script to execute in Actions-hydrated context + script: | + $fbs = (gci -r src/tests/FlatsharpEndToEndTests/*.fbs | where Name -ne "AccessModifiers.fbs") -join ";" + dotnet src/FlatSharp.Compiler/bin/Release/net7.0/FlatSharp.Compiler.dll --nullable-warnings false --normalize-field-names true --input "$fbs" -o src/tests/CompileTests/CSharp8 + + - name: Build + working-directory: src/tests/CompileTests/CSharp8 + run: dotnet build -c Release + + - name: Upload Files + uses: actions/upload-artifact@v3 + if: failure() + with: + name: generated-csharp + path: src/tests/CompileTests/CSharp8/**/*.* diff --git a/src/FlatSharp.Compiler/SchemaModel/RpcServiceSchemaModel.cs b/src/FlatSharp.Compiler/SchemaModel/RpcServiceSchemaModel.cs index 8f8e6068..917b2232 100644 --- a/src/FlatSharp.Compiler/SchemaModel/RpcServiceSchemaModel.cs +++ b/src/FlatSharp.Compiler/SchemaModel/RpcServiceSchemaModel.cs @@ -217,7 +217,7 @@ private void DefineServerBaseClass(CodeWriter writer, Dictionary { foreach (var method in this.calls) { - writer.AppendLine($".AddMethod({methodNameMap[method.Name]}, serviceImpl == null ? null : serviceImpl.{method.Name})"); + writer.AppendLine($".AddMethod({methodNameMap[method.Name]}, serviceImpl == null ? ({this.GetServerHandlerDelegateType(method)}?)null : serviceImpl.{method.Name})"); } writer.AppendLine(".Build();"); @@ -234,7 +234,7 @@ private void DefineServerBaseClass(CodeWriter writer, Dictionary foreach (var method in this.calls) { string serverDelegate = GetServerHandlerDelegate(method); - writer.AppendLine($"serviceBinder.AddMethod({methodNameMap[method.Name]}, serviceImpl == null ? null : {serverDelegate});"); + writer.AppendLine($"serviceBinder.AddMethod({methodNameMap[method.Name]}, serviceImpl == null ? ({this.GetServerHandlerDelegateType(method)}?)null : {serverDelegate});"); } writer.AppendLine("#pragma warning restore CS8604"); } @@ -568,8 +568,13 @@ private void WriteNoRequestParameterMethod( } private string GetServerHandlerDelegate(RpcCallSchemaModel call) + { + return $"new {this.GetServerHandlerDelegateType(call)}(serviceImpl.{call.Name})"; + } + + private string GetServerHandlerDelegateType(RpcCallSchemaModel call) { string methodType = GetGrpcMethodType(call.StreamingType); - return $"new {GrpcCore}.{methodType}ServerMethod<{call.RequestType}, {call.ResponseType}>(serviceImpl.{call.Name})"; + return $"{GrpcCore}.{methodType}ServerMethod<{call.RequestType}, {call.ResponseType}>"; } } diff --git a/src/Tests/CompileTests/CSharp8/CSharp8.csproj b/src/Tests/CompileTests/CSharp8/CSharp8.csproj new file mode 100644 index 00000000..34587b25 --- /dev/null +++ b/src/Tests/CompileTests/CSharp8/CSharp8.csproj @@ -0,0 +1,24 @@ + + + net472;netstandard2.0;netstandard2.1 + false + CSharp8CompileTest + CSharp8CompileTest + true + annotations + CS1591 + 8.0 + + + + + + + + + + + + + +