Skip to content
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
2 changes: 1 addition & 1 deletion rules/aep0004/aep0004.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func getDesiredPattern(pattern string) string {
for _, token := range strings.Split(pattern, "/") {
if strings.HasPrefix(token, "{") && strings.HasSuffix(token, "}") {
varname := token[1 : len(token)-1]
want = append(want, fmt.Sprintf("{%s}", strings.TrimSuffix(strcase.SnakeCase(varname), "_id")))
want = append(want, fmt.Sprintf("{%s}", strcase.SnakeCase(varname)))
} else {
want = append(want, strcase.LowerCamelCase(token))
}
Expand Down
4 changes: 1 addition & 3 deletions rules/aep0004/resource_definition_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ func TestResourceDefinitionVariables(t *testing.T) {
problems testutils.Problems
}{
{"Valid", "publishers/{publisher}/electronicBooks/{electronic_book}", testutils.Problems{}},
{"ValidWithIdSuffix", "publishers/{publisher_id}/electronicBooks/{electronic_book_id}", testutils.Problems{}},
{"CamelCase", "publishers/{publisher}/electronicBooks/{electronicBook}", testutils.Problems{{
Message: "publishers/{publisher}/electronicBooks/{electronic_book}",
}}},
{"ID", "publishers/{publisher}/electronicBooks/{electronic_book_id}", testutils.Problems{{
Message: "publishers/{publisher}/electronicBooks/{electronic_book}",
}}},
} {
t.Run(test.name, func(t *testing.T) {
f := testutils.ParseProto3Tmpl(t, `
Expand Down
2 changes: 1 addition & 1 deletion rules/aep0004/resource_name_components_alternate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/jhump/protoreflect/desc"
)

var identifierRegexp = regexp.MustCompile("^{[a-z][-a-z0-9]*[a-z0-9]}$")
var identifierRegexp = regexp.MustCompile("^{[a-z][_a-z0-9-]*[a-z0-9]}$")

var resourceNameComponentsAlternate = &lint.MessageRule{
Name: lint.NewRuleName(4, "resource-name-components-alternate"),
Expand Down
1 change: 1 addition & 0 deletions rules/aep0004/resource_name_components_alternate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestResourceNameComponentsAlternate(t *testing.T) {
{"Valid", "author/{author}/books/{book}", testutils.Problems{}},
{"Valid", "publishers/{publisher}/books/{book}/editions/{book-edition}", testutils.Problems{}},
{"ValidSingleton", "user/{user}/config", testutils.Problems{}},
{"ValidWithIdSuffix", "stores/{store_id}/items/{item_id}", testutils.Problems{}},
{"InvalidDoubleCollection", "author/books/{book}", testutils.Problems{{Message: "must alternate"}}},
{"InvalidDoubleIdentifier", "books/{author}/{book}", testutils.Problems{{Message: "must alternate"}}},
} {
Expand Down
10 changes: 0 additions & 10 deletions rules/aep0004/resource_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ func lintResourceVariables(resource *annotations.ResourceDescriptor, desc desc.D
Location: loc,
}}
}
if strings.HasSuffix(variable, "_id") {
return []lint.Problem{{
Message: fmt.Sprintf(
"Variable names should omit the `_id` suffix, such as %q.",
getDesiredPattern(pattern),
),
Descriptor: desc,
Location: loc,
}}
}
}
}
return nil
Expand Down
4 changes: 1 addition & 3 deletions rules/aep0004/resource_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ func TestResourceVariables(t *testing.T) {
problems testutils.Problems
}{
{"Valid", "publishers/{publisher}/electronicBooks/{electronic_book}", testutils.Problems{}},
{"ValidWithIdSuffix", "publishers/{publisher_id}/electronicBooks/{electronic_book_id}", testutils.Problems{}},
{"CamelCase", "publishers/{publisher}/electronicBooks/{electronicBook}", testutils.Problems{{
Message: "publishers/{publisher}/electronicBooks/{electronic_book}",
}}},
{"ID", "publishers/{publisher}/electronicBooks/{electronic_book_id}", testutils.Problems{{
Message: "publishers/{publisher}/electronicBooks/{electronic_book}",
}}},
} {
t.Run(test.name, func(t *testing.T) {
f := testutils.ParseProto3Tmpl(t, `
Expand Down
12 changes: 12 additions & 0 deletions rules/aep0122/resource_collection_identifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ var resourceCollectionIdentifiers = &lint.MessageRule{

segs := strings.Split(p, "/")
for _, seg := range segs {
if strings.HasPrefix(seg, "{") && strings.HasSuffix(seg, "}") {
// Variable segments can contain underscores, but must be lowercase
varName := seg[1 : len(seg)-1] // Remove { and }
if HasUpper(varName) {
problems = append(problems, lint.Problem{
Message: "Resource pattern variables must be lowercase.",
Descriptor: m,
Location: locations.MessageResource(m),
})
}
continue
}
if HasUpper(seg) || strings.Contains(seg, "_") {
problems = append(problems, lint.Problem{
Message: "Resource patterns must use kebab-case for collection identifiers.",
Expand Down
2 changes: 2 additions & 0 deletions rules/aep0122/resource_collection_identifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func TestResourceCollectionIdentifiers(t *testing.T) {
problems testutils.Problems
}{
{"Valid", "author/{author}/books/{book}", testutils.Problems{}},
{"ValidWithIdSuffix", "stores/{store_id}/items/{item_id}", testutils.Problems{}},
{"InvalidCapitalIdSuffix", "stores/{Store_id}/items/{item_id}", testutils.Problems{{Message: "lowercase"}}},
{"InvalidUpperCase", "author/{author}/Books/{book}", testutils.Problems{{Message: "kebab-case"}}},
{"InvalidStartsWithSlash", "/author/{author}/Books/{book}", testutils.Problems{{Message: "lowercase letter"}}},
{"InvalidStartsWithCapitalLetter", "Author/{author}/Books/{book}", testutils.Problems{{Message: "lowercase letter"}}},
Expand Down