github.com/googleapis/api-linter@v1.65.2/rules/aip0162/rollback_response_message_name.go (about) 1 // Copyright 2021 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 aip0162 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 rollbackResponseMessageName = &lint.MethodRule{ 28 Name: lint.NewRuleName(162, "rollback-response-message-name"), 29 OnlyIf: isRollbackMethod, 30 LintMethod: func(m *desc.MethodDescriptor) []lint.Problem { 31 // Rule check: Establish that for methods such as `RollbackBook`, the response 32 // message is `Book`. 33 want := rollbackMethodRegexp.FindStringSubmatch(m.GetName())[1] 34 got := m.GetOutputType().GetName() 35 loc := locations.MethodResponseType(m) 36 37 // If LRO, check the response_type short name. 38 if utils.IsOperation(m.GetOutputType()) { 39 t := utils.GetOperationInfo(m).GetResponseType() 40 ndx := strings.LastIndex(t, ".") 41 got = t[ndx+1:] 42 loc = locations.MethodOperationInfo(m) 43 } 44 45 // Return a problem if we did not get the expected return name. 46 if got != want { 47 return []lint.Problem{{ 48 Message: fmt.Sprintf( 49 "Rollback RPCs should have response message type %q, not %q.", 50 want, 51 got, 52 ), 53 Suggestion: want, 54 Descriptor: m, 55 Location: loc, 56 }} 57 } 58 return nil 59 }, 60 }