Skip to content

Commit

Permalink
feat(AIP-148): add uid-format rule (#1270)
Browse files Browse the repository at this point in the history
As mandated in AIP-148, the `uid` field **must** have `(google.api.field_info).format = UUID4`. This rule enforces that.

Also fix incorrect doc title for `use-uid` rule doc.
  • Loading branch information
noahdietz authored Oct 18, 2023
1 parent 2f2e34b commit 79cd201
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 1 deletion.
79 changes: 79 additions & 0 deletions docs/rules/0148/uid-format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
rule:
aip: 148
name: [core, '0148', uid-format]
summary: Annotate uid with UUID4 format.
permalink: /148/uid-format
redirect_from:
- /0148/uid-format
---

# `uid` format annotation

This rule encourages the use of the `UUID4` format annotation on the `uid`
field, as mandated in [AIP-148][].

## Details

This rule looks on for fields named `uid` and complains if it does not have the
`(google.api.field_info).format = UUID4` annotation or has a format other than
`UUID4`.

## Examples

**Incorrect** code for this rule:

```proto
// Incorrect.
message Book {
option (google.api.resource) = {
type: "library.googleapis.com/Book"
pattern: "books/{book}"
};
string name = 1 [(google.api.field_behavior) = IDENTIFIER];
string uid = 2; // missing (google.api.field_info).format = UUID4
}
```

**Correct** code for this rule:

```proto
// Correct.
message Book {
option (google.api.resource) = {
type: "library.googleapis.com/Book"
pattern: "books/{book}"
};
string name = 1 [(google.api.field_behavior) = IDENTIFIER];
string uid = 2 [(google.api.field_info).format = UUID4];
}
```

## Disabling

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

```proto
message Book {
option (google.api.resource) = {
type: "library.googleapis.com/Book"
pattern: "books/{book}"
};
string name = 1 [(google.api.field_behavior) = IDENTIFIER];
// (-- api-linter: core::0148::uid-format=disabled
// aip.dev/not-precedent: We need to do this because reasons. --)
string uid = 2;
}
```

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

[aip-148]: https://aip.dev/148
[aip.dev/not-precedent]: https://aip.dev/not-precedent
2 changes: 1 addition & 1 deletion docs/rules/0148/use-uid.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ redirect_from:
- /0148/use-uid
---

# Human names
# Use `uid` as the resource ID field

This rule encourages the use of `uid` instead of `id` for resource messages, as
mandated in [AIP-148][].
Expand Down
1 change: 1 addition & 0 deletions rules/aip0148/aip0148.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ func AddRules(r lint.RuleRegistry) error {
fieldBehavior,
humanNames,
useUid,
uidFormat,
)
}
40 changes: 40 additions & 0 deletions rules/aip0148/uid_format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2023 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 aip0148

import (
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/types/descriptorpb"
)

var uidFormat = &lint.FieldRule{
Name: lint.NewRuleName(148, "uid-format"),
OnlyIf: func(fd *desc.FieldDescriptor) bool {
return fd.GetType() == descriptorpb.FieldDescriptorProto_TYPE_STRING && fd.GetName() == uidStr
},
LintField: func(fd *desc.FieldDescriptor) []lint.Problem {
if !utils.HasFormat(fd) || utils.GetFormat(fd) != annotations.FieldInfo_UUID4 {
return []lint.Problem{{
Message: "The `uid` field must have a `(google.api.field_info).format = UUID4` annotation.",
Descriptor: fd,
}}
}

return nil
},
}
74 changes: 74 additions & 0 deletions rules/aip0148/uid_format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2023 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 aip0148

import (
"testing"

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

func TestUidFormat(t *testing.T) {
for _, test := range []struct {
name, Annotation, FieldName, Type string
problems testutils.Problems
}{
{
name: "ValidUidFormat",
FieldName: "uid",
Annotation: "[(google.api.field_info).format = UUID4]",
Type: "string",
},
{
name: "SkipNonUid",
FieldName: "other",
Type: "string",
},
{
name: "SkipNonStringUid",
FieldName: "uid",
Type: "Foo",
},
{
name: "InvalidMissingFormat",
FieldName: "uid",
Type: "string",
problems: testutils.Problems{{Message: "format = UUID4"}},
},
{
name: "InvalidWrongFormat",
FieldName: "uid",
Annotation: "[(google.api.field_info).format = IPV4]",
Type: "string",
problems: testutils.Problems{{Message: "format = UUID4"}},
},
} {
t.Run(test.name, func(t *testing.T) {
f := testutils.ParseProto3Tmpl(t, `
import "google/api/field_info.proto";
message Person {
{{.Type}} {{.FieldName}} = 2 {{.Annotation}};
}
message Foo {}
`, test)
field := f.GetMessageTypes()[0].GetFields()[0]
if diff := test.problems.SetDescriptor(field).Diff(uidFormat.Lint(f)); diff != "" {
t.Errorf(diff)
}
})
}
}

0 comments on commit 79cd201

Please sign in to comment.