github.com/googleapis/api-linter@v1.65.2/rules/aip0126/unspecified.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 aip0126 16 17 import ( 18 "fmt" 19 "regexp" 20 "strings" 21 22 "bitbucket.org/creachadair/stringset" 23 "github.com/googleapis/api-linter/lint" 24 "github.com/googleapis/api-linter/locations" 25 "github.com/jhump/protoreflect/desc" 26 "github.com/stoewer/go-strcase" 27 ) 28 29 var unspecified = &lint.EnumRule{ 30 Name: lint.NewRuleName(126, "unspecified"), 31 LintEnum: func(e *desc.EnumDescriptor) []lint.Problem { 32 name := endNum.ReplaceAllString(e.GetName(), "${1}_${2}") 33 unspec := strings.ToUpper(strcase.SnakeCase(name) + "_UNSPECIFIED") 34 allowed := stringset.New(unspec, "UNKNOWN") 35 for _, element := range e.GetValues() { 36 if allowed.Contains(element.GetName()) && element.GetNumber() == 0 { 37 return nil 38 } 39 } 40 41 // We did not find the enum value we wanted; complain. 42 firstValue := e.GetValues()[0] 43 return []lint.Problem{{ 44 Message: fmt.Sprintf("The first enum value should be %q", unspec), 45 Suggestion: unspec, 46 Descriptor: firstValue, 47 Location: locations.DescriptorName(firstValue), 48 }} 49 }, 50 } 51 52 var endNum = regexp.MustCompile("([0-9])([A-Z])")