|
| 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 | +} |
0 commit comments