fix(api): sort label keys in ValidateLabels for deterministic errors - #293
Merged
aojea merged 1 commit intoAug 22, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates ValidateLabels to sort map keys lexicographically before validation, ensuring deterministic and stable error reporting across runs. It also adds corresponding unit tests to verify this behavior. The review feedback suggests optimizing ValidateLabels by returning early when the map is empty to avoid unnecessary allocations, and improving the determinism test by adding a second invalid key to ensure the sorting logic is robustly tested.
Go map iteration order is randomized, so ValidateLabels returned whichever bad key the runtime happened to visit first. After this change the function walks the input in lexicographic order, making the first reported error stable across runs and easier to assert in tests. Mirrors the same pattern already used in api/datalog.go LabelsToFacts. Also short-circuits when the input map is empty or nil so the common "no labels" case skips the slice allocation entirely. The regression test now exercises a mix of valid keys and three independently invalid keys (whitespace, comma, bang) over 200 iterations and asserts the reported error always quotes the lexicographically smallest invalid key, proving sorting is what controlled the choice. A second test confirms the happy path is unaffected.
HosniBelfeki
force-pushed
the
fix/api-labels-deterministic-error
branch
from
August 22, 2026 11:41
8652f5c to
e5a8338
Compare
Contributor
Author
|
Addressed the two Gemini Code Assist suggestions in the latest force-push (
Local validations re-run after the change: |
Collaborator
|
Thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
api.ValidateLabelsiterates the inputmap[string]stringdirectly and returns on the first invalid key it visits. Because Go map iteration is randomized, the reported key — and therefore the diagnostic surface operators see — varies between runs. Two reproductions of the same bad config could print different keys, and a flaky unit test against a multi-bad-key input would be unreliable.sort.Strings, and walks them in that order. The reported key for any given input is now deterministic (the lexicographically smallest invalid one), and the function stays a no-op for valid input. The pattern mirrors the existing one already used forLabelsToFactsinapi/datalog.go, so no new abstraction is introduced.api/labels_test.go:TestValidateLabels_ReportsFirstErrorDeterministicallycallsValidateLabels200 times on a five-bad-key input and asserts the error always quotes the lexicographically smallest invalid key. The previous nondeterministic implementation would have leaked a different key on different iterations.TestValidateLabels_AllKeysValidStaysSortedasserts the happy path is unaffected by the sort.No new module is added to
go.mod; only the stdlibsortpackage is used, in line with the repository's dependency policy.Tests
The package-local test result above is the only check I can run locally without gcc/CGO-enabled race; CI on
google/samwill run the full gated matrix on top of this.