From 5003f0b1f5c23724193f786049047c2221a1952d Mon Sep 17 00:00:00 2001 From: Matt Welke Date: Wed, 21 Aug 2024 18:17:40 -0400 Subject: [PATCH 1/3] Create new validation rule interface to help with rule names. Signed-off-by: Matt Welke --- hauler-manifest.yaml | 2 +- pkg/validationresult/validation_result.go | 2 +- pkg/validationrule/validation_rule.go | 37 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 pkg/validationrule/validation_rule.go diff --git a/hauler-manifest.yaml b/hauler-manifest.yaml index 511b2e2d..585a0c4e 100644 --- a/hauler-manifest.yaml +++ b/hauler-manifest.yaml @@ -57,4 +57,4 @@ metadata: spec: files: - name: validatorctl - path: https://github.com/validator-labs/validatorctl/releases/download/v0.1.1/validator-linux-ARCH \ No newline at end of file + path: https://github.com/validator-labs/validatorctl/releases/download/v0.1.2/validator-linux-ARCH \ No newline at end of file diff --git a/pkg/validationresult/validation_result.go b/pkg/validationresult/validation_result.go index da4d4fb5..81e18b28 100644 --- a/pkg/validationresult/validation_result.go +++ b/pkg/validationresult/validation_result.go @@ -26,7 +26,7 @@ type Patcher interface { Patch(ctx context.Context, obj client.Object, opts ...patch.Option) error } -// ValidationRule is an interface for validation rules. +// ValidationRule is an interface for defining validation rules that are used to build a ValidationResult. type ValidationRule interface { client.Object GetKind() string diff --git a/pkg/validationrule/validation_rule.go b/pkg/validationrule/validation_rule.go new file mode 100644 index 00000000..74cf227f --- /dev/null +++ b/pkg/validationrule/validation_rule.go @@ -0,0 +1,37 @@ +// Package validationrule describes validation rules in the Validator ecosystem. +package validationrule + +// Interface defines validation rule behavior. +type Interface interface { + // Name returns the name of the rule. + Name() string + // SetName sets the name of the rule if it is a rule that supports manually specified names. + SetName(string) + // AllowsManualName returns whether the validation rule allows manually specifying its name. + // Code processing rules should check this first before trying to set a manual name. + AllowsManualName() bool +} + +// ManuallyNamed can be embedded into a rule struct to indicate that the rule allows its name to be +// manually specified. +type ManuallyNamed struct{} + +// AllowsManualName returns true, indicating that the rule supports manually specifying its name. +func (ManuallyNamed) AllowsManualName() bool { + return true +} + +// AutomaticallyNamed can be embedded into a rule struct to indicate that the rule does not allow its +// name to be manually specified. +type AutomaticallyNamed struct{} + +// AllowsManualName returns false, indicating that the rule does not support manually specifying its +// name. +func (AutomaticallyNamed) AllowsManualName() bool { + return false +} + +// SetName is a no-op because the rule does not support manually specifying its name. +func (AutomaticallyNamed) SetName(string) { + // no-op +} From 0090c8c45be1430acbedaf6ac3804851de0ba2a8 Mon Sep 17 00:00:00 2001 From: Matt Welke Date: Thu, 22 Aug 2024 12:43:27 -0400 Subject: [PATCH 2/3] Tweak names and comments. Signed-off-by: Matt Welke --- pkg/validationrule/validation_rule.go | 29 ++++++++++++++------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/pkg/validationrule/validation_rule.go b/pkg/validationrule/validation_rule.go index 74cf227f..1cc19749 100644 --- a/pkg/validationrule/validation_rule.go +++ b/pkg/validationrule/validation_rule.go @@ -1,33 +1,34 @@ -// Package validationrule describes validation rules in the Validator ecosystem. +// Package validationrule describes validation rules. package validationrule // Interface defines validation rule behavior. type Interface interface { // Name returns the name of the rule. Name() string - // SetName sets the name of the rule if it is a rule that supports manually specified names. + // SetName sets the name of the rule if it is a rule that requires manually specifying its name. + // This should be a no-op for rules that automatically generate their name. SetName(string) - // AllowsManualName returns whether the validation rule allows manually specifying its name. - // Code processing rules should check this first before trying to set a manual name. - AllowsManualName() bool + // RequiresName returns whether the validation rule requires its name to be manually specified. + // This should return false for rules that automatically generate their name. + RequiresName() bool } -// ManuallyNamed can be embedded into a rule struct to indicate that the rule allows its name to be -// manually specified. +// ManuallyNamed can be embedded into a rule struct to indicate that the rule requires its name to +// be manually specified. type ManuallyNamed struct{} -// AllowsManualName returns true, indicating that the rule supports manually specifying its name. -func (ManuallyNamed) AllowsManualName() bool { +// RequiresName returns true. +func (ManuallyNamed) RequiresName() bool { return true } -// AutomaticallyNamed can be embedded into a rule struct to indicate that the rule does not allow its -// name to be manually specified. +// AutomaticallyNamed can be embedded into a rule struct to indicate that the rule does not require +// its name to be manually specified because it is automatically generated from other data in the +// rule. type AutomaticallyNamed struct{} -// AllowsManualName returns false, indicating that the rule does not support manually specifying its -// name. -func (AutomaticallyNamed) AllowsManualName() bool { +// RequiresName returns false. +func (AutomaticallyNamed) RequiresName() bool { return false } From 75b3b3563eb1f3c6b9ac30e9aa79073dab57fb09 Mon Sep 17 00:00:00 2001 From: Matt Welke Date: Thu, 22 Aug 2024 13:42:19 -0400 Subject: [PATCH 3/3] Rename `ValidationResult` interface. Signed-off-by: Matt Welke --- pkg/validationresult/validation_result.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/validationresult/validation_result.go b/pkg/validationresult/validation_result.go index 81e18b28..eee4c7b2 100644 --- a/pkg/validationresult/validation_result.go +++ b/pkg/validationresult/validation_result.go @@ -26,16 +26,16 @@ type Patcher interface { Patch(ctx context.Context, obj client.Object, opts ...patch.Option) error } -// ValidationRule is an interface for defining validation rules that are used to build a ValidationResult. -type ValidationRule interface { +// Validator is an interface for building ValidationResults. +type Validator interface { client.Object GetKind() string PluginCode() string ResultCount() int } -// Build creates a new ValidationResult for a specific ValidationRule. -func Build(r ValidationRule) *v1alpha1.ValidationResult { +// Build creates a new ValidationResult for a specific Validator. +func Build(r Validator) *v1alpha1.ValidationResult { return &v1alpha1.ValidationResult{ TypeMeta: metav1.TypeMeta{ APIVersion: v1alpha1.APIVersion, @@ -61,8 +61,8 @@ func Build(r ValidationRule) *v1alpha1.ValidationResult { } } -// Name returns the name of a ValidationResult for a specific ValidationRule. -func Name(r ValidationRule) string { +// Name returns the name of a ValidationResult for a specific Validator. +func Name(r Validator) string { name := fmt.Sprintf("validator-plugin-%s-%s", r.PluginCode(), r.GetName()) return util.Sanitize(name) }