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

     1  // Copyright 2023 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 aip0121
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"bitbucket.org/creachadair/stringset"
    21  	"github.com/googleapis/api-linter/lint"
    22  	"github.com/googleapis/api-linter/rules/internal/utils"
    23  	"github.com/jhump/protoreflect/desc"
    24  )
    25  
    26  var resourceMustSupportList = &lint.ServiceRule{
    27  	Name: lint.NewRuleName(121, "resource-must-support-list"),
    28  	LintService: func(s *desc.ServiceDescriptor) []lint.Problem {
    29  		var problems []lint.Problem
    30  		var resourcesWithList stringset.Set
    31  		var resourcesWithOtherMethods stringset.Set
    32  
    33  		// Iterate all RPCs and try to find resources. Mark the
    34  		// resources which have a List method, and which ones do not.
    35  		for _, m := range s.GetMethods() {
    36  			// Streaming methods do not count as standard methods even if they
    37  			// look like them.
    38  			if utils.IsStreaming(m) {
    39  				continue
    40  			}
    41  
    42  			if utils.IsListMethod(m) {
    43  				if msg := utils.GetListResourceMessage(m); msg != nil && utils.IsResource(msg) {
    44  					t := utils.GetResource(msg).GetType()
    45  					resourcesWithList.Add(t)
    46  				}
    47  			} else if utils.IsCreateMethod(m) || utils.IsUpdateMethod(m) || utils.IsGetMethod(m) {
    48  				if msg := utils.GetResponseType(m); msg != nil && utils.IsResource(msg) {
    49  					// Skip tracking Singleton resources, they do not need List.
    50  					if utils.IsSingletonResource(msg) {
    51  						continue
    52  					}
    53  					t := utils.GetResource(msg).GetType()
    54  					resourcesWithOtherMethods.Add(t)
    55  				}
    56  			}
    57  		}
    58  
    59  		for t := range resourcesWithOtherMethods {
    60  			if !resourcesWithList.Contains(t) {
    61  				problems = append(problems, lint.Problem{
    62  					Message: fmt.Sprintf(
    63  						"Missing Standard List method for resource %q", t,
    64  					),
    65  					Descriptor: s,
    66  				})
    67  			}
    68  		}
    69  		return problems
    70  	},
    71  }