github.com/googleapis/api-linter@v1.65.2/rules/aip0133/request_resource_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 aip0133 16 17 import ( 18 "fmt" 19 20 "github.com/googleapis/api-linter/lint" 21 "github.com/googleapis/api-linter/locations" 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 create request message should have resource field. 28 var resourceField = &lint.MessageRule{ 29 Name: lint.NewRuleName(133, "request-resource-field"), 30 OnlyIf: utils.IsCreateRequestMessage, 31 LintMessage: func(m *desc.MessageDescriptor) []lint.Problem { 32 resourceMsgName := getResourceMsgNameFromReq(m) 33 34 // The rule (resource field name must map to the POST body) is 35 // checked by AIP-0133 ("core::0133::http-body") 36 for _, fieldDesc := range m.GetFields() { 37 if msgDesc := fieldDesc.GetMessageType(); msgDesc != nil && msgDesc.GetName() == resourceMsgName { 38 // Rule check: Is the field named properly? 39 if want := strcase.SnakeCase(resourceMsgName); fieldDesc.GetName() != want { 40 return []lint.Problem{{ 41 Message: fmt.Sprintf("Resource field should be named %q.", want), 42 Descriptor: fieldDesc, 43 Suggestion: want, 44 Location: locations.DescriptorName(fieldDesc), 45 }} 46 } 47 return nil 48 } 49 } 50 51 // Rule check: Establish that a resource field must be included. 52 return []lint.Problem{{ 53 Message: fmt.Sprintf("Message %q has no %q type field", m.GetName(), resourceMsgName), 54 Descriptor: m, 55 }} 56 }, 57 }