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

refactor: Extract common lints into the util package to eliminate redundancy #687

Merged
merged 3 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 3 additions & 16 deletions rules/aip0131/http_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,11 @@ package aip0131
import (
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
)

// Get methods should not have an HTTP body.
var httpBody = &lint.MethodRule{
Name: lint.NewRuleName(131, "http-body"),
OnlyIf: isGetMethod,
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
// Establish that the RPC has no HTTP body.
for _, httpRule := range utils.GetHTTPRules(m) {
if httpRule.Body != "" {
return []lint.Problem{{
Message: "Get methods should not have an HTTP body.",
Descriptor: m,
}}
}
}

return nil
},
Name: lint.NewRuleName(131, "http-body"),
OnlyIf: isGetMethod,
LintMethod: utils.LintNoHTTPBody,
}
19 changes: 3 additions & 16 deletions rules/aip0131/http_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,11 @@ package aip0131
import (
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
)

// Get methods should use the HTTP GET verb.
var httpMethod = &lint.MethodRule{
Name: lint.NewRuleName(131, "http-method"),
OnlyIf: isGetMethod,
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
// Rule check: Establish that the RPC uses HTTP GET.
for _, httpRule := range utils.GetHTTPRules(m) {
if httpRule.Method != "GET" {
return []lint.Problem{{
Message: "Get methods must use the HTTP GET verb.",
Descriptor: m,
}}
}
}

return nil
},
Name: lint.NewRuleName(131, "http-method"),
OnlyIf: isGetMethod,
LintMethod: utils.LintHTTPMethod("GET"),
}
10 changes: 1 addition & 9 deletions rules/aip0131/request_name_behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,5 @@ var requestNameBehavior = &lint.FieldRule{
OnlyIf: func(f *desc.FieldDescriptor) bool {
return isGetRequestMessage(f.GetOwner()) && f.GetName() == "name"
},
LintField: func(f *desc.FieldDescriptor) []lint.Problem {
if !utils.GetFieldBehavior(f).Contains("REQUIRED") {
return []lint.Problem{{
Message: "Get requests: The `name` field should include `(google.api.field_behavior) = REQUIRED`.",
Descriptor: f,
}}
}
return nil
},
LintField: utils.LintRequiredField,
}
10 changes: 1 addition & 9 deletions rules/aip0131/request_name_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,5 @@ var requestNameReference = &lint.FieldRule{
OnlyIf: func(f *desc.FieldDescriptor) bool {
return isGetRequestMessage(f.GetOwner()) && f.GetName() == "name"
},
LintField: func(f *desc.FieldDescriptor) []lint.Problem {
if ref := utils.GetResourceReference(f); ref == nil {
return []lint.Problem{{
Message: "Get methods: The `name` field should include a `google.api.resource_reference` annotation.",
Descriptor: f,
}}
}
return nil
},
LintField: utils.LintFieldResourceReference,
}
10 changes: 1 addition & 9 deletions rules/aip0132/request_parent_behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,5 @@ var requestParentBehavior = &lint.FieldRule{
OnlyIf: func(f *desc.FieldDescriptor) bool {
return isListRequestMessage(f.GetOwner()) && f.GetName() == "parent"
},
LintField: func(f *desc.FieldDescriptor) []lint.Problem {
if !utils.GetFieldBehavior(f).Contains("REQUIRED") {
return []lint.Problem{{
Message: "List requests: The `parent` field should include `(google.api.field_behavior) = REQUIRED`.",
Descriptor: f,
}}
}
return nil
},
LintField: utils.LintRequiredField,
}
16 changes: 2 additions & 14 deletions rules/aip0132/request_parent_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ package aip0132

import (
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/locations"
"github.com/googleapis/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/builder"
)

// The type of the parent field in the List request message should
Expand All @@ -28,16 +27,5 @@ var requestParentField = &lint.FieldRule{
OnlyIf: func(f *desc.FieldDescriptor) bool {
return isListRequestMessage(f.GetOwner()) && f.GetName() == "parent"
},
LintField: func(f *desc.FieldDescriptor) []lint.Problem {
if f.GetType() != builder.FieldTypeString().GetType() {
return []lint.Problem{{
Message: "`parent` field on List RPCs must be a string",
Suggestion: "string",
Descriptor: f,
Location: locations.FieldType(f),
}}
}

return nil
},
LintField: utils.LintStringField,
}
10 changes: 1 addition & 9 deletions rules/aip0132/request_parent_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,5 @@ var requestParentReference = &lint.FieldRule{
OnlyIf: func(f *desc.FieldDescriptor) bool {
return isListRequestMessage(f.GetOwner()) && f.GetName() == "parent"
},
LintField: func(f *desc.FieldDescriptor) []lint.Problem {
if ref := utils.GetResourceReference(f); ref == nil {
return []lint.Problem{{
Message: "List methods: The `parent` field should include a `google.api.resource_reference` annotation.",
Descriptor: f,
}}
}
return nil
},
LintField: utils.LintFieldResourceReference,
}
19 changes: 3 additions & 16 deletions rules/aip0133/http_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,11 @@ package aip0133
import (
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
)

// Create methods should use the HTTP POST verb.
var httpMethod = &lint.MethodRule{
Name: lint.NewRuleName(133, "http-method"),
OnlyIf: isCreateMethod,
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
// Rule check: Establish that the RPC uses HTTP POST.
for _, httpRule := range utils.GetHTTPRules(m) {
if httpRule.Method != "POST" {
return []lint.Problem{{
Message: "Create methods must use the HTTP POST verb.",
Descriptor: m,
}}
}
}

return nil
},
Name: lint.NewRuleName(133, "http-method"),
OnlyIf: isCreateMethod,
LintMethod: utils.LintHTTPMethod("POST"),
}
10 changes: 1 addition & 9 deletions rules/aip0133/request_parent_behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,5 @@ var requestParentBehavior = &lint.FieldRule{
OnlyIf: func(f *desc.FieldDescriptor) bool {
return isCreateRequestMessage(f.GetOwner()) && f.GetName() == "parent"
},
LintField: func(f *desc.FieldDescriptor) []lint.Problem {
if !utils.GetFieldBehavior(f).Contains("REQUIRED") {
return []lint.Problem{{
Message: "Create requests: The `parent` field should include `(google.api.field_behavior) = REQUIRED`.",
Descriptor: f,
}}
}
return nil
},
LintField: utils.LintRequiredField,
}
16 changes: 2 additions & 14 deletions rules/aip0133/request_parent_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ package aip0133

import (
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/locations"
"github.com/googleapis/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/builder"
)

// The type of the parent field in a creat request should be string.
Expand All @@ -27,16 +26,5 @@ var requestParentField = &lint.FieldRule{
OnlyIf: func(f *desc.FieldDescriptor) bool {
return isCreateRequestMessage(f.GetOwner()) && f.GetName() == "parent"
},
LintField: func(f *desc.FieldDescriptor) []lint.Problem {
if f.GetType() != builder.FieldTypeString().GetType() {
return []lint.Problem{{
Message: "`parent` field on create request message must be a string",
Suggestion: "string",
Descriptor: f,
Location: locations.FieldType(f),
}}
}

return nil
},
LintField: utils.LintStringField,
}
10 changes: 1 addition & 9 deletions rules/aip0133/request_parent_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,5 @@ var requestParentReference = &lint.FieldRule{
OnlyIf: func(f *desc.FieldDescriptor) bool {
return isCreateRequestMessage(f.GetOwner()) && f.GetName() == "parent"
},
LintField: func(f *desc.FieldDescriptor) []lint.Problem {
if ref := utils.GetResourceReference(f); ref == nil {
return []lint.Problem{{
Message: "Create methods: The `parent` field should include a `google.api.resource_reference` annotation.",
Descriptor: f,
}}
}
return nil
},
LintField: utils.LintFieldResourceReference,
}
15 changes: 1 addition & 14 deletions rules/aip0133/request_resource_behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package aip0133

import (
"fmt"

"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
Expand All @@ -35,16 +33,5 @@ var requestResourceBehavior = &lint.FieldRule{
}
return false
},
LintField: func(f *desc.FieldDescriptor) []lint.Problem {
if !utils.GetFieldBehavior(f).Contains("REQUIRED") {
return []lint.Problem{{
Message: fmt.Sprintf(
"Create requests: The `%s` field should include `(google.api.field_behavior) = REQUIRED`.",
f.GetName(),
),
Descriptor: f,
}}
}
return nil
},
LintField: utils.LintRequiredField,
}
19 changes: 3 additions & 16 deletions rules/aip0134/http_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,11 @@ package aip0134
import (
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
)

// Update methods should use the HTTP PATCH verb.
var httpMethod = &lint.MethodRule{
Name: lint.NewRuleName(134, "http-method"),
OnlyIf: isUpdateMethod,
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
// Rule check: Establish that the RPC uses HTTP PATCH.
for _, httpRule := range utils.GetHTTPRules(m) {
if httpRule.Method != "PATCH" {
return []lint.Problem{{
Message: "Update methods must use the HTTP PATCH verb.",
Descriptor: m,
}}
}
}

return nil
},
Name: lint.NewRuleName(134, "http-method"),
OnlyIf: isUpdateMethod,
LintMethod: utils.LintHTTPMethod("PATCH"),
}
19 changes: 3 additions & 16 deletions rules/aip0135/http_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,11 @@ package aip0135
import (
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
)

// Delete methods should not have an HTTP body.
var httpBody = &lint.MethodRule{
Name: lint.NewRuleName(135, "http-body"),
OnlyIf: isDeleteMethod,
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
// Establish that the RPC has no HTTP body.
for _, httpRule := range utils.GetHTTPRules(m) {
if httpRule.Body != "" {
return []lint.Problem{{
Message: "Delete methods should not have an HTTP body.",
Descriptor: m,
}}
}
}

return nil
},
Name: lint.NewRuleName(135, "http-body"),
OnlyIf: isDeleteMethod,
LintMethod: utils.LintNoHTTPBody,
}
19 changes: 3 additions & 16 deletions rules/aip0135/http_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,11 @@ package aip0135
import (
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
)

// Delete methods should use the HTTP DELETE method.
var httpMethod = &lint.MethodRule{
Name: lint.NewRuleName(135, "http-method"),
OnlyIf: isDeleteMethod,
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
// Rule check: Establish that the RPC uses HTTP DELETE.
for _, httpRule := range utils.GetHTTPRules(m) {
if httpRule.Method != "DELETE" {
return []lint.Problem{{
Message: "Delete methods must use the HTTP DELETE verb.",
Descriptor: m,
}}
}
}

return nil
},
Name: lint.NewRuleName(135, "http-method"),
OnlyIf: isDeleteMethod,
LintMethod: utils.LintHTTPMethod("DELETE"),
}
10 changes: 1 addition & 9 deletions rules/aip0135/request_name_behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,5 @@ var requestNameBehavior = &lint.FieldRule{
OnlyIf: func(f *desc.FieldDescriptor) bool {
return isDeleteRequestMessage(f.GetOwner()) && f.GetName() == "name"
},
LintField: func(f *desc.FieldDescriptor) []lint.Problem {
if !utils.GetFieldBehavior(f).Contains("REQUIRED") {
return []lint.Problem{{
Message: "Delete requests: The `name` field should include `(google.api.field_behavior) = REQUIRED`.",
Descriptor: f,
}}
}
return nil
},
LintField: utils.LintRequiredField,
}
10 changes: 1 addition & 9 deletions rules/aip0135/request_name_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,5 @@ var requestNameReference = &lint.FieldRule{
OnlyIf: func(f *desc.FieldDescriptor) bool {
return isDeleteRequestMessage(f.GetOwner()) && f.GetName() == "name"
},
LintField: func(f *desc.FieldDescriptor) []lint.Problem {
if ref := utils.GetResourceReference(f); ref == nil {
return []lint.Problem{{
Message: "Delete methods: The `name` field should include a `google.api.resource_reference` annotation.",
Descriptor: f,
}}
}
return nil
},
LintField: utils.LintFieldResourceReference,
}
Loading