Skip to content

Commit

Permalink
Merge pull request #4646 from microsoft/andrueastman/enumParsin
Browse files Browse the repository at this point in the history
[Go] Return nil in the default case for enum parsing methods.
  • Loading branch information
andrueastman authored May 13, 2024
2 parents e445e0b + d4c5885 commit b375476
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 23 deletions.
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 @@ -525,20 +525,12 @@ private static void ReplaceRequestBuilderPropertiesByMethods(CodeElement current
}
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 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
4 changes: 2 additions & 2 deletions tests/Kiota.Builder.Tests/Refiners/GoLanguageRefinerTests.cs
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

0 comments on commit b375476

Please sign in to comment.