diff --git a/CodeSnippetsReflection.OpenAPI.Test/GoGeneratorTests.cs b/CodeSnippetsReflection.OpenAPI.Test/GoGeneratorTests.cs index c4180bdda..576678d35 100644 --- a/CodeSnippetsReflection.OpenAPI.Test/GoGeneratorTests.cs +++ b/CodeSnippetsReflection.OpenAPI.Test/GoGeneratorTests.cs @@ -433,6 +433,20 @@ public async Task WritesEmptyPrimitiveArrays() { Assert.Contains("SetIncludeUserActions", result); } [Fact] + public async Task WriteCorrectFunctionNameWithParametersAsModelName() + { + const string messageObject = "{\r\n\"displayName\": \"Display name\"\r\n}"; + using var requestPayload = new HttpRequestMessage(HttpMethod.Patch, $"{ServiceRootUrl}/applications(uniqueName='app-65278')") + { + Content = new StringContent(messageObject, Encoding.UTF8, "application/json") + }; + requestPayload.Headers.Add("Prefer", "create-if-missing"); + var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetBetaSnippetMetadata()); + var result = _generator.GenerateCodeSnippet(snippetModel); + Assert.Contains("graphapplicationswithuniquename \"github.com/microsoftgraph/msgraph-sdk-go/applicationswithuniquename\"", result); + Assert.Contains("graphapplicationswithuniquename.ApplicationsWithUniqueNameRequestBuilderPatchRequestConfiguration", result); + } + [Fact] public async Task WriteCorrectTypesForFilterParameters() { using var requestPayload = new HttpRequestMessage(HttpMethod.Get, diff --git a/CodeSnippetsReflection.OpenAPI/LanguageGenerators/GoGenerator.cs b/CodeSnippetsReflection.OpenAPI/LanguageGenerators/GoGenerator.cs index e5d8958e0..59428b031 100644 --- a/CodeSnippetsReflection.OpenAPI/LanguageGenerators/GoGenerator.cs +++ b/CodeSnippetsReflection.OpenAPI/LanguageGenerators/GoGenerator.cs @@ -1,21 +1,19 @@ using System; +using System.Collections; using System.Collections.Generic; -using System.Linq; using System.Collections.Immutable; +using System.Linq; using System.Text; +using System.Text.RegularExpressions; using CodeSnippetsReflection.OpenAPI.ModelGraph; using CodeSnippetsReflection.StringExtensions; using Microsoft.OpenApi.Services; -using System.Text.RegularExpressions; -using System.Collections; namespace CodeSnippetsReflection.OpenAPI.LanguageGenerators { public class GoGenerator : ILanguageGenerator { private const string clientVarName = "graphClient"; - private const string clientVarType = "GraphServiceClientWithCredentials"; - private const string clientFactoryVariables = "cred, scopes"; private const string requestBodyVarName = "requestBody"; private const string requestHeadersVarName = "headers"; private const string optionsParameterVarName = "options"; @@ -29,6 +27,10 @@ public class GoGenerator : ILanguageGenerator private static readonly Regex PropertyNameRegex = new Regex(@"@(.*)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)); + private static readonly Regex FunctionRegex = new Regex(@"(\w+)\(([^)]*)\)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)); + + private static readonly Regex ParamRegex = new Regex(@"(\w+)\s*=\s*'[^']*'", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)); + static IImmutableSet GetNativeTypes() { return ImmutableHashSet.Create("string", "int", "float"); @@ -142,9 +144,24 @@ private static void TraverseProperty(CodeProperty property, Action private static String ProcessNameSpaceName(String nameSpace) { - return (nameSpace != null ? nameSpace.Split(".", StringSplitOptions.RemoveEmptyEntries) + if (String.IsNullOrEmpty(nameSpace)) + return ""; + + // process function names and parameters + var functionNameMatch = FunctionRegex.Match(nameSpace); + if (functionNameMatch.Success) + { + var paramMatches = ParamRegex.Matches(functionNameMatch.Groups[2].Value); + var paramNames = paramMatches.Cast().Select(static m => m.Groups[1].Value).ToList(); + + return functionNameMatch.Groups[1].Value + "With" + string.Join("With", paramNames); + } + + var processedName = (nameSpace.Split(".", StringSplitOptions.RemoveEmptyEntries) .Select(x => x.Equals("Me", StringComparison.OrdinalIgnoreCase) ? "Users" : x) - .Aggregate((current, next) => current + "." + next) : "models").Replace(".microsoft.graph", ""); + .Aggregate(static (current, next) => current + "." + next)).Replace(".microsoft.graph", ""); + + return processedName; } private static String ProcessFinalNameSpaceName(String nameSpace) @@ -306,7 +323,7 @@ private static string GetNestedObjectName(IEnumerable nodes) if (x.Segment.IsCollectionIndex()) return "Item"; else - return x.Segment.ToFirstCharacterUpperCase(); + return EscapeFunctionNames(x.Segment.ToFirstCharacterUpperCase()); }) .Aggregate(static (x, y) => { @@ -316,6 +333,22 @@ private static string GetNestedObjectName(IEnumerable nodes) }); } + private static string EscapeFunctionNames(String objectName) + { + if (String.IsNullOrEmpty(objectName)) + return objectName; + + var match = FunctionRegex.Match(objectName); + if (match.Success) + { + var paramMatches = ParamRegex.Matches(match.Groups[2].Value); + var paramNames = paramMatches.Cast().Select(static m => m.Groups[1].Value.ToFirstCharacterUpperCase()).ToList(); + + return match.Groups[1].Value + "With" + string.Join("With", paramNames); + } + return objectName; + } + private static string evaluateParameter(CodeProperty param) { if (param.PropertyType == PropertyType.Array)