github.com/googleapis/api-linter@v1.65.2/rules/aip0132/request_parent_valid_reference.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 aip0132 16 17 import ( 18 "fmt" 19 "strings" 20 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 ) 26 27 var requestParentValidReference = &lint.FieldRule{ 28 Name: lint.NewRuleName(132, "request-parent-valid-reference"), 29 OnlyIf: func(f *desc.FieldDescriptor) bool { 30 ref := utils.GetResourceReference(f) 31 return utils.IsListRequestMessage(f.GetOwner()) && f.GetName() == "parent" && ref != nil && ref.GetType() != "" 32 }, 33 LintField: func(f *desc.FieldDescriptor) []lint.Problem { 34 p := f.GetParent() 35 msg := p.(*desc.MessageDescriptor) 36 res := utils.GetResourceReference(f).GetType() 37 38 response := utils.FindMessage(f.GetFile(), strings.Replace(msg.GetName(), "Request", "Response", 1)) 39 if response == nil { 40 return nil 41 } 42 43 for _, field := range response.GetFields() { 44 typ := field.GetMessageType() 45 if !field.IsRepeated() && typ == nil { 46 continue 47 } 48 49 if r := utils.GetResource(typ); r != nil && r.GetType() == res { 50 return []lint.Problem{{ 51 Message: fmt.Sprintf("The `google.api.resource_reference` on `%s` field should reference the parent(s) of `%s`.", f.GetName(), res), 52 Descriptor: f, 53 Location: locations.FieldResourceReference(f), 54 }} 55 } 56 } 57 58 return nil 59 }, 60 }