Skip to content

Commit cefad37

Browse files
authored
fix(sidekick): sort discovery doc elements (#2279)
This is to preserve their order in the generated code. We want to avoid spurious changes due to hash table salt.
1 parent b7f75c5 commit cefad37

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

internal/sidekick/internal/parser/discovery/discovery.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,18 @@ func NewAPI(serviceConfig *serviceconfig.Service, contents []byte) (*api.API, er
8686
result.Messages = append(result.Messages, message)
8787
result.State.MessageByID[id] = message
8888
}
89+
// The messages must be sorted otherwise the generated code gets different
90+
// output on each run.
91+
slices.SortStableFunc(result.Messages, compareMessages)
8992

9093
for _, resource := range doc.Resources {
9194
if err := addServiceRecursive(result, doc, resource); err != nil {
9295
return nil, err
9396
}
9497
}
98+
// The services must be sorted otherwise the generated code gets different
99+
// output on each run.
100+
slices.SortStableFunc(result.Services, compareServices)
95101

96102
return result, nil
97103
}

internal/sidekick/internal/parser/discovery/discovery_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package discovery
1616

1717
import (
1818
"os"
19+
"slices"
1920
"strings"
2021
"testing"
2122

@@ -26,6 +27,19 @@ import (
2627
"google.golang.org/genproto/googleapis/api/serviceconfig"
2728
)
2829

30+
func TestSorted(t *testing.T) {
31+
got, err := ComputeDisco(t, nil)
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
if !slices.IsSortedFunc(got.Messages, compareMessages) {
36+
t.Fatalf("unsorted messages after parsing")
37+
}
38+
if !slices.IsSortedFunc(got.Services, compareServices) {
39+
t.Fatalf("unsorted messages after parsing")
40+
}
41+
}
42+
2943
func TestInfo(t *testing.T) {
3044
got, err := ComputeDisco(t, nil)
3145
if err != nil {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package discovery
16+
17+
import (
18+
"strings"
19+
20+
"github.com/googleapis/librarian/internal/sidekick/internal/api"
21+
)
22+
23+
func compareMessages(a, b *api.Message) int {
24+
return strings.Compare(a.Name, b.Name)
25+
}
26+
27+
func compareServices(a, b *api.Service) int {
28+
return strings.Compare(a.Name, b.Name)
29+
}

0 commit comments

Comments
 (0)