Repository: googleapis/api-linter
Pull request #656 (2020-10-10 20:02:51)

feat: Add rule for `given_name` and `family_name`.
As opposed to `first_name` and `last_name`.


Patch:
diff --git a/docs/rules/0148/human-names.md b/docs/rules/0148/human-names.md
new file mode 100644
index 0000000..4770faf
--- /dev/null
+++ b/docs/rules/0148/human-names.md
@@ -0,0 +1,63 @@
+---
+rule:
+  aip: 148
+  name: [core, '0148', human-names]
+  summary: Avoid imprecise terms for human names.
+permalink: /148/standardized-codes
+redirect_from:
+  - /0148/standardized-codes
+---
+
+# Human names
+
+This rule encourages terms for human names (`given_name` and `family_name`)
+that are more accurate across cultures, as mandated in [AIP-148][].
+
+## Details
+
+This rule looks for fields named `first_name` and `last_name`, and complains if
+it finds them, suggesting the use of `given_name` and `family_name`
+(respectively) instead.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message Human {
+  string first_name = 1;  // Should be `given_name`.
+  string last_name = 2;   // Should be `family_name`
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message Human {
+  string given_name = 1;
+  string family_name = 2;
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the field or its
+enclosing message. Remember to also include an [aip.dev/not-precedent][]
+comment explaining why.
+
+```proto
+// (-- api-linter: core::0148::human-names=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+message Human {
+  string first_name = 1;
+  string last_name = 2;
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-148]: https://aip.dev/148
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/docs/rules/0148/index.md b/docs/rules/0148/index.md
new file mode 100644
index 0000000..48e0b6f
--- /dev/null
+++ b/docs/rules/0148/index.md
@@ -0,0 +1,10 @@
+---
+aip_listing: 148
+permalink: /148/
+redirect_from:
+  - /0148/
+---
+
+# Standard fields
+
+{% include linter-aip-listing.md aip=148 %}
diff --git a/rules/aip0148/aip0148.go b/rules/aip0148/aip0148.go
new file mode 100644
index 0000000..1fa06bf
--- /dev/null
+++ b/rules/aip0148/aip0148.go
@@ -0,0 +1,28 @@
+// 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 aip0148 contains rules defined in https://aip.dev/148.
+package aip0148
+
+import (
+	"github.com/googleapis/api-linter/lint"
+)
+
+// AddRules adds all of the AIP-148 rules to the provided registry.
+func AddRules(r lint.RuleRegistry) error {
+	return r.Register(
+		148,
+		humanNames,
+	)
+}
diff --git a/rules/aip0148/human_names.go b/rules/aip0148/human_names.go
new file mode 100644
index 0000000..e1105ad
--- /dev/null
+++ b/rules/aip0148/human_names.go
@@ -0,0 +1,45 @@
+// 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 aip0148
+
+import (
+	"fmt"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var humanNames = &lint.FieldRule{
+	Name: lint.NewRuleName(148, "human-names"),
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		for got, want := range corrections {
+			if f.GetName() == got {
+				return []lint.Problem{{
+					Message:    fmt.Sprintf("Use %s instead of %s.", want, got),
+					Descriptor: f,
+					Location:   locations.DescriptorName(f),
+					Suggestion: want,
+				}}
+			}
+		}
+		return nil
+	},
+}
+
+var corrections = map[string]string{
+	"first_name": "given_name",
+	"last_name":  "family_name",
+}
diff --git a/rules/rules.go b/rules/rules.go
index 9747be7..2f822a9 100644
--- a/rules/rules.go
+++ b/rules/rules.go
@@ -67,6 +67,7 @@ import (
 	"github.com/googleapis/api-linter/rules/aip0142"
 	"github.com/googleapis/api-linter/rules/aip0143"
 	"github.com/googleapis/api-linter/rules/aip0146"
+	"github.com/googleapis/api-linter/rules/aip0148"
 	"github.com/googleapis/api-linter/rules/aip0151"
 	"github.com/googleapis/api-linter/rules/aip0154"
 	"github.com/googleapis/api-linter/rules/aip0156"
@@ -105,6 +106,7 @@ var aipAddRulesFuncs = []addRulesFuncType{
 	aip0142.AddRules,
 	aip0143.AddRules,
 	aip0146.AddRules,
+	aip0148.AddRules,
 	aip0151.AddRules,
 	aip0154.AddRules,
 	aip0156.AddRules,


Test patch:
diff --git a/rules/aip0148/aip0148_test.go b/rules/aip0148/aip0148_test.go
new file mode 100644
index 0000000..3e76b2a
--- /dev/null
+++ b/rules/aip0148/aip0148_test.go
@@ -0,0 +1,27 @@
+// Copyright 2019 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 aip0148
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/lint"
+)
+
+func TestAddRules(t *testing.T) {
+	if err := AddRules(lint.NewRuleRegistry()); err != nil {
+		t.Errorf("AddRules got an error: %v", err)
+	}
+}
diff --git a/rules/aip0148/human_names_test.go b/rules/aip0148/human_names_test.go
new file mode 100644
index 0000000..1793ed5
--- /dev/null
+++ b/rules/aip0148/human_names_test.go
@@ -0,0 +1,45 @@
+// 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 aip0148
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestHumanNames(t *testing.T) {
+	for _, test := range []struct {
+		FieldName string
+		problems  testutils.Problems
+	}{
+		{"given_name", nil},
+		{"family_name", nil},
+		{"first_name", testutils.Problems{{Suggestion: "given_name"}}},
+		{"last_name", testutils.Problems{{Suggestion: "family_name"}}},
+	} {
+		t.Run(test.FieldName, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				message Person {
+					string {{.FieldName}} = 1;
+				}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(humanNames.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}