Repository: googleapis/api-linter
Pull request #556 (2020-06-10 20:06:52)

[AIP-140] Prohibit starting any word with a number.
Do not merge until googleapis/aip#528 is merged.


Patch:
diff --git a/docs/rules/0140/numbers.md b/docs/rules/0140/numbers.md
new file mode 100644
index 0000000..5fd0349
--- /dev/null
+++ b/docs/rules/0140/numbers.md
@@ -0,0 +1,75 @@
+---
+rule:
+  aip: 140
+  name: [core, '0140', numbers]
+  summary: Field names should not have words beginning with numbers.
+permalink: /140/numbers
+redirect_from:
+  - /0140/numbers
+---
+
+# Field names: Numbers
+
+This rule enforces that field names do not begin any word in the field with a
+number, as mandated in [AIP-140][].
+
+## Details
+
+This rule checks every field in the proto and complains if any individual word
+begins with a number. It treats the underscore (`_`) character as the only word
+separator for this purpose.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message Book {
+  string name = 1;
+  int32 review_90th_percentile_stars = 2;
+}
+```
+
+**Correct** code for this rule:
+
+The correct code here is likely to vary based on the situation. This may be
+fixed by spelling out the number:
+
+```proto
+// Correct.
+message Book {
+  string name = 1;
+  int32 review_ninetieth_percentile_stars = 2;
+}
+```
+
+Many cases we see involving numbers like this may be better designed with a
+map:
+
+````proto
+// Correct.
+message Book {
+  string name = 1;
+  map<int32, int32> review_stars_per_percentile = 2;
+}
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the method.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+// (-- api-linter: core::0140::numbers=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+message Book {
+  string name = 1;
+  int32 review_90th_percentile_stars = 2;
+}
+````
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-140]: https://aip.dev/140
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0140/aip0140.go b/rules/aip0140/aip0140.go
index 49925e2..0b15088 100644
--- a/rules/aip0140/aip0140.go
+++ b/rules/aip0140/aip0140.go
@@ -32,6 +32,7 @@ func AddRules(r lint.RuleRegistry) error {
 		base64,
 		lowerSnake,
 		noPrepositions,
+		numbers,
 		reservedWords,
 		uri,
 	)
diff --git a/rules/aip0140/numbers.go b/rules/aip0140/numbers.go
new file mode 100644
index 0000000..06b1f21
--- /dev/null
+++ b/rules/aip0140/numbers.go
@@ -0,0 +1,42 @@
+// Copyright 2020 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// 		https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0140
+
+import (
+	"regexp"
+	"strings"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var numbers = &lint.FieldRule{
+	Name: lint.NewRuleName(140, "numbers"),
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		for _, segment := range strings.Split(f.GetName(), "_") {
+			if numberStart.MatchString(segment) {
+				return []lint.Problem{{
+					Message:    "No word in a field name should start with a number.",
+					Descriptor: f,
+					Location:   locations.DescriptorName(f),
+				}}
+			}
+		}
+		return nil
+	},
+}
+
+var numberStart = regexp.MustCompile("^[0-9]")


Test patch:
diff --git a/rules/aip0140/numbers_test.go b/rules/aip0140/numbers_test.go
new file mode 100644
index 0000000..b65c5f6
--- /dev/null
+++ b/rules/aip0140/numbers_test.go
@@ -0,0 +1,47 @@
+// Copyright 2020 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// 		https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0140
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestNumbers(t *testing.T) {
+	for _, test := range []struct {
+		Name     string
+		problems testutils.Problems
+	}{
+		{"foo", nil},
+		{"foo_bar", nil},
+		{"foo_123", testutils.Problems{{Message: "number"}}},
+		{"bar_5foo", testutils.Problems{{Message: "number"}}},
+		// We do not need to test field names that start with a number
+		// (e.g. 123_foo) because it is invalid syntax in protocol buffers.
+	} {
+		t.Run(test.Name, func(t *testing.T) {
+			file := testutils.ParseProto3Tmpl(t, `
+				message Baz {
+					string {{.Name}} = 1;
+				}
+			`, test)
+			field := file.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(numbers.Lint(file)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}