github.com/googleapis/api-linter@v1.65.2/rules/aip0133/aip0133.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 contains rules defined in https://aip.dev/133. 16 package aip0133 17 18 import ( 19 "strings" 20 21 "github.com/googleapis/api-linter/lint" 22 "github.com/jhump/protoreflect/desc" 23 ) 24 25 // AddRules accepts a register function and registers each of 26 // this AIP's rules to it. 27 func AddRules(r lint.RuleRegistry) error { 28 return r.Register( 29 133, 30 httpBody, 31 httpURIParent, 32 httpURIResource, 33 httpMethod, 34 inputName, 35 methodSignature, 36 outputName, 37 requestIDField, 38 requestParentBehavior, 39 requestParentField, 40 requestParentReference, 41 requestParentRequired, 42 requestRequiredFields, 43 requestResourceBehavior, 44 resourceField, 45 resourceReferenceType, 46 responseLRO, 47 synonyms, 48 unknownFields, 49 ) 50 } 51 52 // get resource message type name from request message 53 func getResourceMsgNameFromReq(m *desc.MessageDescriptor) string { 54 // retrieve the string between the prefix "Create" and suffix "Request" from 55 // the name "Create<XXX>Request", and this part will usually be the resource 56 // message name(if its naming follows the right principle) 57 resourceMsgName := m.GetName()[6 : len(m.GetName())-7] 58 59 // Get the resource field of the request message if it exist, this part will 60 // be exactly the resource message name (make a double check here to avoid the 61 // issues when request message naming doesn't follow the right principles) 62 for _, fieldDesc := range m.GetFields() { 63 if msgDesc := fieldDesc.GetMessageType(); msgDesc != nil && strings.Contains(resourceMsgName, msgDesc.GetName()) { 64 resourceMsgName = msgDesc.GetName() 65 } 66 } 67 68 return resourceMsgName 69 }