Skip to content

Commit 5f43847

Browse files
authored
refactor(internal/sidekick): migrate Discovery to api (#4111)
Migrate Discovery from internal/sidekick/config to internal/sidekick/api. This centralizes model overrides and removes the config dependency from the parser and language generators. Fixes #4041
1 parent 485e027 commit 5f43847

12 files changed

Lines changed: 90 additions & 175 deletions

File tree

internal/librarian/rust/codec.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/googleapis/librarian/internal/config"
2222
"github.com/googleapis/librarian/internal/serviceconfig"
2323
"github.com/googleapis/librarian/internal/sidekick/api"
24-
sidekickconfig "github.com/googleapis/librarian/internal/sidekick/config"
2524
"github.com/googleapis/librarian/internal/sidekick/parser"
2625
"github.com/googleapis/librarian/internal/sidekick/source"
2726
)
@@ -89,14 +88,14 @@ func libraryToModelConfig(library *config.Library, ch *config.API, sources *sour
8988
}
9089
}
9190
if library.Rust.Discovery != nil {
92-
pollers := make([]*sidekickconfig.Poller, len(library.Rust.Discovery.Pollers))
91+
pollers := make([]*api.Poller, len(library.Rust.Discovery.Pollers))
9392
for i, poller := range library.Rust.Discovery.Pollers {
94-
pollers[i] = &sidekickconfig.Poller{
93+
pollers[i] = &api.Poller{
9594
Prefix: poller.Prefix,
9695
MethodID: poller.MethodID,
9796
}
9897
}
99-
modelCfg.Discovery = &sidekickconfig.Discovery{
98+
modelCfg.Discovery = &api.Discovery{
10099
OperationID: library.Rust.Discovery.OperationID,
101100
Pollers: pollers,
102101
}

internal/librarian/rust/codec_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/google/go-cmp/cmp"
2222
"github.com/googleapis/librarian/internal/config"
2323
"github.com/googleapis/librarian/internal/sidekick/api"
24-
sidekickconfig "github.com/googleapis/librarian/internal/sidekick/config"
2524
"github.com/googleapis/librarian/internal/sidekick/parser"
2625
"github.com/googleapis/librarian/internal/sidekick/source"
2726
)
@@ -509,9 +508,9 @@ func TestLibraryToModelConfig(t *testing.T) {
509508
Override: api.ModelOverride{
510509
Title: "Google Compute Engine API",
511510
},
512-
Discovery: &sidekickconfig.Discovery{
511+
Discovery: &api.Discovery{
513512
OperationID: ".google.cloud.compute.v1.Operation",
514-
Pollers: []*sidekickconfig.Poller{
513+
Pollers: []*api.Poller{
515514
{
516515
Prefix: "compute/v1/projects/{project}/zones/{zone}",
517516
MethodID: ".google.cloud.compute.v1.zoneOperations.get",
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package config
15+
package api
1616

1717
import (
1818
"strings"
@@ -25,7 +25,7 @@ type Discovery struct {
2525
// The ID of the LRO operation type.
2626
//
2727
// For example: ".google.cloud.compute.v1.Operation".
28-
OperationID string `toml:"operation-id"`
28+
OperationID string
2929

3030
// Possible prefixes to match the LRO polling RPCs.
3131
//
@@ -45,7 +45,7 @@ type Poller struct {
4545
Prefix string
4646

4747
// The corresponding method ID.
48-
MethodID string `toml:"method-id"`
48+
MethodID string
4949
}
5050

5151
// LroServices returns the set of Discovery LRO services.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 api
16+
17+
import (
18+
"testing"
19+
20+
"github.com/google/go-cmp/cmp"
21+
)
22+
23+
func TestLroServices(t *testing.T) {
24+
d := &Discovery{
25+
Pollers: []*Poller{
26+
{Prefix: "p1", MethodID: "Service1.Method1"},
27+
{Prefix: "p2", MethodID: "Service1.Method2"},
28+
{Prefix: "p3", MethodID: "Service2.Method3"},
29+
},
30+
}
31+
want := map[string]bool{
32+
"Service1": true,
33+
"Service2": true,
34+
}
35+
got := d.LroServices()
36+
if diff := cmp.Diff(want, got); diff != "" {
37+
t.Errorf("LroServices() mismatch (-want +got):\n%s", diff)
38+
}
39+
}
40+
41+
func TestPathParameters(t *testing.T) {
42+
tests := []struct {
43+
name string
44+
input *Poller
45+
want []string
46+
}{
47+
{
48+
name: "multiple parameters",
49+
input: &Poller{Prefix: "projects/{project}/zones/{zone}"},
50+
want: []string{"project", "zone"},
51+
},
52+
{
53+
name: "no parameters",
54+
input: &Poller{Prefix: "abc/def"},
55+
want: nil,
56+
},
57+
{
58+
name: "single parameter",
59+
input: &Poller{Prefix: "a/{b}"},
60+
want: []string{"b"},
61+
},
62+
}
63+
for _, test := range tests {
64+
t.Run(test.name, func(t *testing.T) {
65+
got := test.input.PathParameters()
66+
if diff := cmp.Diff(test.want, got); diff != "" {
67+
t.Errorf("PathParameters(%q) mismatch (-want +got):\n%s", test.input.Prefix, diff)
68+
}
69+
})
70+
}
71+
}

internal/sidekick/config/config.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

internal/sidekick/config/discovery_test.go

Lines changed: 0 additions & 129 deletions
This file was deleted.

internal/sidekick/config/source_roots.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package config provides functionality for working with the sidekick.toml
16+
// configuration file.
1517
package config
1618

1719
import (

internal/sidekick/parser/discovery/discovery.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ import (
2626

2727
"github.com/googleapis/librarian/internal/serviceconfig"
2828
"github.com/googleapis/librarian/internal/sidekick/api"
29-
"github.com/googleapis/librarian/internal/sidekick/config"
3029
"github.com/googleapis/librarian/internal/sidekick/parser/svcconfig"
3130
)
3231

3332
// NewAPI parses the discovery doc in `contents` and returns the corresponding `api.API` model.
34-
func NewAPI(serviceConfig *serviceconfig.Service, contents []byte, discoveryConfig *config.Discovery) (*api.API, error) {
33+
func NewAPI(serviceConfig *serviceconfig.Service, contents []byte, discoveryConfig *api.Discovery) (*api.API, error) {
3534
doc, err := newDiscoDocument(contents)
3635
if err != nil {
3736
return nil, err

internal/sidekick/parser/discovery/discovery_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/googleapis/librarian/internal/serviceconfig"
2727
"github.com/googleapis/librarian/internal/sidekick/api"
2828
"github.com/googleapis/librarian/internal/sidekick/api/apitest"
29-
"github.com/googleapis/librarian/internal/sidekick/config"
3029
)
3130

3231
const computeDiscoveryFile = "../../../testdata/discovery/compute.v1.json"
@@ -234,7 +233,7 @@ func ComputeDisco(t *testing.T, sc *serviceconfig.Service) (*api.API, error) {
234233
return NewAPI(sc, contents, nil)
235234
}
236235

237-
func ComputeDiscoWithLros(t *testing.T, discoveryConfig *config.Discovery) (*api.API, error) {
236+
func ComputeDiscoWithLros(t *testing.T, discoveryConfig *api.Discovery) (*api.API, error) {
238237
t.Helper()
239238
contents, err := os.ReadFile(computeDiscoveryFile)
240239
if err != nil {

internal/sidekick/parser/discovery/lro.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ import (
1919
"strings"
2020

2121
"github.com/googleapis/librarian/internal/sidekick/api"
22-
"github.com/googleapis/librarian/internal/sidekick/config"
2322
)
2423

25-
func lroAnnotations(model *api.API, discoveryConfig *config.Discovery) error {
24+
func lroAnnotations(model *api.API, discoveryConfig *api.Discovery) error {
2625
if discoveryConfig == nil {
2726
return nil
2827
}
@@ -72,7 +71,7 @@ func lroAnnotations(model *api.API, discoveryConfig *config.Discovery) error {
7271
return nil
7372
}
7473

75-
func lroFindPoller(method *api.Method, model *api.API, discoveryConfig *config.Discovery) (*api.Method, []string) {
74+
func lroFindPoller(method *api.Method, model *api.API, discoveryConfig *api.Discovery) (*api.Method, []string) {
7675
var flatPath []string
7776
for _, binding := range method.PathInfo.Bindings {
7877
flatPath = append(flatPath, binding.PathTemplate.FlatPath())

0 commit comments

Comments
 (0)