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

     1  // Copyright 2020 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 aip0124
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/googleapis/api-linter/lint"
    21  	"github.com/googleapis/api-linter/locations"
    22  	"github.com/googleapis/api-linter/rules/internal/utils"
    23  	"github.com/jhump/protoreflect/desc"
    24  )
    25  
    26  var referenceSamePackage = &lint.FieldRule{
    27  	Name:   lint.NewRuleName(124, "reference-same-package"),
    28  	OnlyIf: isUnknownType,
    29  	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
    30  		// Get the type we are checking for.
    31  		ref := utils.GetResourceReference(f)
    32  		urt := ref.GetType()
    33  		if urt == "" {
    34  			urt = ref.GetChildType()
    35  		}
    36  
    37  		// Iterate over each dependency file and check for a matching resource.
    38  		for _, file := range getNonPkgDependencies(f.GetFile(), f.GetFile().GetPackage()) {
    39  			// If we find a message with a resource annotation matching our universal
    40  			// resource type, then it is in the wrong package.
    41  			for _, message := range file.GetMessageTypes() {
    42  				if res := utils.GetResource(message); res != nil && res.GetType() == urt {
    43  					return []lint.Problem{{
    44  						Message:    fmt.Sprintf("Resource type %q should be declared in the same package as it is referenced.", urt),
    45  						Descriptor: f,
    46  						Location:   locations.FieldResourceReference(f),
    47  					}}
    48  				}
    49  			}
    50  
    51  			// Some resources are defined as file annotations. Check for these too.
    52  			for _, rd := range utils.GetResourceDefinitions(file) {
    53  				if rd.GetType() == urt {
    54  					return []lint.Problem{{
    55  						Message:    fmt.Sprintf("Resource type %q should be declared in the same package as it is referenced.", urt),
    56  						Descriptor: f,
    57  						Location:   locations.FieldResourceReference(f),
    58  					}}
    59  				}
    60  			}
    61  		}
    62  
    63  		return nil
    64  	},
    65  }
    66  
    67  // getNonPkgDependencies returns dependencies in other packages.
    68  func getNonPkgDependencies(file *desc.FileDescriptor, pkg string) map[string]*desc.FileDescriptor {
    69  	answer := map[string]*desc.FileDescriptor{}
    70  	for name, dep := range utils.GetAllDependencies(file) {
    71  		if dep.GetPackage() != pkg {
    72  			answer[name] = dep
    73  		}
    74  	}
    75  	return answer
    76  }