-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JsonRpcClassAttribute & RpcMethodNameTransformer (#5143)
Fixes #5140
- Loading branch information
1 parent
f3a8b01
commit 403220e
Showing
5 changed files
with
195 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/SLCore.UnitTests/Protocol/RpcMethodNameTransformerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* SonarLint for Visual Studio | ||
* Copyright (C) 2016-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using SonarLint.VisualStudio.SLCore.Core; | ||
using SonarLint.VisualStudio.SLCore.Protocol; | ||
using SonarLint.VisualStudio.TestInfrastructure; | ||
using StreamJsonRpc; | ||
|
||
namespace SonarLint.VisualStudio.SLCore.UnitTests.Protocol; | ||
|
||
[TestClass] | ||
public class RpcMethodNameTransformerTests | ||
{ | ||
private const string Prefix = "prefix"; | ||
|
||
[TestMethod] | ||
public void MefCtor_CheckIsExported() | ||
{ | ||
MefTestHelpers.CheckTypeCanBeImported<RpcMethodNameTransformer, IRpcMethodNameTransformer>(); | ||
} | ||
|
||
[TestMethod] | ||
public void Mef_CheckIsSingleton() | ||
{ | ||
MefTestHelpers.CheckIsSingletonMefComponent<RpcMethodNameTransformer>(); | ||
} | ||
|
||
[TestMethod] | ||
public void Create_NoAttribute_CreatesCamelCaseTransformer() | ||
{ | ||
var testSubject = CreateTestSubject(); | ||
|
||
var transformer = testSubject.Create<ITestSLCoreServiceNoAttribute>(); | ||
|
||
transformer.Should().BeSameAs(CommonMethodNameTransforms.CamelCase); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow("Method", $"{Prefix}/method")] | ||
[DataRow("MyMethod", $"{Prefix}/myMethod")] | ||
[DataRow("MyLovelyMethod", $"{Prefix}/myLovelyMethod")] | ||
[DataRow("myLovelyMethod", $"{Prefix}/myLovelyMethod")] | ||
[DataRow("mylovelymethod", $"{Prefix}/mylovelymethod")] | ||
public void Create_NoAttribute_CreatesCompositeTransformer(string methodName, string expectedMethodName) | ||
{ | ||
var testSubject = CreateTestSubject(); | ||
|
||
var transformer = testSubject.Create<ITestSLCoreServiceWithAttribute>(); | ||
|
||
transformer(methodName).Should().Be(expectedMethodName); | ||
} | ||
|
||
private RpcMethodNameTransformer CreateTestSubject() | ||
{ | ||
return new RpcMethodNameTransformer(); | ||
} | ||
|
||
public interface ITestSLCoreServiceNoAttribute : ISLCoreService {} | ||
|
||
[JsonRpcClass(Prefix)] | ||
public interface ITestSLCoreServiceWithAttribute : ISLCoreService {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* SonarLint for Visual Studio | ||
* Copyright (C) 2016-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System; | ||
|
||
namespace SonarLint.VisualStudio.SLCore.Protocol | ||
{ | ||
/// <summary> | ||
/// This attribute is used to tell <see cref="IRpcMethodNameTransformer"/> which prefix to use | ||
/// for this type's methods when registering them with the RPC. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class)] | ||
public class JsonRpcClassAttribute : Attribute | ||
{ | ||
public JsonRpcClassAttribute(string prefix) | ||
{ | ||
Prefix = prefix; | ||
} | ||
|
||
public string Prefix { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* SonarLint for Visual Studio | ||
* Copyright (C) 2016-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System; | ||
using System.ComponentModel.Composition; | ||
using System.Linq; | ||
using StreamJsonRpc; | ||
|
||
namespace SonarLint.VisualStudio.SLCore.Protocol | ||
{ | ||
/// <summary> | ||
/// Method name transformer for JsonRPC types. | ||
/// </summary> | ||
public interface IRpcMethodNameTransformer | ||
{ | ||
/// <summary> | ||
/// Creates method name transformer (<see cref="JsonRpcProxyOptions.MethodNameTransform"/>) for the type. | ||
/// </summary> | ||
/// <returns> | ||
/// <typeparam name="TRpcType">Type of the RPC entity</typeparam> | ||
/// If <typeparamref name="TRpcType"/> is marked with <see cref="JsonRpcClassAttribute"/>, returns a composition of | ||
/// <see cref="CommonMethodNameTransforms.Prepend"/> with parameter <see cref="JsonRpcClassAttribute.Prefix"/> and <see cref="CommonMethodNameTransforms.CamelCase"/>. | ||
/// If not marked, returns <see cref="CommonMethodNameTransforms.CamelCase"/> | ||
/// </returns> | ||
Func<string, string> Create<TRpcType>(); | ||
} | ||
|
||
[Export(typeof(IRpcMethodNameTransformer))] | ||
[PartCreationPolicy(CreationPolicy.Shared)] | ||
public class RpcMethodNameTransformer : IRpcMethodNameTransformer | ||
{ | ||
public Func<string, string> Create<T>() | ||
{ | ||
if (!(typeof(T).GetCustomAttributes(typeof(JsonRpcClassAttribute), false).FirstOrDefault() is JsonRpcClassAttribute attribute)) | ||
{ | ||
return CommonMethodNameTransforms.CamelCase; | ||
} | ||
|
||
var prependTransform = CommonMethodNameTransforms.Prepend($"{attribute.Prefix}/"); | ||
|
||
return name => prependTransform(CommonMethodNameTransforms.CamelCase(name)); | ||
} | ||
} | ||
} |