github.com/googleapis/api-linter@v1.65.2/rules/aip0122/name_suffix.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 aip0122 16 17 import ( 18 "strings" 19 20 "bitbucket.org/creachadair/stringset" 21 "github.com/googleapis/api-linter/lint" 22 "github.com/googleapis/api-linter/locations" 23 "github.com/jhump/protoreflect/desc" 24 ) 25 26 var nameSuffix = &lint.FieldRule{ 27 Name: lint.NewRuleName(122, "name-suffix"), 28 OnlyIf: func(f *desc.FieldDescriptor) bool { 29 n := f.GetName() 30 // Ignore `{prefix}_display_name` fields as this seems like a reasonable suffix. 31 return strings.HasSuffix(n, "_name") && !strings.HasSuffix(n, "_display_name") 32 }, 33 LintField: func(f *desc.FieldDescriptor) []lint.Problem { 34 allowedNameFields := stringset.New( 35 "display_name", 36 "family_name", 37 "given_name", 38 "full_resource_name", 39 "crypto_key_name", 40 "cmek_key_name", 41 ) 42 if n := f.GetName(); !allowedNameFields.Contains(n) { 43 return []lint.Problem{{ 44 Message: "Fields should not use the `_name` suffix.", 45 Suggestion: strings.TrimSuffix(n, "_name"), 46 Descriptor: f, 47 Location: locations.DescriptorName(f), 48 }} 49 } 50 return nil 51 }, 52 }