Repository: googleapis/api-linter
Pull request #470 (2020-01-26 17:10:19)

[AIP-142] Fix incorrect flagging of civil dates and times (#463)

googlebot: 
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: **Please visit <https://cla.developers.google.com/> to sign.**

Once you've signed (or fixed any issues), please reply here with `@googlebot I signed it!` and we'll verify it.

----

#### What to do if you already signed the CLA

##### Individual signers

*   It's possible we don't have your GitHub username or you're using a different email address on your commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).

##### Corporate signers

*   Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the Google project maintainer to [go/cla#troubleshoot](http://go/cla#troubleshoot) ([Public version](https://opensource.google/docs/cla/#troubleshoot)).
*   The email used to register you as an authorized contributor must be the email used for the Git commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).
*   The email used to register you as an authorized contributor must also be [attached to your GitHub account](https://github.com/settings/emails).
		

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2Fgoogleapis%2Fapi-linter%2Fpull%2F470) for more info**.

<!-- need_sender_cla -->
odsod: ETA on CLA is ~24h. @lukesneeringer what do you think about the change?
jgeewax: @odsod : Can you make sure the SLA issue is resolved?
odsod: @jgeewax Thanks for reviewing! Yes, I'm on it.
odsod: @googlebot I signed it!
odsod: @googlebot I signed it!

The CLA is signed by my CTO and the contributors group is google-contributors@einride.tech - I'm a member there with the email oscar.soderlund@einride.tech - I amended my original commit to use that email as author.

Do you need anything more from me to ensure the CLA is covered?
odsod: @googlebot I signed it!
googlebot: CLAs look good, thanks!

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2Fgoogleapis%2Fapi-linter%2Fpull%2F470) for more info**.

<!-- ok -->

Patch:
diff --git a/rules/aip0142/time_field_type.go b/rules/aip0142/time_field_type.go
index 64e559b..adfdaaa 100644
--- a/rules/aip0142/time_field_type.go
+++ b/rules/aip0142/time_field_type.go
@@ -27,13 +27,21 @@ import (
 var fieldType = &lint.FieldRule{
 	Name: lint.NewRuleName(142, "time-field-type"),
 	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		tokens := strings.Split(f.GetName(), "_")
+		suffix := tokens[len(tokens)-1]
+		typeName := utils.GetTypeName(f)
 		suffixes := stringset.New(
 			"date", "datetime", "ms", "msec", "msecs", "millis", "nanos", "ns",
 			"nsec", "nsecs", "sec", "secs", "seconds", "time", "timestamp", "us",
 			"usec", "usecs",
 		)
-		tokens := strings.Split(f.GetName(), "_")
-		if suffixes.Contains(tokens[len(tokens)-1]) && utils.GetTypeName(f) != "google.protobuf.Timestamp" {
+		allowed := stringset.New(
+			"google.protobuf.Timestamp",
+			"google.type.Date",
+			"google.type.DateTime",
+			"google.type.TimeOfDay",
+		)
+		if suffixes.Contains(suffix) && !allowed.Contains(typeName) {
 			return []lint.Problem{{
 				Message:    "Fields representing timestamps should use `google.protobuf.Timestamp`.",
 				Suggestion: "google.protobuf.Timestamp",


Test patch:
diff --git a/rules/aip0142/time_field_type_test.go b/rules/aip0142/time_field_type_test.go
index 53c1743..131b458 100644
--- a/rules/aip0142/time_field_type_test.go
+++ b/rules/aip0142/time_field_type_test.go
@@ -28,12 +28,18 @@ func TestFieldType(t *testing.T) {
 		problems  testutils.Problems
 	}{
 		{"Valid", "google.protobuf.Timestamp", "create_time", testutils.Problems{}},
+		{"Valid Date", "google.type.Date", "birth_date", testutils.Problems{}},
+		{"Valid Civil Time", "google.type.TimeOfDay", "civil_time", testutils.Problems{}},
+		{"Valid DateTime", "google.type.DateTime", "local_time", testutils.Problems{}},
 		{"Invalid", "int32", "created_ms", testutils.Problems{{Suggestion: "google.protobuf.Timestamp"}}},
 		{"Irrelevant", "int32", "foo", testutils.Problems{}},
 	}
 	for _, test := range tests {
 		t.Run(test.testName, func(t *testing.T) {
 			file := testutils.ParseProto3Tmpl(t, `
+				import "google/type/date.proto";
+				import "google/type/datetime.proto";
+				import "google/type/timeofday.proto";
 				import "google/protobuf/timestamp.proto";
 				message Book {
 					{{.FieldType}} {{.FieldName}} = 1;
diff --git a/rules/internal/testutils/parse.go b/rules/internal/testutils/parse.go
index 03bcca8..fe91818 100644
--- a/rules/internal/testutils/parse.go
+++ b/rules/internal/testutils/parse.go
@@ -30,6 +30,9 @@ import (
 	// `proto.FileDescriptor` work for the imported files.
 	_ "google.golang.org/genproto/googleapis/api/annotations"
 	_ "google.golang.org/genproto/googleapis/longrunning"
+	_ "google.golang.org/genproto/googleapis/type/date"
+	_ "google.golang.org/genproto/googleapis/type/datetime"
+	_ "google.golang.org/genproto/googleapis/type/timeofday"
 )
 
 // ParseProtoStrings parses a map representing a proto files, and returns