Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates go fluent path api #1679

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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