Repository: googleapis/api-linter
Pull request #394 (2019-11-26 21:47:04)

[AIP-214] Enforce `ttl` type.
Uses #393 as a base.


Patch:
diff --git a/docs/rules/0214/ttl-type.md b/docs/rules/0214/ttl-type.md
new file mode 100644
index 0000000..1449d0a
--- /dev/null
+++ b/docs/rules/0214/ttl-type.md
@@ -0,0 +1,70 @@
+---
+rule:
+  aip: 214
+  name: [core, '0214', ttl-type]
+  summary: The `ttl` field should be a `google.protobuf.Duration`.
+permalink: /214/ttl-type
+redirect_from:
+  - /0214/ttl-type
+---
+
+# Resource expiry
+
+This rule enforces that `ttl` fields use `google.protobuf.Duration`, as
+mandated in [AIP-214][].
+
+## Details
+
+This rule looks at fields named `ttl`, and complains if they are a type other
+than `google.protobuf.Duration`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message Book {
+  string name = 1;
+  oneof expiration {
+    google.protobuf.Timestamp expire_time = 2;
+    int32 ttl = 3;  // Should be `google.protobuf.Duration`.
+  }
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message Book {
+  string name = 1;
+  oneof expiration {
+    google.protobuf.Timestamp expire_time = 2;
+    google.protobuf.Duration ttl = 3;
+  }
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the enum.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+message Book {
+  string name = 1;
+  oneof expiration {
+    google.protobuf.Timestamp expire_time = 2;
+    // (-- api-linter: core::0214::ttl-type=disabled
+    //     aip.dev/not-precedent: We need to do this because reasons. --)
+    int32 ttl = 3;  // Should be `google.protobuf.Duration`.
+  }
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-214]: https://aip.dev/214
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0214/aip0214.go b/rules/aip0214/aip0214.go
index a0b2cf3..3b51249 100644
--- a/rules/aip0214/aip0214.go
+++ b/rules/aip0214/aip0214.go
@@ -24,5 +24,6 @@ func AddRules(r lint.RuleRegistry) error {
 	return r.Register(
 		214,
 		resourceExpiry,
+		ttlType,
 	)
 }
diff --git a/rules/aip0214/ttl_type.go b/rules/aip0214/ttl_type.go
new file mode 100644
index 0000000..19a036d
--- /dev/null
+++ b/rules/aip0214/ttl_type.go
@@ -0,0 +1,42 @@
+// 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 aip0214
+
+import (
+	"fmt"
+
+	"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 ttlType = &lint.FieldRule{
+	Name: lint.NewRuleName(214, "ttl-type"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return f.GetName() == "ttl"
+	},
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		if fieldType := utils.GetTypeName(f); fieldType != "google.protobuf.Duration" {
+			return []lint.Problem{{
+				Message:    fmt.Sprintf("ttl fields should be `google.protobuf.Duration` type, not `%s`.", fieldType),
+				Suggestion: "google.protobuf.Duration",
+				Descriptor: f,
+				Location:   locations.FieldType(f),
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0214/ttl_type_test.go b/rules/aip0214/ttl_type_test.go
new file mode 100644
index 0000000..3830008
--- /dev/null
+++ b/rules/aip0214/ttl_type_test.go
@@ -0,0 +1,44 @@
+// 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 aip0214
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestTtlType(t *testing.T) {
+	for _, test := range []struct {
+		name     string
+		Type     string
+		problems testutils.Problems
+	}{
+		{"Valid", "google.protobuf.Duration", testutils.Problems{}},
+		{"Invalid", "int32", testutils.Problems{{Suggestion: "google.protobuf.Duration"}}},
+	} {
+		f := testutils.ParseProto3Tmpl(t, `
+			import "google/protobuf/duration.proto";
+			message Book {
+				string name = 1;
+				{{.Type}} ttl = 2;
+			}
+		`, test)
+		field := f.GetMessageTypes()[0].GetFields()[1]
+		if diff := test.problems.SetDescriptor(field).Diff(ttlType.Lint(f)); diff != "" {
+			t.Errorf(diff)
+		}
+	}
+}