Skip to content

Commit 9a793b5

Browse files
author
Luke Sneeringer
authored
fix: Fix enums with words ending in numerals. (#618)
Fixes #614.
1 parent 20b5ec1 commit 9a793b5

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

rules/aip0126/unspecified.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package aip0126
1616

1717
import (
1818
"fmt"
19+
"regexp"
1920
"strings"
2021

2122
"github.com/googleapis/api-linter/lint"
@@ -27,13 +28,16 @@ import (
2728
var unspecified = &lint.EnumRule{
2829
Name: lint.NewRuleName(126, "unspecified"),
2930
LintEnum: func(e *desc.EnumDescriptor) []lint.Problem {
30-
firstValue := e.GetValues()[0]
31-
want := strings.ToUpper(strcase.SnakeCase(e.GetName()) + "_UNSPECIFIED")
31+
name := endNum.ReplaceAllString(e.GetName(), "${1}_${2}")
32+
want := strings.ToUpper(strcase.SnakeCase(name) + "_UNSPECIFIED")
3233
for _, element := range e.GetValues() {
3334
if element.GetName() == want && element.GetNumber() == 0 {
3435
return nil
3536
}
3637
}
38+
39+
// We did not find the enum value we wanted; complain.
40+
firstValue := e.GetValues()[0]
3741
return []lint.Problem{{
3842
Message: fmt.Sprintf("The first enum value should be %q", want),
3943
Suggestion: want,
@@ -42,3 +46,5 @@ var unspecified = &lint.EnumRule{
4246
}}
4347
},
4448
}
49+
50+
var endNum = regexp.MustCompile("([0-9])([A-Z])")

rules/aip0126/unspecified_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,22 @@ import (
2323
func TestUnspecified(t *testing.T) {
2424
tests := []struct {
2525
testName string
26+
EnumName string
2627
ValueName string
2728
problems testutils.Problems
2829
}{
29-
{"Valid", "BOOK_FORMAT_UNSPECIFIED", testutils.Problems{}},
30-
{"InvalidNoPrefix", "UNSPECIFIED", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}},
31-
{"InvalidWrongSuffix", "BOOK_FORMAT_UNKNOWN", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}},
32-
{"InvalidJustWrong", "UNKNOWN", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}},
30+
{"Valid", "BookFormat", "BOOK_FORMAT_UNSPECIFIED", testutils.Problems{}},
31+
{"ValidWithNum", "Ipv6Format", "IPV6_FORMAT_UNSPECIFIED", nil},
32+
{"InvalidNoPrefix", "BookFormat", "UNSPECIFIED", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}},
33+
{"InvalidWrongSuffix", "BookFormat", "BOOK_FORMAT_UNKNOWN", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}},
34+
{"InvalidJustWrong", "BookFormat", "UNKNOWN", testutils.Problems{{Suggestion: "BOOK_FORMAT_UNSPECIFIED"}}},
35+
{"InvalidWithNum", "Ipv6Format", "IPV6FORMAT_UNSPECIFIED", testutils.Problems{{Suggestion: "IPV6_FORMAT_UNSPECIFIED"}}},
3336
}
3437
for _, test := range tests {
3538
t.Run(test.testName, func(t *testing.T) {
3639
// Create the proto with the enum.
3740
f := testutils.ParseProto3Tmpl(t, `
38-
enum BookFormat {
41+
enum {{.EnumName}} {
3942
{{.ValueName}} = 0;
4043
HARDBACK = 1;
4144
PAPERBACK = 2;
@@ -53,8 +56,8 @@ func TestUnspecified(t *testing.T) {
5356
t.Run(test.testName, func(t *testing.T) {
5457
// Create the proto with the enum.
5558
f := testutils.ParseProto3Tmpl(t, `
56-
enum BookFormat {
57-
option allow_alias = true;
59+
enum {{.EnumName}} {
60+
option allow_alias = true;
5861
HARDBACK = 0;
5962
{{.ValueName}} = 0;
6063
PAPERBACK = 1;

0 commit comments

Comments
 (0)