github.com/googleapis/api-linter@v1.65.2/rules/aip0235/request_parent_field.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 aip0235 16 17 import ( 18 "strings" 19 20 "github.com/googleapis/api-linter/lint" 21 "github.com/googleapis/api-linter/rules/internal/utils" 22 "github.com/jhump/protoreflect/desc" 23 ) 24 25 // The Batch Delete request message should have parent field. 26 var requestParentField = &lint.MessageRule{ 27 Name: lint.NewRuleName(235, "request-parent-field"), 28 OnlyIf: func(m *desc.MessageDescriptor) bool { 29 // Sanity check: If the resource has a pattern, and that pattern 30 // contains only one variable, then a parent field is not expected. 31 // 32 // In order to parse out the pattern, we get the resource message 33 // from the request name, then get the resource annotation from that, 34 // and then inspect the pattern there (oy!). 35 plural := strings.TrimPrefix(strings.TrimSuffix(m.GetName(), "Request"), "BatchDelete") 36 singular := utils.ToSingular(plural) 37 if msg := utils.FindMessage(m.GetFile(), singular); msg != nil { 38 if resource := utils.GetResource(msg); resource != nil { 39 for _, pattern := range resource.GetPattern() { 40 if strings.Count(pattern, "{") == 1 { 41 return false 42 } 43 } 44 } 45 } 46 47 return isBatchDeleteRequestMessage(m) 48 }, 49 LintMessage: utils.LintFieldPresentAndSingularString("parent"), 50 }