From 03a74662afe9db1b45a854b24fc18275d130e9ef Mon Sep 17 00:00:00 2001 From: Gabriele Fontana Date: Sat, 16 Nov 2024 14:49:17 +0100 Subject: [PATCH 1/6] feat(add-contain-value-rule): add contain_value expression --- internal/arch/file/that/contain_value.go | 43 ++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 internal/arch/file/that/contain_value.go diff --git a/internal/arch/file/that/contain_value.go b/internal/arch/file/that/contain_value.go new file mode 100644 index 0000000..92b2d9a --- /dev/null +++ b/internal/arch/file/that/contain_value.go @@ -0,0 +1,43 @@ +package that + +import ( + "strings" + + "github.com/omissis/goarkitect/internal/arch/file" + "github.com/omissis/goarkitect/internal/arch/rule" +) + +func ContainsValue(s string) *ContainsValueExpression { + return &ContainsValueExpression{ + value: s, + } +} + +type ContainsValueExpression struct { + value string + + errors []error +} + +func (e *ContainsValueExpression) GetErrors() []error { + return e.errors +} + +func (e *ContainsValueExpression) 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) +} From 6350d59e78dd01bf427d392e2604c3fe93798ee3 Mon Sep 17 00:00:00 2001 From: Gabriele Fontana Date: Sat, 16 Nov 2024 15:13:35 +0100 Subject: [PATCH 2/6] refactor(add-contain-value-rule): renaming --- internal/arch/file/that/contain_value.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/arch/file/that/contain_value.go b/internal/arch/file/that/contain_value.go index 92b2d9a..c2f0b2e 100644 --- a/internal/arch/file/that/contain_value.go +++ b/internal/arch/file/that/contain_value.go @@ -7,23 +7,23 @@ import ( "github.com/omissis/goarkitect/internal/arch/rule" ) -func ContainsValue(s string) *ContainsValueExpression { - return &ContainsValueExpression{ +func ContainValue(s string) *ContainValueExpression { + return &ContainValueExpression{ value: s, } } -type ContainsValueExpression struct { +type ContainValueExpression struct { value string errors []error } -func (e *ContainsValueExpression) GetErrors() []error { +func (e *ContainValueExpression) GetErrors() []error { return e.errors } -func (e *ContainsValueExpression) Evaluate(rb rule.Builder) { +func (e *ContainValueExpression) Evaluate(rb rule.Builder) { frb, ok := rb.(*file.RuleBuilder) if !ok { e.errors = append(e.errors, file.ErrInvalidRuleBuilder) From dd144ed0e0b5545e10d0962850b63d9ab1d15aed Mon Sep 17 00:00:00 2001 From: Gabriele Fontana Date: Sat, 16 Nov 2024 15:14:36 +0100 Subject: [PATCH 3/6] test(add-contain-value-rule): add test for contain_value expression --- internal/arch/file/that/contain_value_test.go | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 internal/arch/file/that/contain_value_test.go diff --git a/internal/arch/file/that/contain_value_test.go b/internal/arch/file/that/contain_value_test.go new file mode 100644 index 0000000..2ea34e2 --- /dev/null +++ b/internal/arch/file/that/contain_value_test.go @@ -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) + } + }) + } +} From 57c72be1dfec2b1daf889d2146842f9ad5981ede Mon Sep 17 00:00:00 2001 From: Gabriele Fontana Date: Sat, 16 Nov 2024 16:38:49 +0100 Subject: [PATCH 4/6] feat(add-contain-value-rule): add fileThatContainValue schema definition --- api/config_schema.json | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/api/config_schema.json b/api/config_schema.json index 3edb181..72b84c2 100644 --- a/api/config_schema.json +++ b/api/config_schema.json @@ -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, From 4d0309558a4ead70c712fbbc93ddbc9a3b69c786 Mon Sep 17 00:00:00 2001 From: Gabriele Fontana Date: Sat, 16 Nov 2024 17:04:24 +0100 Subject: [PATCH 5/6] feat(add-contain-value-rule): add Value field in That type --- internal/config/executor.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/config/executor.go b/internal/config/executor.go index 00b64fe..99c14d3 100644 --- a/internal/config/executor.go +++ b/internal/config/executor.go @@ -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"` @@ -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) } From 2081583d711429add8ea339c936d6c870d7d1169 Mon Sep 17 00:00:00 2001 From: Gabriele Fontana Date: Sat, 16 Nov 2024 17:05:31 +0100 Subject: [PATCH 6/6] test(add-contain-value-rule): add taht contain rule example --- examples/.goarkitect.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/examples/.goarkitect.yaml b/examples/.goarkitect.yaml index 7413639..0767edb 100644 --- a/examples/.goarkitect.yaml +++ b/examples/.goarkitect.yaml @@ -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 "