Skip to content

Commit

Permalink
Updates go fluent path api (#1679)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkodev authored Jul 24, 2023
1 parent 803eaf7 commit 62aec68
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
11 changes: 10 additions & 1 deletion CodeSnippetsReflection.OpenAPI.Test/GoGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using CodeSnippetsReflection.OpenAPI.LanguageGenerators;
using Xunit;

Expand Down Expand Up @@ -49,7 +50,7 @@ public async Task GeneratesTheCorrectFluentAPIPathForIndexedCollectionsWithMulti
};
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("Drives().ByDriveId(\"drive-id\").Items().ByItemId(\"driveItem-id\").Checkin().", result);
Assert.Contains("Drives().ByDriveId(\"drive-id\").Items().ByDriveItemId(\"driveItem-id\").Checkin().Post(context.Background(), requestBody, nil)", result);
}
[Fact]
public async Task IgnoreOdataTypeWhenGenerating() {
Expand Down Expand Up @@ -522,6 +523,14 @@ public async Task GenerateFindMeetingTime()
Assert.Contains("minimumAttendeePercentage := float64(200)", result);
Assert.Contains("isOrganizerOptional := false", result);
}
[Fact]
public async Task GeneratesPathSegmentsUsingVariableName()
{
using var requestPayload = new HttpRequestMessage(HttpMethod.Get, $"{ServiceRootUrl}/me/drive/items/{{id}}/workbook/worksheets/{{id|name}}/charts");
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains(".Drives().ByDriveId(\"drive-id\").Items().ByDriveItemId(\"driveItem-id\").Workbook().Worksheets().ByWorkbookWorksheetId(\"workbookWorksheet-id\").Charts().Get(context.Background(), nil)", result);
}
//TODO test for DateTimeOffset
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,10 @@ private static string GetFluentApiPath(IEnumerable<OpenApiUrlTreeNode> nodes)
var elements = nodes.Select(static (x, i) =>
{
if (x.Segment.IsCollectionIndex())
return $"ByTypeId{x.Segment.Replace("{", "(\"").Replace("}", "\")")}.";
{
var pathName = string.IsNullOrEmpty(x.Segment) ? x.Segment : x.Segment.ReplaceMultiple("", "{", "}").Split('-').Where(static s => !string.IsNullOrEmpty(s)).Select(static s => s.ToFirstCharacterUpperCase()).Aggregate(static (a, b) => $"By{a}{b}");
return $"{pathName ?? "ByTypeId"}{x.Segment.Replace("{", "(\"", StringComparison.OrdinalIgnoreCase).Replace("}", "\")", StringComparison.OrdinalIgnoreCase)}.";
}
else if (x.Segment.IsFunction())
return x.Segment.Split('.')
.Select(static s => s.ToFirstCharacterUpperCase())
Expand Down
14 changes: 13 additions & 1 deletion CodeSnippetsReflection/StringExtensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text;
using System.Globalization;
using System.Linq;
using System;

namespace CodeSnippetsReflection.StringExtensions
{
Expand Down Expand Up @@ -31,6 +31,18 @@ public static string ToFirstCharacterUpperCase(this string stringValue) {
return char.ToUpper(stringValue[0]) + stringValue[1..];
}

public static string ReplaceMultiple(this string stringValue, string haystack, params string []needles)
{
if (string.IsNullOrEmpty(stringValue)) return stringValue;
if (needles == null || needles.Length == 0) return stringValue;
foreach (var needle in needles)
{
if (string.IsNullOrEmpty(needle) || !stringValue.Contains(needle, StringComparison.InvariantCulture)) continue;
stringValue = stringValue.Replace(needle, haystack, StringComparison.OrdinalIgnoreCase);
}
return stringValue;
}

public static string ToFirstCharacterUpperCaseAfterCharacter(this string stringValue, char character)
{
if (string.IsNullOrEmpty(stringValue)) return stringValue;
Expand Down

0 comments on commit 62aec68

Please sign in to comment.