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

fix(AIP-123): disallow spaces in pattern #1293

Merged
merged 1 commit into from
Dec 1, 2023
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
13 changes: 11 additions & 2 deletions rules/aip0123/resource_pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ func lintResourcePattern(resource *annotations.ResourceDescriptor, desc desc.Des
}

// Ensure that the constant segments of the pattern uses camel case,
// not snake case.
// not snake case, and there are no spaces.
for _, pattern := range resource.GetPattern() {
if strings.Contains(getPlainPattern(pattern), "_") {
plainPattern := getPlainPattern(pattern)

if strings.Contains(plainPattern, "_") {
return []lint.Problem{{
Message: fmt.Sprintf(
"Resource patterns should use camel case (apart from the variable names), such as %q.",
Expand All @@ -58,6 +60,13 @@ func lintResourcePattern(resource *annotations.ResourceDescriptor, desc desc.Des
Location: loc,
}}
}
if strings.Contains(plainPattern, " ") {
return []lint.Problem{{
Message: "Resource patterns should not have spaces",
Descriptor: desc,
Location: loc,
}}
}
}
return nil
}
3 changes: 3 additions & 0 deletions rules/aip0123/resource_pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func TestResourcePattern(t *testing.T) {
{"SnakeCase", `pattern: "book_publishers/{book_publisher}/books/{book}"`, testutils.Problems{{
Message: "bookPublishers/{book_publisher}/books/{book}",
}}},
{"HasSpaces", `pattern: "publishers/{publisher}/ books /{book}"`, testutils.Problems{{
Message: "Resource patterns should not have spaces",
}}},
} {
t.Run(test.name, func(t *testing.T) {
f := testutils.ParseProto3Tmpl(t, `
Expand Down