github.com/googleapis/api-linter@v1.65.2/rules/aip0136/declarative_standard_methods_only.go (about) 1 // Copyright 2020 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 aip0136 16 17 import ( 18 "strings" 19 20 "bitbucket.org/creachadair/stringset" 21 "github.com/googleapis/api-linter/lint" 22 "github.com/googleapis/api-linter/rules/internal/utils" 23 "github.com/jhump/protoreflect/desc" 24 ) 25 26 var standardMethodsOnly = &lint.MethodRule{ 27 Name: lint.NewRuleName(136, "declarative-standard-methods-only"), 28 OnlyIf: utils.IsDeclarativeFriendlyMethod, 29 LintMethod: func(m *desc.MethodDescriptor) []lint.Problem { 30 // Standard methods are fine. 31 standard := stringset.New("Get", "List", "Create", "Update", "Delete", "Undelete", "Batch") 32 for s := range standard { 33 if strings.HasPrefix(m.GetName(), s) { 34 return nil 35 } 36 } 37 38 // This is likely to have a non-trivial number of exceptions, and a 39 // traditional linter disable may not be appropriate. 40 // 41 // Therefore, we allow "Imperative only." in an internal comment to make 42 // this not complain. 43 if cmt := m.GetSourceInfo().GetLeadingComments(); strings.Contains(strings.ToLower(cmt), "imperative only") { 44 return nil 45 } 46 47 // Okay, complain. 48 return []lint.Problem{{ 49 Message: strings.Join([]string{ 50 "Declarative-friendly resources should generally avoid custom methods.\n", 51 "However, if this is an imperative-only method that does *not* need ", 52 `declarative tooling support, add the text "Imperative only." to the comment. `, 53 "(Using an internal comment is fine.)", 54 }, ""), 55 Descriptor: m, 56 }} 57 }, 58 }