Skip to content

Commit

Permalink
Add missing rules and make names nicer (#49)
Browse files Browse the repository at this point in the history
* Use replection to add waf rules and make the names nicer

* linting

* Fix integration tests

* Update waf/waf.go

Co-authored-by: Matt White <[email protected]>

* Update waf/waf.go

Co-authored-by: Matt White <[email protected]>

* fix: duff suggestion

* Make the code more readable

* Simplify code

---------

Co-authored-by: Matt White <[email protected]>
  • Loading branch information
jaredfholgate and matt-FFFFFF authored Jun 21, 2024
1 parent eabe5f6 commit 2b6d63a
Show file tree
Hide file tree
Showing 27 changed files with 214 additions and 122 deletions.
24 changes: 12 additions & 12 deletions attrvalue/nested_block_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestNestedBlockValueRule(t *testing.T) {
}{
{
name: "correct string",
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false),
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false, ""),
content: `
variable "test" {
type = string
Expand All @@ -36,7 +36,7 @@ func TestNestedBlockValueRule(t *testing.T) {
},
{
name: "incorrect string",
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false),
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false, ""),
content: `
variable "test" {
type = string
Expand All @@ -49,14 +49,14 @@ func TestNestedBlockValueRule(t *testing.T) {
}`,
expected: helper.Issues{
{
Rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false),
Rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false, ""),
Message: "baz is an invalid attribute value of `bar` - expecting (one of) [biz bat]",
},
},
},
{
name: "incorrect resource with correct block",
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false),
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false, ""),
content: `
variable "test" {
type = string
Expand All @@ -71,7 +71,7 @@ func TestNestedBlockValueRule(t *testing.T) {
},
{
name: "no nested block of that type",
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false),
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false, ""),
content: `
variable "test" {
type = string
Expand All @@ -86,7 +86,7 @@ func TestNestedBlockValueRule(t *testing.T) {
},
{
name: "no nested block of that type with must exist set",
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", true),
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", true, ""),
content: `
variable "test" {
type = string
Expand All @@ -99,14 +99,14 @@ func TestNestedBlockValueRule(t *testing.T) {
}`,
expected: helper.Issues{
{
Rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", true),
Rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", true, ""),
Message: "The attribute `bar` must be specified",
},
},
},
{
name: "no nested block attribute of that type with must exist set",
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", true),
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", true, ""),
content: `
variable "test" {
type = string
Expand All @@ -119,14 +119,14 @@ func TestNestedBlockValueRule(t *testing.T) {
}`,
expected: helper.Issues{
{
Rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", true),
Rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", true, ""),
Message: "The attribute `bar` must be specified",
},
},
},
{
name: "multiple blocks correct",
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false),
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false, ""),
content: `
variable "test" {
type = string
Expand All @@ -144,7 +144,7 @@ func TestNestedBlockValueRule(t *testing.T) {
},
{
name: "multiple blocks partially correct",
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false),
rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false, ""),
content: `
variable "test" {
type = string
Expand All @@ -164,7 +164,7 @@ func TestNestedBlockValueRule(t *testing.T) {
}`,
expected: helper.Issues{
{
Rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false),
Rule: attrvalue.NewSimpleNestedBlockRule("foo", "fiz", "bar", []string{"biz", "bat"}, "", false, ""),
Message: "incorrect is an invalid attribute value of `bar` - expecting (one of) [biz bat]",
},
},
Expand Down
12 changes: 9 additions & 3 deletions attrvalue/set_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,30 @@ type SetRule[T cmp.Ordered] struct {
tflint.DefaultRule // Embed the default rule to reuse its implementation
baseValue
expectedValues [][]T // e.g. [][int{1, 2, 3}]
ruleName string
}

var _ tflint.Rule = (*SetRule[int])(nil)
var _ AttrValueRule = (*SimpleRule[any])(nil)

// NewSetRule returns a new rule with the given resource type, attribute name, and expected values.
func NewSetRule[T cmp.Ordered](resourceType string, attributeName string, expectedValues [][]T, link string) *SetRule[T] {
func NewSetRule[T cmp.Ordered](resourceType string, attributeName string, expectedValues [][]T, link string, ruleName string) *SetRule[T] {
return &SetRule[T]{
baseValue: newBaseValue(resourceType, nil, attributeName, true, link, tflint.ERROR),
expectedValues: expectedValues,
ruleName: ruleName,
}
}

func (r *SetRule[T]) Name() string {
if r.ruleName != "" {
return r.ruleName
}

if r.nestedBlockType != nil {
return fmt.Sprintf("set_value_%s.%s.%s", r.resourceType, *r.nestedBlockType, r.attributeName)
return fmt.Sprintf("%s.%s.%s", r.resourceType, *r.nestedBlockType, r.attributeName)
}
return fmt.Sprintf("set_value_%s.%s", r.resourceType, r.attributeName)
return fmt.Sprintf("%s.%s", r.resourceType, r.attributeName)
}

func (r *SetRule[T]) Check(runner tflint.Runner) error {
Expand Down
14 changes: 7 additions & 7 deletions attrvalue/set_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestListNumberValueRule(t *testing.T) {
}{
{
name: "incorrect",
rule: attrvalue.NewSetRule("foo", "bar", [][]int{{1, 2, 3}}, ""),
rule: attrvalue.NewSetRule("foo", "bar", [][]int{{1, 2, 3}}, "", ""),
content: `
variable "test" {
type = list(number)
Expand All @@ -30,14 +30,14 @@ func TestListNumberValueRule(t *testing.T) {
}`,
expected: helper.Issues{
{
Rule: attrvalue.NewSetRule("foo", "bar", [][]int{{1, 2, 3}}, ""),
Rule: attrvalue.NewSetRule("foo", "bar", [][]int{{1, 2, 3}}, "", ""),
Message: "\"[3]\" is an invalid attribute value of `bar` - expecting (one of) [[1 2 3]]",
},
},
},
{
name: "correct",
rule: attrvalue.NewSetRule("foo", "bar", [][]int{{1, 2, 3}}, ""),
rule: attrvalue.NewSetRule("foo", "bar", [][]int{{1, 2, 3}}, "", ""),
content: `
variable "test" {
type = list(number)
Expand All @@ -50,7 +50,7 @@ func TestListNumberValueRule(t *testing.T) {
},
{
name: "correct with string list",
rule: attrvalue.NewSetRule("foo", "bar", [][]string{{"1", "2", "3"}}, ""),
rule: attrvalue.NewSetRule("foo", "bar", [][]string{{"1", "2", "3"}}, "", ""),
content: `
variable "test" {
type = list(string)
Expand All @@ -63,7 +63,7 @@ func TestListNumberValueRule(t *testing.T) {
},
{
name: "correct but different order",
rule: attrvalue.NewSetRule("foo", "bar", [][]int{{1, 2, 3}}, ""),
rule: attrvalue.NewSetRule("foo", "bar", [][]int{{1, 2, 3}}, "", ""),
content: `
variable "test" {
type = list(number)
Expand All @@ -76,7 +76,7 @@ func TestListNumberValueRule(t *testing.T) {
},
{
name: "variable without default",
rule: attrvalue.NewSetRule("foo", "bar", [][]int{{1, 2, 3}}, ""),
rule: attrvalue.NewSetRule("foo", "bar", [][]int{{1, 2, 3}}, "", ""),
content: `
variable "test" {
type = list(number)
Expand All @@ -88,7 +88,7 @@ func TestListNumberValueRule(t *testing.T) {
},
{
name: "variable with convertable element type",
rule: attrvalue.NewSetRule("foo", "bar", [][]int{{1, 2, 3}}, ""),
rule: attrvalue.NewSetRule("foo", "bar", [][]int{{1, 2, 3}}, "", ""),
content: `
variable "test" {
type = list(string)
Expand Down
15 changes: 11 additions & 4 deletions attrvalue/simple_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,29 @@ type SimpleRule[T any] struct {
baseValue
expectedValues []T // e.g. []string{"ZRS"}
mustExist bool
ruleName string
}

var _ tflint.Rule = (*SimpleRule[any])(nil)
var _ AttrValueRule = (*SimpleRule[any])(nil)

// NewSimpleRule returns a new rule with the given resource type, attribute name, and expected values.
func NewSimpleRule[T any](resourceType, attributeName string, expectedValues []T, link string, mustExist bool) *SimpleRule[T] {
func NewSimpleRule[T any](resourceType, attributeName string, expectedValues []T, link string, mustExist bool, ruleName string) *SimpleRule[T] {
return &SimpleRule[T]{
baseValue: newBaseValue(resourceType, nil, attributeName, true, link, tflint.ERROR),
expectedValues: expectedValues,
mustExist: mustExist,
ruleName: ruleName,
}
}

// NewSimpleNestedBlockRule returns a new rule with the given resource type, attribute name, and expected values.
func NewSimpleNestedBlockRule[T any](resourceType, nestedBlockType, attributeName string, expectedValues []T, link string, mustExist bool) *SimpleRule[T] {
func NewSimpleNestedBlockRule[T any](resourceType, nestedBlockType, attributeName string, expectedValues []T, link string, mustExist bool, ruleName string) *SimpleRule[T] {
return &SimpleRule[T]{
baseValue: newBaseValue(resourceType, &nestedBlockType, attributeName, true, link, tflint.ERROR),
expectedValues: expectedValues,
mustExist: mustExist,
ruleName: ruleName,
}
}

Expand All @@ -45,10 +48,14 @@ func (r *SimpleRule[T]) Link() string {
}

func (r *SimpleRule[T]) Name() string {
if r.ruleName != "" {
return r.ruleName
}

if r.nestedBlockType != nil {
return fmt.Sprintf("simple_value_%s.%s.%s", r.resourceType, *r.nestedBlockType, r.attributeName)
return fmt.Sprintf("%s.%s.%s", r.resourceType, *r.nestedBlockType, r.attributeName)
}
return fmt.Sprintf("simple_value_%s.%s", r.resourceType, r.attributeName)
return fmt.Sprintf("%s.%s", r.resourceType, r.attributeName)
}

func (r *SimpleRule[T]) Check(runner tflint.Runner) error {
Expand Down
Loading

0 comments on commit 2b6d63a

Please sign in to comment.