Skip to content

Commit

Permalink
fix: Fix enums with words ending in numerals. (#618)
Browse files Browse the repository at this point in the history
Fixes #614.
  • Loading branch information
Luke Sneeringer authored Sep 2, 2020
1 parent 20b5ec1 commit 9a793b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
10 changes: 8 additions & 2 deletions rules/aip0126/unspecified.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package aip0126

import (
"fmt"
"regexp"
"strings"

"github.com/googleapis/api-linter/lint"
Expand All @@ -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,
Expand All @@ -42,3 +46,5 @@ var unspecified = &lint.EnumRule{
}}
},
}

var endNum = regexp.MustCompile("([0-9])([A-Z])")
17 changes: 10 additions & 7 deletions rules/aip0126/unspecified_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 9a793b5

Please sign in to comment.