github.com/googleapis/api-linter@v1.65.2/rules/aip0233/request_parent_field.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 aip0233 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 "github.com/stoewer/go-strcase" 25 ) 26 27 // The Batch Create request message should have parent field. 28 var requestParentField = &lint.MessageRule{ 29 Name: lint.NewRuleName(233, "request-parent-field"), 30 OnlyIf: func(m *desc.MessageDescriptor) bool { 31 // Sanity check: If the resource has a pattern, and that pattern 32 // contains only one variable, then a parent field is not expected. 33 // 34 // In order to parse out the pattern, we get the resource message 35 // from the response, then get the resource annotation from that, 36 // and then inspect the pattern there (oy!). 37 plural := strings.TrimPrefix(strings.TrimSuffix(m.GetName(), "Request"), "BatchCreate") 38 if resp := utils.FindMessage(m.GetFile(), fmt.Sprintf("BatchCreate%sResponse", plural)); resp != nil { 39 if paged := resp.FindFieldByName(strcase.SnakeCase(plural)); paged != nil { 40 if resource := utils.GetResource(paged.GetMessageType()); resource != nil { 41 for _, pattern := range resource.GetPattern() { 42 if strings.Count(pattern, "{") == 1 { 43 return false 44 } 45 } 46 } 47 } 48 } 49 50 return isBatchCreateRequestMessage(m) 51 }, 52 LintMessage: utils.LintFieldPresentAndSingularString("parent"), 53 }