From 9a793b531cffe0a982c9db350b68f59beedb8d2b Mon Sep 17 00:00:00 2001 From: Luke Sneeringer Date: Wed, 2 Sep 2020 14:49:35 -0700 Subject: [PATCH] fix: Fix enums with words ending in numerals. (#618) Fixes #614. --- rules/aip0126/unspecified.go | 10 ++++++++-- rules/aip0126/unspecified_test.go | 17 ++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/rules/aip0126/unspecified.go b/rules/aip0126/unspecified.go index f34f30b0e..e00f1c68b 100644 --- a/rules/aip0126/unspecified.go +++ b/rules/aip0126/unspecified.go @@ -16,6 +16,7 @@ package aip0126 import ( "fmt" + "regexp" "strings" "github.com/googleapis/api-linter/lint" @@ -27,13 +28,16 @@ import ( var unspecified = &lint.EnumRule{ Name: lint.NewRuleName(126, "unspecified"), LintEnum: func(e *desc.EnumDescriptor) []lint.Problem { - firstValue := e.GetValues()[0] - want := strings.ToUpper(strcase.SnakeCase(e.GetName()) + "_UNSPECIFIED") + name := endNum.ReplaceAllString(e.GetName(), "${1}_${2}") + want := strings.ToUpper(strcase.SnakeCase(name) + "_UNSPECIFIED") for _, element := range e.GetValues() { if element.GetName() == want && element.GetNumber() == 0 { return nil } } + + // We did not find the enum value we wanted; complain. + firstValue := e.GetValues()[0] return []lint.Problem{{ Message: fmt.Sprintf("The first enum value should be %q", want), Suggestion: want, @@ -42,3 +46,5 @@ var unspecified = &lint.EnumRule{ }} }, } + +var endNum = regexp.MustCompile("([0-9])([A-Z])") diff --git a/rules/aip0126/unspecified_test.go b/rules/aip0126/unspecified_test.go index e49befcac..d4919a110 100644 --- a/rules/aip0126/unspecified_test.go +++ b/rules/aip0126/unspecified_test.go @@ -23,19 +23,22 @@ import ( func TestUnspecified(t *testing.T) { tests := []struct { testName string + EnumName string ValueName string problems testutils.Problems }{ - {"Valid", "BOOK_FORMAT_UNSPECIFIED", testutils.Problems{}}, - {"InvalidNoPrefix", "UNSPECIFIED", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}}, - {"InvalidWrongSuffix", "BOOK_FORMAT_UNKNOWN", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}}, - {"InvalidJustWrong", "UNKNOWN", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}}, + {"Valid", "BookFormat", "BOOK_FORMAT_UNSPECIFIED", testutils.Problems{}}, + {"ValidWithNum", "Ipv6Format", "IPV6_FORMAT_UNSPECIFIED", nil}, + {"InvalidNoPrefix", "BookFormat", "UNSPECIFIED", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}}, + {"InvalidWrongSuffix", "BookFormat", "BOOK_FORMAT_UNKNOWN", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}}, + {"InvalidJustWrong", "BookFormat", "UNKNOWN", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}}, + {"InvalidWithNum", "Ipv6Format", "IPV6FORMAT_UNSPECIFIED", testutils.Problems{{Suggestion: "IPV6_FORMAT_UNSPECIFIED"}}}, } for _, test := range tests { t.Run(test.testName, func(t *testing.T) { // Create the proto with the enum. f := testutils.ParseProto3Tmpl(t, ` - enum BookFormat { + enum {{.EnumName}} { {{.ValueName}} = 0; HARDBACK = 1; PAPERBACK = 2; @@ -53,8 +56,8 @@ func TestUnspecified(t *testing.T) { t.Run(test.testName, func(t *testing.T) { // Create the proto with the enum. f := testutils.ParseProto3Tmpl(t, ` - enum BookFormat { - option allow_alias = true; + enum {{.EnumName}} { + option allow_alias = true; HARDBACK = 0; {{.ValueName}} = 0; PAPERBACK = 1;