Skip to content

Commit

Permalink
feat: Add rule for AIP-162 Delete Revision request name field behavior (
Browse files Browse the repository at this point in the history
  • Loading branch information
apasel422 committed Feb 17, 2021
1 parent f27c257 commit 6061a0a
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
69 changes: 69 additions & 0 deletions docs/rules/0162/delete-revision-request-name-behavior.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
rule:
aip: 162
name: [core, '0162', delete-revision-request-name-behavior]
summary: |
Delete Revision requests should annotate the `name` field with `google.api.field_behavior`.
permalink: /162/delete-revision-request-name-behavior
redirect_from:
- /0162/delete-revision-request-name-behavior
---

# Delete Revision requests: Name field behavior

This rule enforces that all Delete Revision requests have
`google.api.field_behavior` set to `REQUIRED` on their `string name` field, as
mandated in [AIP-162][].

## Details

This rule looks at any message matching `Delete*RevisionRequest` and complains if the
`name` field does not have a `google.api.field_behavior` annotation with a
value of `REQUIRED`.

## Examples

**Incorrect** code for this rule:

```proto
// Incorrect.
message DeleteBookRevisionRequest {
// The `google.api.field_behavior` annotation should also be included.
string name = 1 [
(google.api.resource_reference).type = "library.googleapis.com/Book"
];
}
```

**Correct** code for this rule:

```proto
// Correct.
message DeleteBookRevisionRequest {
string name = 1 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference).type = "library.googleapis.com/Book"
];
}
```

## Disabling

If you need to violate this rule, use a leading comment above the field.
Remember to also include an [aip.dev/not-precedent][] comment explaining why.

```proto
message DeleteBookRevisionRequest {
// (-- api-linter: core::0162::delete-revision-request-name-behavior=disabled
// aip.dev/not-precedent: We need to do this because reasons. --)
string name = 1 [
(google.api.resource_reference).type = "library.googleapis.com/Book"
];
}
```

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

[aip-162]: https://aip.dev/162
[aip.dev/not-precedent]: https://aip.dev/not-precedent
1 change: 1 addition & 0 deletions rules/aip0162/aip0162.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func AddRules(r lint.RuleRegistry) error {
deleteRevisionHTTPMethod,
deleteRevisionHTTPURISuffix,
deleteRevisionRequestMessageName,
deleteRevisionRequestNameBehavior,
deleteRevisionRequestNameField,
rollbackHTTPBody,
rollbackHTTPMethod,
Expand Down
29 changes: 29 additions & 0 deletions rules/aip0162/delete_revision_request_name_behavior.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021 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 aip0162

import (
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
)

var deleteRevisionRequestNameBehavior = &lint.FieldRule{
Name: lint.NewRuleName(162, "delete-revision-request-name-behavior"),
OnlyIf: func(f *desc.FieldDescriptor) bool {
return isDeleteRevisionRequestMessage(f.GetOwner()) && f.GetName() == "name"
},
LintField: utils.LintRequiredField,
}
49 changes: 49 additions & 0 deletions rules/aip0162/delete_revision_request_name_behavior_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2021 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 aip0162

import (
"testing"

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

func TestDeleteRevisionRequestNameBehavior(t *testing.T) {
for _, test := range []struct {
name string
RPC string
Field string
FieldOpts string
problems testutils.Problems
}{
{"Valid", "DeleteBookRevision", "name", " [(google.api.field_behavior) = REQUIRED]", nil},
{"Missing", "DeleteBookRevision", "name", "", testutils.Problems{{Message: "(google.api.field_behavior) = REQUIRED"}}},
{"IrrelevantMessage", "PurgeBooks", "name", "", nil},
{"IrrelevantField", "DeleteBookRevision", "something_else", "", nil},
} {
t.Run(test.name, func(t *testing.T) {
f := testutils.ParseProto3Tmpl(t, `
import "google/api/field_behavior.proto";
message {{.RPC}}Request {
string {{.Field}} = 1{{.FieldOpts}};
}
`, test)
field := f.GetMessageTypes()[0].GetFields()[0]
if diff := test.problems.SetDescriptor(field).Diff(deleteRevisionRequestNameBehavior.Lint(f)); diff != "" {
t.Errorf(diff)
}
})
}
}

0 comments on commit 6061a0a

Please sign in to comment.