Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Go code to improve import handling in CodeEnumWriter #4651

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading