Skip to content

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

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

Open
wants to merge 49 commits into
base: main
Choose a base branch
from

Conversation

firefart
Copy link
Contributor

@firefart firefart commented Apr 6, 2025

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/net9.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

Still to do

  • trailing spaces on setter methods
  • some missing spaces between declarations

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

@baywet baywet left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

@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
@firefart
Copy link
Contributor Author

firefart commented May 4, 2025

The first version is ready for review @baywet . There are still some minor issues which I was not able to fix as I did not find the code that generates the corresponding code. I will open an issue with the additional minor issues that need to be adressed

@baywet
Copy link
Member

baywet commented May 13, 2025

@firefart thanks for the extended work on this. As for your question, have you looked here?

writer.WriteLine($"{propertyName} {returnType}{suffix}");

It seems to me that if both the suffix and the return type are null or empty, we should not add the space.

@firefart
Copy link
Contributor Author

@firefart thanks for the extended work on this. As for your question, have you looked here?

writer.WriteLine($"{propertyName} {returnType}{suffix}");

It seems to me that if both the suffix and the return type are null or empty, we should not add the space.

@baywet thanks for the hint but this particular place it not the provblem. Added some checks, but the trailing spaces are still generated, so it must be elsewhere (but I suspect the same pattern)

@baywet
Copy link
Member

baywet commented May 16, 2025

Thank you for the additional information.

First, I'm not seeing the trailing space behaviour from the main branch.
Second, I spent more time looking into this, and it's actually here.

writer.WriteLine($"{funcPrefix}{associatedTypePrefix}{methodName}({parameters})({finalReturnType}{errorDeclaration}){openingBracket}");

Which breaks with the following conditional breakpoint.

code.IsOfKind(CodeMethodKind.Setter) && code.Parent is CodeInterface

Let me know if you have any additional comments or questions.

@firefart
Copy link
Contributor Author

@baywet thanks, that did the trick, the trailing whitespaces are now gone. I also tried to add an integration test to github actions to run go fmt ./... on the generated code and error out if there are formatted files. Could you please approve the github actions run so I can check if the test works? (running it locally is not really possible)

Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

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

Thanks for the continuous work on this one. One minor comment left

firefart and others added 2 commits May 19, 2025 21:31
baywet
baywet previously approved these changes May 19, 2025
Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

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

Thank you for making the changes!

@baywet baywet enabled auto-merge (squash) May 19, 2025 19:37
auto-merge was automatically disabled May 19, 2025 20:13

Head branch was pushed to by a user without write access

baywet
baywet previously approved these changes May 19, 2025
@baywet baywet enabled auto-merge (squash) May 19, 2025 21:09
auto-merge was automatically disabled May 20, 2025 06:05

Head branch was pushed to by a user without write access

@firefart
Copy link
Contributor Author

@baywet fixed the unit tests, so would appreciate a new approval for all the tests.

PS: The used xunit version is really old, you should consider upgrading to xunit.v3. I needed to upgrade the local csproj files to this version so see the full strings from the Assert calls which is not possible in the current used version (I used XUNIT_PRINT_MAX_STRING_LENGTH=99999 dotnet test to test)

@@ -635,7 +635,7 @@ public void WritesNullableVoidTypeForExecutor()
};
writer.Write(method);
var result = tw.ToString();
Assert.Contains(" error {", result);
Assert.Contains("() {", result);
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this change is correct, the request executor by definition makes the request, so might encounter a whole range of different errors which need to be reported back to the user. Even if the expected response doesn't have content.

@baywet
Copy link
Member

baywet commented May 20, 2025

The used xunit version is really old, you should consider upgrading to xunit.v3

Thanks for bringing this up. From reading the migration documentation, it seems there might be quite some work for this migration. If this is something you feel like you would like to submit a pull request for, I'd suggest starting a new issue.

@baywet
Copy link
Member

baywet commented Jun 16, 2025

gentle reminder on this one @firefart

@firefart
Copy link
Contributor Author

@baywet sorry haven't found time yet to look into the tests, but hopefully by the end of the week to get this PR done

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
2 participants