Skip to content

Commit 9063240

Browse files
authored
impl(sidekick): populate services skeleton (#1890)
This change starts parsing the services. Only resources with methods get a `api.Service` entry.
1 parent 8fd2f80 commit 9063240

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func NewAPI(serviceConfig *serviceconfig.Service, contents []byte) (*api.API, er
8282
result.State.MessageByID[id] = message
8383
}
8484

85+
for _, resource := range doc.Resources {
86+
addServiceRecursive(result, resource)
87+
}
88+
8589
return result, nil
8690
}
8791

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
"fmt"
19+
20+
"github.com/googleapis/librarian/internal/sidekick/internal/api"
21+
)
22+
23+
func addServiceRecursive(model *api.API, resource *resource) {
24+
if len(resource.Methods) != 0 {
25+
addService(model, resource)
26+
}
27+
for _, child := range resource.Resources {
28+
addServiceRecursive(model, child)
29+
}
30+
}
31+
32+
func addService(model *api.API, resource *resource) {
33+
id := fmt.Sprintf(".%s.%s", model.PackageName, resource.Name)
34+
var service *api.Service
35+
if _, ok := model.State.ServiceByID[id]; !ok {
36+
service = &api.Service{
37+
ID: id,
38+
Name: resource.Name,
39+
Package: model.PackageName,
40+
Documentation: fmt.Sprintf("Service for the `%s` resource.", resource.Name),
41+
}
42+
model.Services = append(model.Services, service)
43+
model.State.ServiceByID[id] = service
44+
}
45+
// TODO(#1850) - add the methods, if the service already exists then merge
46+
// the methods.
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
"testing"
19+
20+
"github.com/googleapis/librarian/internal/sidekick/internal/api"
21+
"github.com/googleapis/librarian/internal/sidekick/internal/api/apitest"
22+
)
23+
24+
func TestService(t *testing.T) {
25+
model, err := PublicCaDisco(t, nil)
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
if _, ok := model.State.ServiceByID["..projects"]; ok {
30+
t.Errorf("expected no service for `projects` resource as it has no methods")
31+
}
32+
33+
id := "..externalAccountKeys"
34+
got, ok := model.State.ServiceByID[id]
35+
if !ok {
36+
t.Fatalf("expected service %s in the API model", id)
37+
}
38+
want := &api.Service{
39+
Name: "externalAccountKeys",
40+
ID: id,
41+
Package: "",
42+
Documentation: "Service for the `externalAccountKeys` resource.",
43+
}
44+
apitest.CheckService(t, got, want)
45+
}

0 commit comments

Comments
 (0)