Skip to content

[GO] Go Server: Adds ordered routes to go-server router #21280

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 5 commits into
base: master
Choose a base branch
from

Conversation

spencercornish-wk
Copy link

Go maps are never deterministically ordered, according to the Go map documentation.

The iteration order is not specified and is not guaranteed to be the same from one call to the next

In the past, I think it was close enough to not be a problem, but since Go 1.24's refactor of how Go maps work (they are using swiss tables now) the orders are nearly random. This results in the returned Routesmap being in a different iteration order each time, which is not ideal for cases where router-order matters (#19445 for instance)

To solve this, I have added a new function called OrderedRouteswhich returns routes in an order-deterministic slice instead. I left the old Routes()getter for the time being for backwards compat.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@spencercornish-wk spencercornish-wk marked this pull request as ready for review May 14, 2025 17:41
@spencercornish-wk
Copy link
Author

FYI to the go folks:

@antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5

@@ -21,8 +21,9 @@ import (

// A Route defines the parameters for an api endpoint
type Route struct {
Method string
Pattern string
Name string
Copy link
Member

Choose a reason for hiding this comment

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

thanks for the PR

is adding Name a breaking change to the existing users using the auto-generated go server code?

Copy link
Author

Choose a reason for hiding this comment

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

From my understanding, this would only be a breaking change if someone constructed one using an unkeyed literal, so like Route{"pattern","etc"}, which in my experience is pretty rare to see in Go as the vast majority of folks used keyed literals (like Route{ Pattern: "pattern", ...})

In fact, even the Go language backwards compatibility documentation specifically calls this out and seems to indicate that only keyed structs will be ensured to be backwards compatible:

Struct literals. For the addition of features in later point releases, it may be necessary to add fields to exported structs in the API. Code that uses unkeyed struct literals (such as pkg.T{3, "x"}) to create values of these types would fail to compile after such a change. However, code that uses keyed literals (pkg.T{A: 3, B: "x"}) will continue to compile after such a change. We will update such data structures in a way that allows keyed struct literals to remain compatible, although unkeyed literals may fail to compile. (There are also more intricate cases involving nested data structures or interfaces, but they have the same resolution.) We therefore recommend that composite literals whose type is defined in a separate package should use the keyed notation.

Copy link
Author

Choose a reason for hiding this comment

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

There are a few situations where if people are using bits and pieces of generated code, but not all of it, it could be considered a breaking change, but I am not sure where y'all draw the official line for this repo (e.g. any mustache template change could be considered a breaking change if users are overriding one with their own version)

@wing328
Copy link
Member

wing328 commented May 19, 2025

can you please review the test failure when you've time?

@spencercornish-wk
Copy link
Author

@wing328 I fixed the test, thanks for the heads up on that!

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.

2 participants