Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Resource references should be strings. #602

Merged
merged 2 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions docs/rules/0122/resource-reference-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
rule:
aip: 122
name: [core, '0122', resource-reference-type]
summary: All resource references must be strings.
permalink: /122/resource-reference-type
redirect_from:
- /0122/resource-reference-type
---

# Resource reference type

This rule enforces that all fields with the `google.api.resource_reference`
annotation are strings, as mandated in [AIP-122][].

## Details

This rule complains if it sees a field with a `google.api.resource_reference`
that has a type other than `string`.

## Examples

**Incorrect** code for this rule:

```proto
// Incorrect.
message Book {
string name = 1;

// Resource references should be strings.
Author author = 2 [(google.api.resource_reference) = {
type: "library.googleapis.com/Author"
}];
}
```

**Correct** code for this rule:

```proto
// Correct.
message Book {
string name = 1;

string author = 2 [(google.api.resource_reference) = {
type: "library.googleapis.com/Author"
}];
}
```

```proto
// Correct.
message Book {
string name = 1;

// If "author" is not a first-class resource, then it may be a composite
// field within the book.
Author author = 2;
}
```

## 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
message Book {
string name = 1;

// (-- api-linter: core::0122::resource-reference-type=disabled
// aip.dev/not-precedent: We need to do this because reasons. --)
Author author = 2 [(google.api.resource_reference) = {
type: "library.googleapis.com/Author"
}];
}
```

If you need to violate this rule for an entire file, place the comment at the
top of the file.

[aip-122]: https://aip.dev/122
[aip.dev/not-precedent]: https://aip.dev/not-precedent
1 change: 1 addition & 0 deletions rules/aip0122/aip0122.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ func AddRules(r lint.RuleRegistry) error {
122,
httpURICase,
nameSuffix,
resourceReferenceType,
)
}
36 changes: 36 additions & 0 deletions rules/aip0122/resource_reference_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 aip0122

import (
"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 resourceReferenceType = &lint.FieldRule{
Name: lint.NewRuleName(122, "resource-reference-type"),
LintField: func(f *desc.FieldDescriptor) []lint.Problem {
if utils.GetResourceReference(f) != nil && utils.GetTypeName(f) != "string" {
return []lint.Problem{{
Message: "The resource_reference annotation should only be used on string fields.",
Descriptor: f,
Location: locations.FieldResourceReference(f),
}}
}
return nil
},
}
53 changes: 53 additions & 0 deletions rules/aip0122/resource_reference_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 aip0122

import (
"testing"

"github.com/googleapis/api-linter/rules/internal/testutils"
)

func TestResourceReferenceType(t *testing.T) {
ann := ` [(google.api.resource_reference) = {
type: "library.googleapis.com/Author"
}]`
for _, test := range []struct {
name string
Type string
Annotation string
problems testutils.Problems
}{
{"ValidString", "string", ann, nil},
{"InvalidMessage", "Author", ann, testutils.Problems{{Message: "string fields"}}},
{"IrrelevantNoAnnotation", "Author", "", nil},
} {
t.Run(test.name, func(t *testing.T) {
f := testutils.ParseProto3Tmpl(t, `
import "google/api/resource.proto";

message Book {
{{.Type}} author = 1{{.Annotation}};

}
message Author {}
`, test)
field := f.GetMessageTypes()[0].GetFields()[0]
if diff := test.problems.SetDescriptor(field).Diff(resourceReferenceType.Lint(f)); diff != "" {
t.Errorf(diff)
}
})
}
}