Skip to content

Commit

Permalink
Merge pull request #4651 from microsoft/bugfix/enums-import
Browse files Browse the repository at this point in the history
Refactor Go code to improve import handling in CodeEnumWriter
  • Loading branch information
baywet authored May 13, 2024
2 parents b375476 + 46d2907 commit 7a66465
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/Go/CodeEnumWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 7a66465

Please sign in to comment.