Skip to content

Commit

Permalink
fix(AIP-123): multiword singleton reduction (#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahdietz authored Aug 14, 2024
1 parent 6bfbcdf commit 7868552
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rules/aip0123/resource_pattern_singular.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ var resourcePatternSingular = &lint.MessageRule{
if !utils.IsSingletonResource(m) {
singular = fmt.Sprintf("{%s}", strcase.SnakeCase(singular))
nn = fmt.Sprintf("{%s}", nn)
} else {
// singular is already in lower camel case
// but nested name is returned in snake_case form
// and final segment of singleton is lowerCamelCase
nn = strcase.LowerCamelCase(nn)
}

// If the first pattern is reduced or non-compliant, but is nested name eligible, we want to recommend the nested name.
Expand Down
35 changes: 35 additions & 0 deletions rules/aip0123/resource_pattern_singular_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,41 @@ func TestResourcePatternSingularSimple(t *testing.T) {
}
}

func TestResourcePatternSingularMultiWord(t *testing.T) {
for _, test := range []struct {
name string
Pattern string
problems testutils.Problems
}{
{"Valid", "fooBars/{foo_bar}/fooBarBazBuzzes/{foo_bar_baz_buz}", testutils.Problems{}},
{"ValidReduced", "fooBars/{foo_bar}/bazBuzzes/{baz_buz}", testutils.Problems{}},
{"ValidSingleton", "fooBars/{foo_bar}/fooBarBazBuz", testutils.Problems{}},
{"ValidSingletonReduced", "fooBars/{foo_bar}/bazBuz", testutils.Problems{}},
{"Invalid", "fooBars/{foo_bar}/fooBarBazBuzzes/{buz}", testutils.Problems{{Message: "baz_buz"}}},
{"InvalidSingleton", "fooBars/{foo_bar}/buz", testutils.Problems{{Message: "bazBuz"}}},
} {
t.Run(test.name, func(t *testing.T) {
f := testutils.ParseProto3Tmpl(t, `
import "google/api/resource.proto";
message FooBarBazBuz {
option (google.api.resource) = {
type: "foo.googleapis.com/FooBarBazBuz"
singular: "fooBarBazBuz"
plural: "fooBarBazBuzzes"
pattern: "{{.Pattern}}"
};
string name = 1;
}
`, test)
m := f.GetMessageTypes()[0]
if diff := test.problems.SetDescriptor(m).Diff(resourcePatternSingular.Lint(f)); diff != "" {
t.Errorf(diff)
}
})
}
}

func TestResourcePatternSingularNested(t *testing.T) {
for _, test := range []struct {
name string
Expand Down

0 comments on commit 7868552

Please sign in to comment.