From 4444a879e096aec7c77171be6e396006b19193e2 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 9 May 2024 14:38:35 +0300 Subject: [PATCH 1/4] WIP: golang enum parsing --- .../Writers/Go/CodeEnumWriter.cs | 19 +++++++++---------- .../Writers/Go/CodeEnumWriterTests.cs | 4 ++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs b/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs index ecb596c681..422b727b5f 100644 --- a/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs +++ b/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs @@ -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(); @@ -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) {{"); @@ -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) { @@ -117,10 +117,9 @@ private void WriteParsableEnum(CodeEnum codeElement, LanguageWriter writer, Bool writer.DecreaseIndent(); } } - - + 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(); @@ -128,7 +127,7 @@ private void WriteParsableEnum(CodeEnum codeElement, LanguageWriter writer, Bool 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 {{"); @@ -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 {{"); diff --git a/tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs index 91b0c5514f..85a0453853 100644 --- a/tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs @@ -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); @@ -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); From eb4d211c411a9e3dde6ff718669299c691d49dbd Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 9 May 2024 14:55:27 +0300 Subject: [PATCH 2/4] Cleanup --- src/Kiota.Builder/Refiners/GoRefiner.cs | 12 ++---------- .../Refiners/GoLanguageRefinerTests.cs | 4 ++-- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Kiota.Builder/Refiners/GoRefiner.cs b/src/Kiota.Builder/Refiners/GoRefiner.cs index 7dafcd70f0..191497d776 100644 --- a/src/Kiota.Builder/Refiners/GoRefiner.cs +++ b/src/Kiota.Builder/Refiners/GoRefiner.cs @@ -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); } diff --git a/tests/Kiota.Builder.Tests/Refiners/GoLanguageRefinerTests.cs b/tests/Kiota.Builder.Tests/Refiners/GoLanguageRefinerTests.cs index 170c31dafc..f080ab861d 100644 --- a/tests/Kiota.Builder.Tests/Refiners/GoLanguageRefinerTests.cs +++ b/tests/Kiota.Builder.Tests/Refiners/GoLanguageRefinerTests.cs @@ -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] @@ -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] From 0f4db1622beb700cc6917227cfcc35704f99ca0d Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 9 May 2024 14:55:43 +0300 Subject: [PATCH 3/4] format --- src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs b/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs index 422b727b5f..ffc3426e82 100644 --- a/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs +++ b/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs @@ -117,7 +117,7 @@ private static void WriteParsableEnum(CodeEnum codeElement, LanguageWriter write writer.DecreaseIndent(); } } - + writer.StartBlock("default:"); writer.WriteLine($"return nil, nil"); writer.DecreaseIndent(); From 9efd22af6c8971e933e11b7ad91881b7d16c2fe2 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Mon, 13 May 2024 10:04:43 +0300 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f6d7bfe89..5e4a7e92c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ## [1.14.0] - 2024-05-02