Skip to content

Format generated Go code to adhere to golang coding standards#6416

Open
firefart wants to merge 63 commits into
microsoft:mainfrom
firefart:fmt
Open

Format generated Go code to adhere to golang coding standards#6416
firefart wants to merge 63 commits into
microsoft:mainfrom
firefart:fmt

Conversation

@firefart

@firefart firefart commented Apr 6, 2025

Copy link
Copy Markdown
Contributor

Golang expects generated code to be properly go fmt'ed (see golang/go#73181 (comment)).

This PR tries to implement the changes so the code will be compatible and go fmt will not change anything in the generated code.

Changes:

  • indent using tabs instead of spaces
  • when there is only one return argument, omit the parentheses
  • removes trailing spaces on comments without a content
  • fix import ordering
  • correctly indent case statements inside a switch

Test:
To test we will create a test client. Next go to the golangtest directory, init a git repo, and run go fmt to see the differences

rm -rf ./golangtest/ && cp -r ./it/go golangtest && dotnet build && ./src/kiota/bin/Debug/net10.0/kiota generate --openapi https://aka.ms/graph/v1.0/openapi.yaml --output ./golangtest/client --language go --exclude-backward-compatible --clean-output -i '/groups/getByIds#POST' -n 'integrationtest/client'
cd golangtest
git init
git add .
go fmt ./...
git diff

Ideally the git diff will show no changes

@baywet baywet linked an issue Apr 7, 2025 that may be closed by this pull request

@baywet baywet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

Comment thread src/Kiota.Builder/Writers/Go/GoWriter.cs Outdated
Comment thread src/Kiota.Builder/Writers/Go/GoConventionService.cs Outdated
@firefart firefart requested a review from baywet April 7, 2025 14:36
@firefart firefart changed the title Format Go code to adhere to golang coding standards Format generated Go code to adhere to golang coding standards Apr 7, 2025
Copilot AI review requested due to automatic review settings June 28, 2026 10:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Go code generator to better match gofmt expectations (tabs for indentation, Go-style return type formatting, cleaner comments, import ordering, and switch/case indentation) so go fmt produces no diffs on generated output.

Changes:

  • Switch Go generation indentation to tabs (via LanguageWriter + GoWriter).
  • Adjust Go signatures/returns/comment formatting to align with gofmt conventions.
  • Update Go writer logic and tests around imports, switch/case indentation, and return type formatting.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs Updates assertions to match new Go formatting expectations.
src/Kiota.Builder/Writers/LanguageWriter.cs Adds configurable indentation character/size to support tabs for Go.
src/Kiota.Builder/Writers/Go/GoWriter.cs Configures Go writer to use tab indentation.
src/Kiota.Builder/Writers/Go/GoNamespaceExtensions.cs Simplifies namespace import string concatenation.
src/Kiota.Builder/Writers/Go/GoConventionService.cs Adjusts doc comment prefix handling to avoid trailing spaces.
src/Kiota.Builder/Writers/Go/CodeProprietableBlockDeclarationWriter.cs Changes import ordering/format in type declarations.
src/Kiota.Builder/Writers/Go/CodePropertyWriter.cs Avoids emitting trailing spaces for typeless/embedded fields.
src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs Refactors Go method prototype emission, removes semicolons, adjusts spacing/blocks.
src/Kiota.Builder/Writers/Go/CodeInterfaceDeclarationWriter.cs Removes redundant string interpolation in output.
src/Kiota.Builder/Writers/Go/CodeFileDeclarationWriter.cs Changes import ordering/format at file scope.
src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs Refactors enum parsing switch generation for gofmt-style indentation.
src/Kiota.Builder/Writers/Go/CodeClassDeclarationWriter.cs Removes redundant string interpolation in output.
src/Kiota.Builder/Refiners/GoRefiner.cs Updates deserializer function return type formatting.
it/go/app.go Reorders imports/indentation in Go integration test app.
CHANGELOG.md Adds changelog entries for the Go formatting changes (and adjusts some older formatting).
.devcontainer/devcontainer.json Updates recommended VS Code extensions in devcontainer configuration.

Comment thread src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs Outdated
Comment thread src/Kiota.Builder/Writers/Go/CodeEnumWriter.cs
Comment thread src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
Comment thread src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
Comment thread src/Kiota.Builder/Writers/Go/CodeFileDeclarationWriter.cs Outdated
Comment thread src/Kiota.Builder/Writers/Go/CodeProprietableBlockDeclarationWriter.cs Outdated
Comment thread src/Kiota.Builder/Writers/LanguageWriter.cs Outdated
Comment thread tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs Outdated
Comment thread CHANGELOG.md Outdated
Comment thread src/Kiota.Builder/Writers/Go/GoConventionService.cs Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 4 comments.

Comment thread src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
Comment thread src/Kiota.Builder/CodeRenderers/GoCodeRenderer.cs Outdated
Comment thread CHANGELOG.md Outdated
Comment thread tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 3 comments.

Comment thread src/Kiota.Builder/Writers/LanguageWriter.cs Outdated
Comment thread CHANGELOG.md Outdated
Comment thread src/Kiota.Builder/Writers/Go/GoConventionService.cs Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 4 comments.

Comment thread src/Kiota.Builder/Writers/LanguageWriter.cs Outdated
Comment thread src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
Comment thread src/Kiota.Builder/Writers/Go/CodeFileDeclarationWriter.cs Outdated
Comment thread src/Kiota.Builder/Writers/Go/CodeProprietableBlockDeclarationWriter.cs Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 1 comment.

Comment thread tests/Kiota.Builder.IntegrationTests/GenerateSample.cs
@firefart

Copy link
Copy Markdown
Contributor Author

@baywet sorry for the long silence, I totally forgot about this open PR. I just added the missing tests. Please let me know if you need anything else on this to get it ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

golang - Code is not correctly formatted

3 participants