Repository: googleapis/api-linter
Pull request #545 (2020-05-29 19:28:04)

[AIP-140] Prohibit url in field names.
Do not merge until googleapis/aip#519 is merged.


Patch:
diff --git a/docs/rules/0140/uri.md b/docs/rules/0140/uri.md
new file mode 100644
index 0000000..9856a0a
--- /dev/null
+++ b/docs/rules/0140/uri.md
@@ -0,0 +1,58 @@
+---
+rule:
+  aip: 140
+  name: [core, '0140', uri]
+  summary: Field names should prefer `uri` to `url`.
+permalink: /140/uri
+redirect_from:
+  - /0140/uri
+---
+
+# Field names: URIs
+
+This rule enforces that field names use `uri` rather than `url`, as mandated in
+[AIP-140][].
+
+## Details
+
+This rule checks every field in the proto and complains if the field name
+includes `url`. (It splits on `_` to avoid false positives.)
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message Book {
+  string url = 1;  // Should be `uri`.
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message Book {
+  string uri = 1;
+}
+```
+
+## 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::uri=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+message Book {
+  string url = 1;
+}
+```
+
+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 432ae5d..49925e2 100644
--- a/rules/aip0140/aip0140.go
+++ b/rules/aip0140/aip0140.go
@@ -33,6 +33,7 @@ func AddRules(r lint.RuleRegistry) error {
 		lowerSnake,
 		noPrepositions,
 		reservedWords,
+		uri,
 	)
 }
 
diff --git a/rules/aip0140/uri.go b/rules/aip0140/uri.go
new file mode 100644
index 0000000..908be9b
--- /dev/null
+++ b/rules/aip0140/uri.go
@@ -0,0 +1,40 @@
+// 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 (
+	"strings"
+
+	"bitbucket.org/creachadair/stringset"
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var uri = &lint.FieldRule{
+	Name: lint.NewRuleName(140, "uri"),
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		nameSegments := stringset.New(strings.Split(f.GetName(), "_")...)
+		if nameSegments.Contains("url") {
+			return []lint.Problem{{
+				Message:    "Use `uri` instead of `url` in field names.",
+				Descriptor: f,
+				Location:   locations.DescriptorName(f),
+				Suggestion: strings.ReplaceAll(f.GetName(), "url", "uri"),
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0140/uri_test.go b/rules/aip0140/uri_test.go
new file mode 100644
index 0000000..cd8b558
--- /dev/null
+++ b/rules/aip0140/uri_test.go
@@ -0,0 +1,50 @@
+// 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 TestURI(t *testing.T) {
+	for _, test := range []struct {
+		name     string
+		Field    string
+		problems testutils.Problems
+	}{
+		{"Valid", "uri", nil},
+		{"ValidPrefix", "uri_foo", nil},
+		{"ValidSuffix", "foo_uri", nil},
+		{"ValidIntermixed", "foo_uri_bar", nil},
+		{"Invalid", "url", testutils.Problems{{Message: "uri", Suggestion: "uri"}}},
+		{"InvalidPrefix", "url_foo", testutils.Problems{{Message: "uri", Suggestion: "uri_foo"}}},
+		{"InvalidSuffix", "foo_url", testutils.Problems{{Message: "uri", Suggestion: "foo_uri"}}},
+		{"InvalidIntermixed", "foo_url_bar", testutils.Problems{{Message: "uri", Suggestion: "foo_uri_bar"}}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				message Foo {
+					string {{.Field}} = 1;
+				}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(uri.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}