Skip to content

Commit

Permalink
fix(AIP-123): disallow spaces in pattern (#1293)
Browse files Browse the repository at this point in the history
  • Loading branch information
Juneezee authored Dec 1, 2023
1 parent ebf2763 commit 4d6e057
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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

0 comments on commit 4d6e057

Please sign in to comment.