github.com/googleapis/api-linter@v1.65.2/rules/aip0191/aip0191.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 aip0191 contains rules defined in https://aip.dev/191. 16 package aip0191 17 18 import ( 19 "regexp" 20 "strings" 21 22 "github.com/googleapis/api-linter/lint" 23 "github.com/jhump/protoreflect/desc" 24 ) 25 26 // AddRules adds all of the AIP-191 rules to the provided registry. 27 func AddRules(r lint.RuleRegistry) error { 28 return r.Register( 29 191, 30 csharpNamespace, 31 filename, 32 fileLayout, 33 fileOptionConsistency, 34 javaMultipleFiles, 35 javaOuterClassname, 36 javaPackage, 37 phpNamespace, 38 protoPkg, 39 rubyPackage, 40 syntax, 41 ) 42 } 43 44 func hasPackage(f *desc.FileDescriptor) bool { 45 return f.GetPackage() != "" 46 } 47 48 func packagingServiceNameEquals(serv, pkg, sep string) bool { 49 segments := strings.Split(pkg, sep) 50 for _, segment := range segments { 51 // If a packaging annotation segment and a service name are equal in a 52 // case-insensitive comparison, they must also be equal using a 53 // case-sensitive comparison. 54 if strings.EqualFold(segment, serv) && segment != serv { 55 return false 56 } 57 } 58 59 return true 60 } 61 62 var ( 63 versionRegexp = regexp.MustCompile(`^v[0-9]+(p[0-9]+)?((alpha|beta)[0-9]*)?$`) 64 validCharacterRegexp = regexp.MustCompile(`^[a-z0-9\\_\\/]*$`) 65 )