Repository: googleapis/api-linter
Pull request #378 (2019-11-21 23:23:28)

[AIP-159] Prohibit hard-coded hyphen segments.


Patch:
diff --git a/docs/rules/0159/hardcoded-hyphen.md b/docs/rules/0159/hardcoded-hyphen.md
new file mode 100644
index 0000000..1fda9e3
--- /dev/null
+++ b/docs/rules/0159/hardcoded-hyphen.md
@@ -0,0 +1,64 @@
+---
+rule:
+  aip: 159
+  name: [core, '0159', hardcoded-hyphen]
+  summary: Request URIs must not hard-code a `-` segment.
+permalink: /159/hardcoded-hyphen
+redirect_from:
+  - /0159/hardcoded-hyphen
+---
+
+# Hardcoded hyphen segments
+
+This rule enforces that URIs do not "hard-code" a segment of `-` in their URIs,
+as mandated in [AIP-159][].
+
+## Details
+
+This rule looks at every method and complains if it sees a segment in the URI
+that is just the `-` character and nothing else.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc ListBooks(ListBooksRequest) returns (ListBooksResponse) {
+  option (google.api.http) = {
+    get: "/v1/{parent=publishers/-}/books"  // Should use `*`, not `-`.
+  };
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc ListBooks(ListBooksRequest) returns (ListBooksResponse) {
+  option (google.api.http) = {
+    get: "/v1/{parent=publishers/*}/books"
+  };
+}
+```
+
+## 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::0159::hardcoded-hyphen=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc ListBooks(ListBooksRequest) returns (ListBooksResponse) {
+  option (google.api.http) = {
+    get: "/v1/{parent=publishers/-}/books"
+  };
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-159]: https://aip.dev/159
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/docs/rules/0159/index.md b/docs/rules/0159/index.md
new file mode 100644
index 0000000..6c9dffc
--- /dev/null
+++ b/docs/rules/0159/index.md
@@ -0,0 +1,10 @@
+---
+aip_listing: 159
+permalink: /159/
+redirect_from:
+  - /0159/
+---
+
+# Reading across collections
+
+{% include linter-aip-listing.md aip=159 %}
diff --git a/rules/aip0159/aip0159.go b/rules/aip0159/aip0159.go
new file mode 100644
index 0000000..d539f47
--- /dev/null
+++ b/rules/aip0159/aip0159.go
@@ -0,0 +1,28 @@
+// 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 aip0159 contains rules defined in https://aip.dev/159.
+package aip0159
+
+import (
+	"github.com/googleapis/api-linter/lint"
+)
+
+// AddRules adds all of the AIP-159 rules to the provided registry.
+func AddRules(r lint.RuleRegistry) error {
+	return r.Register(
+		159,
+		hardcodedHyphen,
+	)
+}
diff --git a/rules/aip0159/hardcoded_hyphen.go b/rules/aip0159/hardcoded_hyphen.go
new file mode 100644
index 0000000..bc833ef
--- /dev/null
+++ b/rules/aip0159/hardcoded_hyphen.go
@@ -0,0 +1,40 @@
+// 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 aip0159
+
+import (
+	"strings"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var hardcodedHyphen = &lint.MethodRule{
+	Name: lint.NewRuleName(159, "hardcoded-hyphen"),
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		for _, http := range utils.GetHTTPRules(m) {
+			if strings.Contains(http.GetPlainURI(), "/-/") {
+				return []lint.Problem{{
+					Message:    "URIs must not hard-code a `-` segment.",
+					Descriptor: m,
+					Location:   locations.MethodHTTPRule(m),
+				}}
+			}
+		}
+		return nil
+	},
+}
diff --git a/rules/rules.go b/rules/rules.go
index aab8ba0..bc98b15 100644
--- a/rules/rules.go
+++ b/rules/rules.go
@@ -67,6 +67,7 @@ import (
 	"github.com/googleapis/api-linter/rules/aip0151"
 	"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/aip0191"
 	"github.com/googleapis/api-linter/rules/aip0192"
 	"github.com/googleapis/api-linter/rules/aip0203"
@@ -95,6 +96,7 @@ var aipAddRulesFuncs = []addRulesFuncType{
 	aip0151.AddRules,
 	aip0156.AddRules,
 	aip0158.AddRules,
+	aip0159.AddRules,
 	aip0191.AddRules,
 	aip0192.AddRules,
 	aip0203.AddRules,


Test patch:
diff --git a/rules/aip0159/aip0159_test.go b/rules/aip0159/aip0159_test.go
new file mode 100644
index 0000000..3a68324
--- /dev/null
+++ b/rules/aip0159/aip0159_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 aip0159
+
+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/aip0159/hardcoded_hyphen_test.go b/rules/aip0159/hardcoded_hyphen_test.go
new file mode 100644
index 0000000..5cec68e
--- /dev/null
+++ b/rules/aip0159/hardcoded_hyphen_test.go
@@ -0,0 +1,49 @@
+// 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 aip0159
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestHardcodedHyphen(t *testing.T) {
+	for _, test := range []struct {
+		name     string
+		Parent   string
+		problems testutils.Problems
+	}{
+		{"Valid", "publishers/*", testutils.Problems{}},
+		{"Invalid", "publishers/-", testutils.Problems{{Message: "`-`"}}},
+	} {
+		f := testutils.ParseProto3Tmpl(t, `
+			import "google/api/annotations.proto";
+			service Library {
+				rpc ListBooks(ListBooksRequest) returns (ListBooksResponse) {
+					option (google.api.http) = {
+						get: "/v1/{parent={{.Parent}}}/books"
+					};
+				}
+			}
+			message ListBooksRequest {}
+			message ListBooksResponse {}
+		`, test)
+		method := f.GetServices()[0].GetMethods()[0]
+		if diff := test.problems.SetDescriptor(method).Diff(hardcodedHyphen.Lint(f)); diff != "" {
+			t.Errorf(diff)
+		}
+	}
+}