github.com/googleapis/api-linter@v1.65.2/rules/aip0122/resource_collection_identifiers.go (about) 1 // Copyright 2022 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 "regexp" 19 "strings" 20 "unicode" 21 22 "github.com/googleapis/api-linter/lint" 23 "github.com/googleapis/api-linter/locations" 24 "github.com/googleapis/api-linter/rules/internal/utils" 25 "github.com/jhump/protoreflect/desc" 26 ) 27 28 var firstCharRegexp = regexp.MustCompile(`^[a-z]`) 29 30 var resourceCollectionIdentifiers = &lint.MessageRule{ 31 Name: lint.NewRuleName(122, "resource-collection-identifiers"), 32 OnlyIf: func(m *desc.MessageDescriptor) bool { 33 return utils.GetResource(m) != nil 34 }, 35 LintMessage: func(m *desc.MessageDescriptor) []lint.Problem { 36 var problems []lint.Problem 37 resource := utils.GetResource(m) 38 for _, p := range resource.GetPattern() { 39 if !firstCharRegexp.MatchString(p) { 40 return append(problems, lint.Problem{ 41 Message: "Resource patterns must start with a lowercase letter.", 42 Descriptor: m, 43 Location: locations.MessageResource(m), 44 }) 45 } 46 47 segs := strings.Split(p, "/") 48 for _, seg := range segs { 49 // Get first rune of each pattern segment. 50 c := []rune(seg)[0] 51 52 if unicode.IsLetter(c) && unicode.IsUpper(c) { 53 problems = append(problems, lint.Problem{ 54 Message: "Resource patterns must use lowerCamelCase for collection identifiers.", 55 Descriptor: m, 56 Location: locations.MessageResource(m), 57 }) 58 } 59 } 60 } 61 62 return problems 63 }, 64 }