github.com/googleapis/api-linter@v1.65.2/rules/aip0233/request_requests_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/gertd/go-pluralize" 22 "github.com/googleapis/api-linter/lint" 23 "github.com/googleapis/api-linter/locations" 24 "github.com/jhump/protoreflect/desc" 25 ) 26 27 // The Batch Create standard method should have repeated standard create request 28 // message field. 29 var requestRequestsField = &lint.MessageRule{ 30 Name: lint.NewRuleName(233, "request-requests-field"), 31 OnlyIf: isBatchCreateRequestMessage, 32 LintMessage: func(m *desc.MessageDescriptor) (problems []lint.Problem) { 33 // Rule check: Establish that a "requests" field is present. 34 requests := m.FindFieldByName("requests") 35 36 // Rule check: Ensure that the "requests" field is existed. 37 if requests == nil { 38 return []lint.Problem{{ 39 Message: fmt.Sprintf(`Message %q has no "requests" field`, m.GetName()), 40 Descriptor: m, 41 }} 42 } 43 44 // Rule check: Ensure that the standard create request message field "requests" is repeated. 45 if !requests.IsRepeated() { 46 problems = append(problems, lint.Problem{ 47 Message: `The "requests" field should be repeated`, 48 Descriptor: requests, 49 }) 50 } 51 52 // Rule check: Ensure that the standard create request message field is the 53 // correct type. Note: Retrieve the resource name from the the batch create 54 // request, for example: "BatchCreateBooksRequest" -> "Books" 55 rightTypeName := fmt.Sprintf("Create%sRequest", 56 pluralize.NewClient().Singular(strings.TrimPrefix(strings.TrimSuffix(m.GetName(), "Request"), "BatchCreate"))) 57 if requests.GetMessageType() == nil || requests.GetMessageType().GetName() != rightTypeName { 58 problems = append(problems, lint.Problem{ 59 Message: fmt.Sprintf(`The "requests" field on Batch Create Request should be a %q type`, rightTypeName), 60 Descriptor: requests, 61 Location: locations.FieldType(requests), 62 Suggestion: rightTypeName, 63 }) 64 } 65 return 66 }, 67 }