Repository: googleapis/api-linter
Pull request #414 (2019-12-04 23:43:10)

[feat] Add GetResource and GetResourceReference methods.
AIP-123 rules using these are forthcoming.


Patch:
diff --git a/rules/internal/utils/extension.go b/rules/internal/utils/extension.go
index 1a10d0a..8e214aa 100644
--- a/rules/internal/utils/extension.go
+++ b/rules/internal/utils/extension.go
@@ -36,7 +36,7 @@ func GetFieldBehavior(f *desc.FieldDescriptor) stringset.Set {
 	return nil
 }
 
-// GetOperationInfo returns the LRO annotation.
+// GetOperationInfo returns the google.longrunning.operation_info annotation.
 func GetOperationInfo(m *desc.MethodDescriptor) *lrpb.OperationInfo {
 	opts := m.GetMethodOptions()
 	if x, err := proto.GetExtension(opts, lrpb.E_OperationInfo); err == nil {
@@ -44,3 +44,21 @@ func GetOperationInfo(m *desc.MethodDescriptor) *lrpb.OperationInfo {
 	}
 	return nil
 }
+
+// GetResource returns the google.api.resource annotation.
+func GetResource(m *desc.MessageDescriptor) *apb.ResourceDescriptor {
+	opts := m.GetMessageOptions()
+	if x, err := proto.GetExtension(opts, apb.E_Resource); err == nil {
+		return x.(*apb.ResourceDescriptor)
+	}
+	return nil
+}
+
+// GetResourceReference returns the google.api.resource_reference annotation.
+func GetResourceReference(f *desc.FieldDescriptor) *apb.ResourceReference {
+	opts := f.GetFieldOptions()
+	if x, err := proto.GetExtension(opts, apb.E_ResourceReference); err == nil {
+		return x.(*apb.ResourceReference)
+	}
+	return nil
+}


Test patch:
diff --git a/rules/internal/utils/extension_test.go b/rules/internal/utils/extension_test.go
index 0d08972..9840801 100644
--- a/rules/internal/utils/extension_test.go
+++ b/rules/internal/utils/extension_test.go
@@ -90,3 +90,53 @@ func TestGetOperationInfoNone(t *testing.T) {
 		t.Errorf("Got %v, expected nil LRO annotation.", lro)
 	}
 }
+
+func TestGetResource(t *testing.T) {
+	t.Run("Present", func(t *testing.T) {
+		f := testutils.ParseProto3String(t, `
+			import "google/api/resource.proto";
+			message Book {
+				option (google.api.resource) = {
+					type: "library.googleapis.com/Book"
+					pattern: "publishers/{publisher}/books/{book}"
+				};
+			}
+		`)
+		resource := GetResource(f.GetMessageTypes()[0])
+		if got, want := resource.GetType(), "library.googleapis.com/Book"; got != want {
+			t.Errorf("Got %q, expected %q.", got, want)
+		}
+		if got, want := resource.GetPattern()[0], "publishers/{publisher}/books/{book}"; got != want {
+			t.Errorf("Got %q, expected %q.", got, want)
+		}
+	})
+	t.Run("Absent", func(t *testing.T) {
+		f := testutils.ParseProto3String(t, "message Book {}")
+		if got := GetResource(f.GetMessageTypes()[0]); got != nil {
+			t.Errorf(`Got "%v", expected nil`, got)
+		}
+	})
+}
+
+func TestGetResourceReference(t *testing.T) {
+	t.Run("Present", func(t *testing.T) {
+		f := testutils.ParseProto3String(t, `
+			import "google/api/resource.proto";
+			message GetBookRequest {
+				string name = 1 [(google.api.resource_reference) = {
+					type: "library.googleapis.com/Book"
+				}];
+			}
+		`)
+		ref := GetResourceReference(f.GetMessageTypes()[0].GetFields()[0])
+		if got, want := ref.GetType(), "library.googleapis.com/Book"; got != want {
+			t.Errorf("Got %q, expected %q.", got, want)
+		}
+	})
+	t.Run("Absent", func(t *testing.T) {
+		f := testutils.ParseProto3String(t, "message GetBookRequest { string name = 1; }")
+		if got := GetResourceReference(f.GetMessageTypes()[0].GetFields()[0]); got != nil {
+			t.Errorf(`Got "%v", expected nil`, got)
+		}
+	})
+}