Skip to content

Commit

Permalink
Feat/add contain value rule (#103)
Browse files Browse the repository at this point in the history
* feat(add-contain-value-rule): add contain_value expression

* refactor(add-contain-value-rule): renaming

* test(add-contain-value-rule): add test for contain_value expression

* feat(add-contain-value-rule): add fileThatContainValue schema definition

* feat(add-contain-value-rule): add Value field in That type

* test(add-contain-value-rule): add taht contain rule example
  • Loading branch information
gafreax authored Nov 24, 2024
1 parent 8b5ceb8 commit 615df88
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
17 changes: 17 additions & 0 deletions api/config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@
"folder"
]
},
"fileThatContainValue": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"pattern": "^contain_value$"
},
"value": {
"type": "string"
}
},
"required": [
"kind",
"value"
]
},
"fileThatEndWith": {
"type": "object",
"additionalProperties": false,
Expand Down
12 changes: 12 additions & 0 deletions examples/.goarkitect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,15 @@ rules:
- kind: end_with
suffix: file
because: "it is an example"
- name: a set of file that contains .go must be go file (end with .go)
kind: file
matcher:
kind: all
thats:
- kind: contain_value
value: .go
excepts: []
shoulds:
- kind: end_with
suffix: .go
because: "it is an example "
43 changes: 43 additions & 0 deletions internal/arch/file/that/contain_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package that

import (
"strings"

"github.com/omissis/goarkitect/internal/arch/file"
"github.com/omissis/goarkitect/internal/arch/rule"
)

func ContainValue(s string) *ContainValueExpression {
return &ContainValueExpression{
value: s,
}
}

type ContainValueExpression struct {
value string

errors []error
}

func (e *ContainValueExpression) GetErrors() []error {
return e.errors
}

func (e *ContainValueExpression) Evaluate(rb rule.Builder) {
frb, ok := rb.(*file.RuleBuilder)
if !ok {
e.errors = append(e.errors, file.ErrInvalidRuleBuilder)

return
}

files := make([]string, 0)

for _, f := range frb.GetFiles() {
if strings.Contains(f, e.value) {
files = append(files, f)
}
}

frb.SetFiles(files)
}
52 changes: 52 additions & 0 deletions internal/arch/file/that/contain_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package that_test

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

"github.com/omissis/goarkitect/internal/arch/file"
"github.com/omissis/goarkitect/internal/arch/file/that"
"github.com/omissis/goarkitect/internal/arch/rule"
)

func Test_ContainValue(t *testing.T) {
t.Parallel()

rb := func() *file.RuleBuilder {
rb := file.All()
rb.SetFiles([]string{"Dockerfile", "Makefile", "foo/bar.go"})

return rb
}

testCases := []struct {
desc string
ruleBuilder *file.RuleBuilder
value string
want []string
}{
{
desc: "files contains value 'foo'",
ruleBuilder: rb(),
value: "foo",
want: []string{"foo/bar.go"},
},
}
for _, tC := range testCases {
tC := tC

t.Run(tC.desc, func(t *testing.T) {
t.Parallel()

ew := that.ContainValue(tC.value)
ew.Evaluate(tC.ruleBuilder)

got := tC.ruleBuilder.GetFiles()
if !cmp.Equal(got, tC.want, cmp.AllowUnexported(rule.Violation{}), cmpopts.EquateEmpty()) {
t.Errorf("want = %+v, got = %+v", tC.want, got)
}
})
}
}
4 changes: 4 additions & 0 deletions internal/config/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type That struct {
Folder string `json:"folder" yaml:"folder"`
Recursive bool `json:"recursive" yaml:"recursive"`
Suffix string `json:"suffix" yaml:"suffix"`
Value string `json:"value" yaml:"value"`
}
type Except struct {
Kind string `json:"kind" yaml:"kind"`
Expand Down Expand Up @@ -158,6 +159,9 @@ func applyThats(rb *file.RuleBuilder, ts []That) error {
case "end_with":
rb.That(ft.EndWith(t.Suffix))

case "contain_value":
rb.That(ft.ContainValue(t.Value))

default:
return fmt.Errorf("'%s': %w", t.Kind, ErrUnknonwnThat)
}
Expand Down

0 comments on commit 615df88

Please sign in to comment.