Repository: googleapis/api-linter
Pull request #561 (2020-06-15 18:28:51)

[AIP-163] Add rule for `dry_run` -> `validate_only`.
This is a common and easy-to-catch mistake.


Patch:
diff --git a/docs/rules/0163/index.md b/docs/rules/0163/index.md
new file mode 100644
index 0000000..2a7cc6b
--- /dev/null
+++ b/docs/rules/0163/index.md
@@ -0,0 +1,10 @@
+---
+aip_listing: 163
+permalink: /163/
+redirect_from:
+  - /0163/
+---
+
+# Change validation
+
+{% include linter-aip-listing.md aip=163 %}
diff --git a/docs/rules/0163/synonyms.md b/docs/rules/0163/synonyms.md
new file mode 100644
index 0000000..15c5616
--- /dev/null
+++ b/docs/rules/0163/synonyms.md
@@ -0,0 +1,64 @@
+---
+rule:
+  aip: 163
+  name: [core, '0163', synonyms]
+  summary: Change validation fields should be named `validate_only`.
+permalink: /163/synonyms
+redirect_from:
+  - /0163/synonyms
+---
+
+# Synonyms
+
+This rule enforces that the `validate_only` field is named `validate_only`, and
+not a common synonym, as mandated in [AIP-163][].
+
+## Details
+
+This rule complains if it encounters a known synonym to `validate_only`.
+Currently, the only recognized synonym `dry_run`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message CreateBookRequest {
+  string parent = 1;
+  Book book = 2;
+  bool dry_run = 3;  // Should be `validate_only`.
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message CreateBookRequest {
+  string parent = 1;
+  Book book = 2;
+  bool validate_only = 3;
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the field.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+message CreateBookRequest {
+  string parent = 1;
+  Book book = 2;
+  // (-- api-linter: core::0163::synonyms=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  bool dry_run = 3;
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-163]: https://aip.dev/163
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0163/aip0163.go b/rules/aip0163/aip0163.go
new file mode 100644
index 0000000..0499b36
--- /dev/null
+++ b/rules/aip0163/aip0163.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 aip0163 contains rules defined in https://aip.dev/163.
+package aip0163
+
+import (
+	"github.com/googleapis/api-linter/lint"
+)
+
+// AddRules adds all of the AIP-163 rules to the provided registry.
+func AddRules(r lint.RuleRegistry) error {
+	return r.Register(
+		163,
+		synonyms,
+	)
+}
diff --git a/rules/aip0163/synonyms.go b/rules/aip0163/synonyms.go
new file mode 100644
index 0000000..78207ad
--- /dev/null
+++ b/rules/aip0163/synonyms.go
@@ -0,0 +1,36 @@
+// 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 aip0163
+
+import (
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var synonyms = &lint.FieldRule{
+	Name: lint.NewRuleName(163, "synonyms"),
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		if f.GetName() == "dry_run" {
+			return []lint.Problem{{
+				Message:    `Prefer "validate_only" over "dry_run".`,
+				Suggestion: "validate_only",
+				Descriptor: f,
+				Location:   locations.DescriptorName(f),
+			}}
+		}
+		return nil
+	},
+}
diff --git a/rules/rules.go b/rules/rules.go
index fdd17bb..be485c5 100644
--- a/rules/rules.go
+++ b/rules/rules.go
@@ -70,6 +70,7 @@ import (
 	"github.com/googleapis/api-linter/rules/aip0156"
 	"github.com/googleapis/api-linter/rules/aip0158"
 	"github.com/googleapis/api-linter/rules/aip0159"
+	"github.com/googleapis/api-linter/rules/aip0163"
 	"github.com/googleapis/api-linter/rules/aip0191"
 	"github.com/googleapis/api-linter/rules/aip0192"
 	"github.com/googleapis/api-linter/rules/aip0203"
@@ -104,6 +105,7 @@ var aipAddRulesFuncs = []addRulesFuncType{
 	aip0156.AddRules,
 	aip0158.AddRules,
 	aip0159.AddRules,
+	aip0163.AddRules,
 	aip0191.AddRules,
 	aip0192.AddRules,
 	aip0203.AddRules,


Test patch:
diff --git a/rules/aip0163/aip0163_test.go b/rules/aip0163/aip0163_test.go
new file mode 100644
index 0000000..87e83b6
--- /dev/null
+++ b/rules/aip0163/aip0163_test.go
@@ -0,0 +1,27 @@
+// 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 aip0163
+
+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/aip0163/synonyms_test.go b/rules/aip0163/synonyms_test.go
new file mode 100644
index 0000000..83e448c
--- /dev/null
+++ b/rules/aip0163/synonyms_test.go
@@ -0,0 +1,43 @@
+// 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 aip0163
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestSynonyms(t *testing.T) {
+	for _, test := range []struct {
+		Name     string
+		problems testutils.Problems
+	}{
+		{"validate_only", nil},
+		{"dry_run", testutils.Problems{{Suggestion: "validate_only"}}},
+	} {
+		t.Run(test.Name, func(t *testing.T) {
+			file := testutils.ParseProto3Tmpl(t, `
+				message Foo {
+					bool {{.Name}} = 1;
+				}
+			`, test)
+			field := file.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(synonyms.Lint(file)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}