From 8d6f606110577528dc9e8b969c138e92fc0e6052 Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Mon, 28 Aug 2023 15:14:37 +0300 Subject: [PATCH] Adds mask for go enums --- CHANGELOG.md | 1 + src/Kiota.Builder/Refiners/GoRefiner.cs | 10 ++++++--- .../Writers/Go/CodeEnumWriter.cs | 22 +++++++++++++++---- .../Writers/Go/CodeEnumWriterTests.cs | 4 +++- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f62425ee4..c4b8929364 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added support for external documentation links within descriptions in Python. [#2041](https://github.com/microsoft/kiota/issues/2041) - Added support for API manifests. [#3104](https://github.com/microsoft/kiota/issues/3104) - Added support for reserved path parameters. [#2320](https://github.com/microsoft/kiota/issues/2320) +- Added support for csv values in enums using a mask. ### Changed diff --git a/src/Kiota.Builder/Refiners/GoRefiner.cs b/src/Kiota.Builder/Refiners/GoRefiner.cs index 5bc5075059..cf0a299b52 100644 --- a/src/Kiota.Builder/Refiners/GoRefiner.cs +++ b/src/Kiota.Builder/Refiners/GoRefiner.cs @@ -119,7 +119,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance cancellationToken.ThrowIfCancellationRequested(); MakeModelPropertiesNullable( generatedCode); - AddErrorImportForEnums( + AddErrorAndStringsImportForEnums( generatedCode); var defaultConfiguration = new GenerationConfiguration(); ReplaceDefaultSerializationModules( @@ -472,7 +472,7 @@ private static void ReplaceRequestBuilderPropertiesByMethods(CodeElement current } CrawlTree(currentElement, ReplaceRequestBuilderPropertiesByMethods); } - private static void AddErrorImportForEnums(CodeElement currentElement) + private static void AddErrorAndStringsImportForEnums(CodeElement currentElement) { if (currentElement is CodeEnum currentEnum) { @@ -480,8 +480,12 @@ private static void AddErrorImportForEnums(CodeElement currentElement) { Name = "errors", }); + currentEnum.AddUsing(new CodeUsing + { + Name = "strings", + }); } - CrawlTree(currentElement, AddErrorImportForEnums); + CrawlTree(currentElement, AddErrorAndStringsImportForEnums); } private static readonly GoConventionService conventions = new(); private static readonly HashSet typeToSkipStrConv = new(StringComparer.OrdinalIgnoreCase) { diff --git a/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs b/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs index a6a80116a5..c972381acf 100644 --- a/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs +++ b/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs @@ -44,22 +44,35 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write string.Empty, $"func (i {typeName}) String() string {{"); writer.IncreaseIndent(); + writer.WriteLine($"var values []string"); var literalOptions = enumOptions .Select(x => $"\"{x.WireName}\"") .Aggregate((x, y) => x + ", " + y); - writer.WriteLine($"return []string{{{literalOptions}}}[i]"); + writer.WriteLine($"for p := {typeName}(1); p <= {enumOptions.Last()}; p <<= 1 {{"); + writer.IncreaseIndent(); + writer.WriteLine($"if i&p == p {{"); + writer.IncreaseIndent(); + writer.WriteLine($"values = append(values, []string{{{literalOptions}}}[p])"); + writer.DecreaseIndent(); + writer.WriteLine($"}}"); + writer.DecreaseIndent(); + writer.WriteLine($"}}"); + writer.WriteLine($"return strings.Join(values, \",\")"); writer.DecreaseIndent(); writer.WriteLines("}", $"func Parse{typeName}(v string) (any, error) {{"); writer.IncreaseIndent(); - writer.WriteLine($"result := {enumOptions.First().Name.ToUpperInvariant()}_{typeName.ToUpperInvariant()}"); - writer.WriteLine("switch v {"); + writer.WriteLine($"var result {typeName}"); + writer.WriteLine("values := strings.Split(v, \",\")"); + writer.WriteLine("for _, str := range values {"); + writer.IncreaseIndent(); + writer.WriteLine("switch str {"); writer.IncreaseIndent(); foreach (var item in enumOptions) { writer.WriteLine($"case \"{item.WireName}\":"); writer.IncreaseIndent(); - writer.WriteLine($"result = {item.Name.ToUpperInvariant()}_{typeName.ToUpperInvariant()}"); + writer.WriteLine($"result |= {item.Name.ToUpperInvariant()}_{typeName.ToUpperInvariant()}"); writer.DecreaseIndent(); } writer.WriteLine("default:"); @@ -67,6 +80,7 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write writer.WriteLine($"return 0, errors.New(\"Unknown {typeName} value: \" + v)"); writer.DecreaseIndent(); writer.CloseBlock(); + writer.CloseBlock(); writer.WriteLine("return &result, nil"); writer.CloseBlock(); writer.WriteLine($"func Serialize{typeName}(values []{typeName}) []string {{"); diff --git a/tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs index bd1b44348a..0ababd8c61 100644 --- a/tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs @@ -45,7 +45,9 @@ public void WritesEnum() Assert.Contains($"{EnumName.ToFirstCharacterUpperCase()} = iota", result); Assert.Contains("func (i", result); Assert.Contains("String() string {", result); - Assert.Contains("return []string{", result); + Assert.Contains("values := strings.Split(v, \",\"))", result); + Assert.Contains("strings.Join(values", result); + Assert.Contains("result |= OPTION1_SOMEENUM", result); Assert.Contains("[i]", result); Assert.Contains("func Parse", result); Assert.Contains("(v string) (any, error)", result);