github.com/googleapis/api-linter@v1.65.2/rules/aip0151/lro_response_reachable.go (about)

     1  // Copyright 2019 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 aip0151
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/googleapis/api-linter/lint"
    22  	"github.com/googleapis/api-linter/locations"
    23  	"github.com/googleapis/api-linter/rules/internal/utils"
    24  	"github.com/jhump/protoreflect/desc"
    25  )
    26  
    27  var lroResponseReachable = &lint.MethodRule{
    28  	Name:   lint.NewRuleName(151, "lro-response-reachable"),
    29  	OnlyIf: isAnnotatedLRO,
    30  	LintMethod: func(m *desc.MethodDescriptor) (problems []lint.Problem) {
    31  		return checkReachable(m, utils.GetOperationInfo(m).GetResponseType())
    32  	},
    33  }
    34  
    35  func checkReachable(m *desc.MethodDescriptor, name string) []lint.Problem {
    36  	// Ignore types defined in other packages.
    37  	if name == "" || strings.Contains(name, ".") {
    38  		return nil
    39  	}
    40  
    41  	// Make this the fully qualified type name.
    42  	f := m.GetFile()
    43  	if pkg := f.GetPackage(); pkg != "" {
    44  		name = pkg + "." + name
    45  	}
    46  
    47  	// If the message is defined in the file, we are good to go.
    48  	for _, file := range utils.GetAllDependencies(f) {
    49  		if file.FindMessage(name) != nil {
    50  			return nil
    51  		}
    52  	}
    53  
    54  	// We could not find the message; complain.
    55  	return []lint.Problem{{
    56  		Message: fmt.Sprintf(
    57  			"Message %q must be defined in the same file, or a file imported by this file.",
    58  			name,
    59  		),
    60  		Descriptor: m,
    61  		Location:   locations.MethodOperationInfo(m),
    62  	}}
    63  }