Skip to content

Commit db60eaa

Browse files
authored
feat(sidekick): scaffold resource identification logic (#4102)
This PR establishes the foundation for the logic that will identify resource naming. It is executed in `parser`, which means the information lives in the `model` and can be accessed by all languages. The implementation is split into two priorities, tracked by #4099 and #4100.
1 parent cfcb46f commit db60eaa

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2026 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+
// IdentifyTargetResources populates the TargetResource field in PathBinding
18+
// for all methods in the API.
19+
//
20+
// This is done in two passes:
21+
// 1. Explicit Identification: Matches google.api.resource_reference annotations
22+
// with fields present in the PathTemplate.
23+
// 2. Heuristic Identification: For allow-listed services, uses path segment
24+
// patterns to identify resources when annotations are missing.
25+
func IdentifyTargetResources(model *API) {
26+
for _, service := range model.Services {
27+
for _, method := range service.Methods {
28+
if method.PathInfo == nil {
29+
continue
30+
}
31+
for _, binding := range method.PathInfo.Bindings {
32+
identifyTargetResourceForBinding(method, binding)
33+
}
34+
}
35+
}
36+
}
37+
38+
// identifyTargetResourceForBinding processes a single path binding to identify its target resource.
39+
func identifyTargetResourceForBinding(method *Method, binding *PathBinding) {
40+
if binding.PathTemplate == nil {
41+
return
42+
}
43+
44+
// Priority 1: Explicit Identification
45+
// Matches google.api.resource_reference annotations.
46+
if target := identifyExplicitTarget(method, binding); target != nil {
47+
binding.TargetResource = target
48+
return
49+
}
50+
51+
// Priority 2: Heuristic Identification
52+
// Uses path segment patterns to guess the resource.
53+
// TODO(#4100): Implement IdentifyTargetResources for allow-listed services using heuristic path segment patterns.
54+
}
55+
56+
func identifyExplicitTarget(_ *Method, _ *PathBinding) *TargetResource {
57+
// TODO(#4099): Implement IdentifyTargetResources for consistent explicit resource identification in the sidekick parser.
58+
return nil
59+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2026 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+
21+
func TestIdentifyTargetResources(t *testing.T) {
22+
// TODO(#4099): Implement IdentifyTargetResources for consistent explicit resource identification in the sidekick parser.
23+
}

internal/sidekick/parser/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func CreateModel(cfg *ModelConfig) (*api.API, error) {
8080
if err := api.CrossReference(model); err != nil {
8181
return nil, err
8282
}
83+
api.IdentifyTargetResources(model)
8384
if err := api.SkipModelElements(model, cfg.Override); err != nil {
8485
return nil, err
8586
}

0 commit comments

Comments
 (0)