github.com/googleapis/api-linter@v1.65.2/rules/aip0136/http_name_variable.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 aip0136 16 17 import ( 18 "strings" 19 20 pluralize "github.com/gertd/go-pluralize" 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 "github.com/stoewer/go-strcase" 26 ) 27 28 var httpNameVariable = &lint.MethodRule{ 29 Name: lint.NewRuleName(136, "http-name-variable"), 30 OnlyIf: isCustomMethod, 31 LintMethod: func(m *desc.MethodDescriptor) []lint.Problem { 32 p := pluralize.NewClient() 33 for _, http := range utils.GetHTTPRules(m) { 34 vars := http.GetVariables() 35 36 // Special case: AIP-162 describes "revision" methods; the `name` 37 // variable is appropriate (and mandated) for those. 38 if strings.HasSuffix(m.GetName(), "Revision") || strings.HasSuffix(m.GetName(), "Revisions") { 39 return nil 40 } 41 42 // If there is a "name" variable, the noun should be present 43 // in the RPC name. 44 if name, ok := vars["name"]; ok { 45 // Determine the resource. 46 name = strings.TrimSuffix(name, "/*") 47 segs := strings.Split(name, "/") 48 resource := strcase.SnakeCase(p.Singular(segs[len(segs)-1])) 49 50 // Does the RPC name end in the singular name of the resource? 51 // If not, complain. 52 if !strings.HasSuffix(strcase.SnakeCase(m.GetName()), resource) { 53 return []lint.Problem{{ 54 Message: "The name variable should only be used if the RPC noun matches the URI.", 55 Descriptor: m, 56 Location: locations.MethodHTTPRule(m), 57 }} 58 } 59 } 60 } 61 return nil 62 }, 63 }