From 46d2907a81ac780dade76cde38f5c533c6d0b92c Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Mon, 13 May 2024 09:15:50 -0400 Subject: [PATCH] Refactor Go code to improve import handling in CodeEnumWriter Signed-off-by: Vincent Biret --- src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs | 12 ++++++++---- .../Writers/Go/CodeEnumWriterTests.cs | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs b/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs index ffc3426e82..2326c74196 100644 --- a/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs +++ b/src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs @@ -16,10 +16,14 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write if (codeElement.Parent is CodeNamespace ns) writer.WriteLine($"package {ns.Name.GetLastNamespaceSegment().Replace("-", string.Empty, StringComparison.OrdinalIgnoreCase)}"); - writer.StartBlock("import ("); - foreach (var cUsing in codeElement.Usings.OrderBy(static x => x.Name, StringComparer.OrdinalIgnoreCase)) - writer.WriteLine($"\"{cUsing.Name}\""); - writer.CloseBlock(")"); + var usings = codeElement.Usings.OrderBy(static x => x.Name, StringComparer.OrdinalIgnoreCase).ToArray(); + if (usings.Length > 0) + { + writer.StartBlock("import ("); + foreach (var cUsing in usings) + writer.WriteLine($"\"{cUsing.Name}\""); + writer.CloseBlock(")"); + } var typeName = codeElement.Name.ToFirstCharacterUpperCase(); conventions.WriteShortDescription(codeElement, writer); diff --git a/tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs index 85a0453853..39f8ce54af 100644 --- a/tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs @@ -138,4 +138,21 @@ public void WritesEnumOptionDescription() Assert.Contains($"// {option.Documentation.DescriptionTemplate}", result); AssertExtensions.CurlyBracesAreClosed(result); } + [Fact] + public void DoesNotWriteImportOnEmptyImports() + { + var option = new CodeEnumOption + { + Documentation = new() + { + DescriptionTemplate = "Some option description", + }, + Name = "option1", + }; + currentEnum.AddOption(option); + writer.Write(currentEnum); + var result = tw.ToString(); + Assert.DoesNotContain("import", result); + AssertExtensions.CurlyBracesAreClosed(result); + } }