Skip to content

Commit

Permalink
Merge pull request #4688 from microsoft/fix/go-package-name-casing
Browse files Browse the repository at this point in the history
Fix/go package name casing
  • Loading branch information
SilasKenneth authored May 21, 2024
2 parents 41131a8 + 45647c0 commit 8c953fb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Fix package casing normalizing for value passed using `--namespace-name` option when generating Go Client. [#4498](https://github.com/microsoft/kiota/issues/4498)
- Aligns naming of sliced OpenAPI description generated by `plugin add` should be named `<plugin-name>-openapi.json|yml` [#4595](https://github.com/microsoft/kiota/issues/4595)
- Fixed RPC server to respect the `KIOTA_CONFIG_PREVIEW` flag.
- Added missing nullable directives for CLI generation.
Expand Down
22 changes: 22 additions & 0 deletions src/Kiota.Builder/Refiners/GoRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
{
cancellationToken.ThrowIfCancellationRequested();
DeduplicateErrorMappings(generatedCode);
NormalizeNamespaceNames(generatedCode);
MoveRequestBuilderPropertiesToBaseType(generatedCode,
new CodeUsing
{
Expand Down Expand Up @@ -757,4 +758,25 @@ currentInnerClass.Parent is CodeClass currentParentClass &&
}
CrawlTree(currentElement, RenameInnerModelsToAppended);
}

private void NormalizeNamespaceNames(CodeElement currentElement)
{
if (currentElement is CodeNamespace codeNamespace)
{
var clientNamespace = _configuration.ClientNamespaceName;
var namespaceName = codeNamespace.Name;
if (namespaceName.StartsWith(clientNamespace, StringComparison.OrdinalIgnoreCase) && !namespaceName.Equals(clientNamespace, StringComparison.OrdinalIgnoreCase))
{
var secondPart = namespaceName[clientNamespace.Length..]; // The rest of the name after the clientNamespace
var withEmptyRemoved = string.Join('.', secondPart.Split('.', StringSplitOptions.RemoveEmptyEntries));
var normalizedString = string.IsNullOrEmpty(withEmptyRemoved) switch
{
true => string.Empty,
false => $".{withEmptyRemoved}"
};
codeNamespace.Name = $"{clientNamespace}{normalizedString.ToLowerInvariant()}";
}
}
CrawlTree(currentElement, NormalizeNamespaceNames);
}
}
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Writers/Go/GoNamespaceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public static string GetLastNamespaceSegment(this string nsName)
var urlPrefixIndex = nsName.LastIndexOf('/') + 1;
var tentativeSegment = nsName[urlPrefixIndex..].Split('.', StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
if (string.IsNullOrEmpty(tentativeSegment)) tentativeSegment = nsName.Split('/', StringSplitOptions.RemoveEmptyEntries).Last();
return tentativeSegment.ToLowerInvariant();
return tentativeSegment;
}
public static string GetInternalNamespaceImport(this CodeElement ns)
{
if (ns == null) return string.Empty;
var urlPrefixIndex = ns.Name.LastIndexOf('/') + 1;
return (ns.Name[..urlPrefixIndex] + string.Join("/", ns.Name[urlPrefixIndex..].Split('.', StringSplitOptions.RemoveEmptyEntries))).ToLowerInvariant();
return (ns.Name[..urlPrefixIndex] + string.Join("/", ns.Name[urlPrefixIndex..].Split('.', StringSplitOptions.RemoveEmptyEntries)));
}
public static string GetNamespaceImportSymbol(this CodeElement ns)
{
Expand Down
12 changes: 12 additions & 0 deletions tests/Kiota.Builder.Tests/Refiners/GoLanguageRefinerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Kiota.Builder.CodeDOM;
using Kiota.Builder.Configuration;
using Kiota.Builder.Refiners;
using Kiota.Builder.Writers.Go;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;
Expand Down Expand Up @@ -1181,5 +1182,16 @@ public async Task AddsUsingForUntypedNode()
Assert.Single(nodeUsing);
Assert.Equal("github.com/microsoft/kiota-abstractions-go/serialization", nodeUsing[0].Declaration.Name);
}

[Fact]
public async Task NormalizeNamespaceName()
{
root.Name = "github.com/OrgName/RepoName";
var models = root.AddNamespace("ApiSdk.models");
var submodels = models.AddNamespace("ApiSdk.models.submodels");
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.Go, ClientNamespaceName = "github.com/OrgName/RepoName" }, root);
Assert.Equal("github.com/OrgName/RepoName.apisdk.models.submodels", submodels.Name);
Assert.Equal("github.com/OrgName/RepoName.apisdk.models", models.Name);
}
#endregion
}

0 comments on commit 8c953fb

Please sign in to comment.