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

Fix/go package name casing #4688

Merged
merged 10 commits into from
May 21, 2024
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 @@
{
cancellationToken.ThrowIfCancellationRequested();
DeduplicateErrorMappings(generatedCode);
NormalizeNamespaceNames(generatedCode);
MoveRequestBuilderPropertiesToBaseType(generatedCode,
new CodeUsing
{
Expand All @@ -45,7 +46,7 @@
string.Empty,
false,
MergeOverLappedStrings);
if (_configuration.ExcludeBackwardCompatible) //TODO remove condition for v2

Check warning on line 49 in src/Kiota.Builder/Refiners/GoRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)

Check warning on line 49 in src/Kiota.Builder/Refiners/GoRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
RemoveRequestConfigurationClasses(generatedCode,
new CodeUsing
{
Expand Down Expand Up @@ -182,7 +183,7 @@
);
AddParsableImplementsForModelClasses(
generatedCode,
"Parsable"

Check warning on line 186 in src/Kiota.Builder/Refiners/GoRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'Parsable' 5 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)

Check warning on line 186 in src/Kiota.Builder/Refiners/GoRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'Parsable' 5 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
);
RenameInnerModelsToAppended(
generatedCode
Expand Down Expand Up @@ -239,7 +240,7 @@
return $"{start}{end}";
}

private void CorrectBackingStoreTypes(CodeElement currentElement)

Check warning on line 243 in src/Kiota.Builder/Refiners/GoRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
if (!_configuration.UsesBackingStore)
return;
Expand Down Expand Up @@ -380,7 +381,7 @@
.ToList();

// check if the last element contains current name and remove it
if (namespacePathSegments.Count > 0 && removeDuplicate && fileName.ToFirstCharacterUpperCase().Contains(namespacePathSegments.Last(), StringComparison.Ordinal))

Check warning on line 384 in src/Kiota.Builder/Refiners/GoRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Indexing at Count-1 should be used instead of the "Enumerable" extension method "Last" (https://rules.sonarsource.com/csharp/RSPEC-6608)
namespacePathSegments.RemoveAt(namespacePathSegments.Count - 1);

namespacePathSegments.Add(fileName.ToFirstCharacterUpperCase());
Expand Down Expand Up @@ -544,7 +545,7 @@
"UUID",
"Guid"
};
private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {

Check warning on line 548 in src/Kiota.Builder/Refiners/GoRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this field to reduce its Cognitive Complexity from 24 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)

Check warning on line 548 in src/Kiota.Builder/Refiners/GoRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this field to reduce its Cognitive Complexity from 24 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.RequestAdapter),
AbstractionsNamespaceName, "RequestAdapter"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator),
Expand Down Expand Up @@ -602,7 +603,7 @@
block.ReplaceImplementByName(KiotaBuilder.AdditionalHolderInterface, "AdditionalDataHolder");
block.ReplaceImplementByName(KiotaBuilder.BackedModelInterface, "BackedModel");
}
private static void CorrectMethodType(CodeMethod currentMethod)

Check warning on line 606 in src/Kiota.Builder/Refiners/GoRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 21 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
var parentClass = currentMethod.Parent as CodeClass;
if (currentMethod.IsOfKind(CodeMethodKind.RequestGenerator, CodeMethodKind.RequestExecutor))
Expand Down Expand Up @@ -757,4 +758,25 @@
}
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 @@ -12,14 +12,14 @@
if (string.IsNullOrEmpty(nsName)) return string.Empty;
var urlPrefixIndex = nsName.LastIndexOf('/') + 1;
var tentativeSegment = nsName[urlPrefixIndex..].Split('.', StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
if (string.IsNullOrEmpty(tentativeSegment)) tentativeSegment = nsName.Split('/', StringSplitOptions.RemoveEmptyEntries).Last();

Check warning on line 15 in src/Kiota.Builder/Writers/Go/GoNamespaceExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

Indexing at Count-1 should be used instead of the "Enumerable" extension method "Last" (https://rules.sonarsource.com/csharp/RSPEC-6608)
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
Loading