github.com/googleapis/api-linter@v1.65.2/rules/aip0121/resource_must_support_get.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 resourceMustSupportGet = &lint.ServiceRule{
    27  	Name: lint.NewRuleName(121, "resource-must-support-get"),
    28  	LintService: func(s *desc.ServiceDescriptor) []lint.Problem {
    29  		var problems []lint.Problem
    30  		var resourcesWithGet stringset.Set
    31  		var resourcesWithOtherMethods stringset.Set
    32  
    33  		// Iterate all RPCs and try to find resources. Mark the
    34  		// resources which have a Get 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.IsGetMethod(m) && utils.IsResource(utils.GetResponseType(m)) {
    43  				t := utils.GetResource(m.GetOutputType()).GetType()
    44  				resourcesWithGet.Add(t)
    45  			} else if utils.IsCreateMethod(m) || utils.IsUpdateMethod(m) {
    46  				if msg := utils.GetResponseType(m); msg != nil && utils.IsResource(msg) {
    47  					t := utils.GetResource(msg).GetType()
    48  					resourcesWithOtherMethods.Add(t)
    49  				}
    50  			} else if utils.IsListMethod(m) {
    51  				if msg := utils.GetListResourceMessage(m); msg != nil && utils.IsResource(msg) {
    52  					t := utils.GetResource(msg).GetType()
    53  					resourcesWithOtherMethods.Add(t)
    54  				}
    55  			}
    56  		}
    57  
    58  		for t := range resourcesWithOtherMethods {
    59  			if !resourcesWithGet.Contains(t) {
    60  				problems = append(problems, lint.Problem{
    61  					Message: fmt.Sprintf(
    62  						"Missing Standard Get method for resource %q", t,
    63  					),
    64  					Descriptor: s,
    65  				})
    66  			}
    67  		}
    68  		return problems
    69  	},
    70  }