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

[Go] Return nil in the default case for enum parsing methods. #4646

Merged
merged 6 commits into from
May 13, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed `InvalidOperationException` thrown when serializing IBacked models with no changes present in the additional data in dotnet [microsoftgraph/msgraph-sdk-dotnet#2471](https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/2471).
- Fixed `RequestConfiguration` Classes dropped in RequestBuilder methods in python [#4535](https://github.com/microsoft/kiota/issues/4535)
- Fixed incorrect optional types in method parameters in Python [#4507](https://github.com/microsoft/kiota/issues/4507)
- Changed enum parsing methods to return nil in the default case in Go [#4621](https://github.com/microsoft/kiota/issues/4621)
- Changes the cached description file name to `openapi.yml|json` from `description.yml|json` [#4641](https://github.com/microsoft/kiota/issues/4641)

## [1.14.0] - 2024-05-02
Expand Down
12 changes: 2 additions & 10 deletions src/Kiota.Builder/Refiners/GoRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
string.Empty,
false,
MergeOverLappedStrings);
if (_configuration.ExcludeBackwardCompatible) //TODO remove condition for v2

Check warning on line 48 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 48 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 +182,7 @@
);
AddParsableImplementsForModelClasses(
generatedCode,
"Parsable"

Check warning on line 185 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 185 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 +239,7 @@
return $"{start}{end}";
}

private void CorrectBackingStoreTypes(CodeElement currentElement)

Check warning on line 242 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)

Check warning on line 242 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 +380,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 383 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 @@ -525,20 +525,12 @@
}
private static void AddErrorAndStringsImportForEnums(CodeElement currentElement)
{
if (currentElement is CodeEnum currentEnum)
if (currentElement is CodeEnum { Flags: true } currentEnum)
{
currentEnum.AddUsing(new CodeUsing
{
Name = "errors",
Name = "strings",
});

if (currentEnum.Flags)
{
currentEnum.AddUsing(new CodeUsing
{
Name = "strings",
});
}
}
CrawlTree(currentElement, AddErrorAndStringsImportForEnums);
}
Expand All @@ -552,7 +544,7 @@
"UUID",
"Guid"
};
private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {

Check warning on line 547 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 547 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 @@ -610,7 +602,7 @@
block.ReplaceImplementByName(KiotaBuilder.AdditionalHolderInterface, "AdditionalDataHolder");
block.ReplaceImplementByName(KiotaBuilder.BackedModelInterface, "BackedModel");
}
private static void CorrectMethodType(CodeMethod currentMethod)

Check warning on line 605 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
17 changes: 8 additions & 9 deletions src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write

WriteStringFunction(codeElement, writer, isMultiValue);
WriteParsableEnum(codeElement, writer, isMultiValue);
WriteSerializeFunction(codeElement, writer, isMultiValue);
WriteSerializeFunction(codeElement, writer);
WriteMultiValueFunction(codeElement, writer, isMultiValue);
}

private void WriteStringFunction(CodeEnum codeElement, LanguageWriter writer, bool isMultiValue)
private static void WriteStringFunction(CodeEnum codeElement, LanguageWriter writer, bool isMultiValue)
{
var typeName = codeElement.Name.ToFirstCharacterUpperCase();
var enumOptions = codeElement.Options.ToList();
Expand Down Expand Up @@ -86,10 +86,10 @@ private void WriteStringFunction(CodeEnum codeElement, LanguageWriter writer, bo
}
}

private void WriteParsableEnum(CodeEnum codeElement, LanguageWriter writer, Boolean isMultiValue)
private static void WriteParsableEnum(CodeEnum codeElement, LanguageWriter writer, Boolean isMultiValue)
{
var typeName = codeElement.Name.ToFirstCharacterUpperCase();
var enumOptions = codeElement.Options;
var enumOptions = codeElement.Options.ToArray();

writer.StartBlock($"func Parse{typeName}(v string) (any, error) {{");

Expand All @@ -108,7 +108,7 @@ private void WriteParsableEnum(CodeEnum codeElement, LanguageWriter writer, Bool
}
else
{
writer.WriteLine($"result := {enumOptions.First().Name.ToUpperInvariant()}_{typeName.ToUpperInvariant()}");
writer.WriteLine($"result := {enumOptions[0].Name.ToUpperInvariant()}_{typeName.ToUpperInvariant()}");
writer.StartBlock("switch v {");
foreach (var item in enumOptions)
{
Expand All @@ -118,17 +118,16 @@ private void WriteParsableEnum(CodeEnum codeElement, LanguageWriter writer, Bool
}
}


writer.StartBlock("default:");
writer.WriteLine($"return 0, errors.New(\"Unknown {typeName} value: \" + v)");
writer.WriteLine($"return nil, nil");
writer.DecreaseIndent();
writer.CloseBlock();
if (isMultiValue) writer.CloseBlock();
writer.WriteLine("return &result, nil");
writer.CloseBlock();
}

private void WriteSerializeFunction(CodeEnum codeElement, LanguageWriter writer, Boolean isMultiValue)
private static void WriteSerializeFunction(CodeEnum codeElement, LanguageWriter writer)
{
var typeName = codeElement.Name.ToFirstCharacterUpperCase();
writer.StartBlock($"func Serialize{typeName}(values []{typeName}) []string {{");
Expand All @@ -141,7 +140,7 @@ private void WriteSerializeFunction(CodeEnum codeElement, LanguageWriter writer,
writer.CloseBlock();
}

private void WriteMultiValueFunction(CodeEnum codeElement, LanguageWriter writer, Boolean isMultiValue)
private static void WriteMultiValueFunction(CodeEnum codeElement, LanguageWriter writer, Boolean isMultiValue)
{
var typeName = codeElement.Name.ToFirstCharacterUpperCase();
writer.StartBlock($"func (i {typeName}) isMultiValue() bool {{");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ public async Task AddsErrorImportForEnumsForMultiValueEnum()
Flags = true
}).First();
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.Go }, root);
Assert.Single(testEnum.Usings.Where(static x => "errors".Equals(x.Name, StringComparison.Ordinal)));
Assert.Empty(testEnum.Usings.Where(static x => "errors".Equals(x.Name, StringComparison.Ordinal)));
Assert.Single(testEnum.Usings.Where(static x => "strings".Equals(x.Name, StringComparison.Ordinal)));
}
[Fact]
Expand All @@ -923,7 +923,7 @@ public async Task AddsErrorImportForEnumsForSingleValueEnum()
Flags = false
}).First();
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.Go }, root);
Assert.Single(testEnum.Usings.Where(static x => "errors".Equals(x.Name, StringComparison.Ordinal)));
Assert.Empty(testEnum.Usings.Where(static x => "errors".Equals(x.Name, StringComparison.Ordinal)));
Assert.Empty(testEnum.Usings.Where(static x => "strings".Equals(x.Name, StringComparison.Ordinal)));
}
[Fact]
Expand Down
4 changes: 2 additions & 2 deletions tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void WritesSingleValueEnum()
Assert.Contains("default", result);
Assert.Contains("result :=", result);
Assert.Contains("return &result, nil", result);
Assert.Contains("return 0, errors.New(\"Unknown ", result);
Assert.Contains("return nil, nil", result);
AssertExtensions.CurlyBracesAreClosed(result);
Assert.Contains(optionName.ToUpperInvariant(), result);
Assert.Contains("func (i SomeEnum) isMultiValue() bool {", result);
Expand Down Expand Up @@ -96,7 +96,7 @@ public void WritesMultiValueEnum()
Assert.Contains("default", result);
Assert.Contains("result :=", result);
Assert.Contains("return &result, nil", result);
Assert.Contains("return 0, errors.New(\"Unknown ", result);
Assert.Contains("return nil, nil", result);
Assert.Contains("func (i MultiValueEnum) isMultiValue() bool {", result);
Assert.Contains("return true", result);
AssertExtensions.CurlyBracesAreClosed(result);
Expand Down
Loading