github.com/googleapis/api-linter@v1.65.2/rules/aip0133/http_uri_resource.go (about) 1 // Copyright 2022 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 aip0133 16 17 import ( 18 "fmt" 19 "strings" 20 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 // The resource name used in the Create method's URI should match the name used 27 // in the resource definition. 28 var httpURIResource = &lint.MethodRule{ 29 Name: lint.NewRuleName(133, "http-uri-resource"), 30 OnlyIf: func(m *desc.MethodDescriptor) bool { 31 return utils.IsCreateMethod(m) && len(utils.GetHTTPRules(m)) > 0 32 }, 33 LintMethod: func(m *desc.MethodDescriptor) []lint.Problem { 34 problems := []lint.Problem{} 35 36 // Extract the suffix of the URI path as the collection identifier. 37 uriParts := strings.Split(utils.GetHTTPRules(m)[0].URI, "/") 38 collectionName := uriParts[len(uriParts)-1] 39 // Custom Method Standard Create lookalikes can still be linted, but 40 // don't include the custom method http suffix 41 // e.g. .../books:createAndCheckout --> books. 42 if strings.Contains(collectionName, ":") { 43 collectionName = strings.Split(collectionName, ":")[0] 44 } 45 46 // Ensure that a collection identifier is provided. 47 if collectionName == "" { 48 return []lint.Problem{{ 49 Message: "The URI path does not end in a collection identifier.", 50 Descriptor: m, 51 }} 52 } 53 54 // Go through each pattern in the resource and make sure it contains the 55 // collection identifier. 56 collectionName += "/" 57 resource := utils.GetResource(m.GetOutputType()) 58 for _, pattern := range resource.GetPattern() { 59 if !strings.Contains(pattern, collectionName) { 60 problems = append(problems, lint.Problem{ 61 Message: fmt.Sprintf("Resource pattern should contain the collection identifier %q.", collectionName), 62 Descriptor: m.GetOutputType(), 63 }) 64 } 65 } 66 67 return problems 68 }, 69 }