From 4b5e5620100326ac2650702dc2bacebcb3a6d5d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lovro=20Ma=C5=BEgon?= Date: Thu, 15 Feb 2024 14:57:29 +0100 Subject: [PATCH] fix linter errors --- config/config.go | 8 ++++---- config/config_test.go | 2 +- config/errors.go | 5 +++-- config/proto.go | 6 ++++-- config/proto_test.go | 23 ++++++++++++++++++----- config/validation.go | 4 ++-- 6 files changed, 32 insertions(+), 16 deletions(-) diff --git a/config/config.go b/config/config.go index f9f70f0..b0cd4c1 100644 --- a/config/config.go +++ b/config/config.go @@ -99,22 +99,22 @@ func (c Config) validateParamType(key string, param Parameter) error { case ParameterTypeInt: _, err := strconv.Atoi(value) if err != nil { - return fmt.Errorf("error validating %q: %q value is not an integer: %w", key, value, ErrInvalidParamType) + return fmt.Errorf("error validating %q: %q value is not an integer: %w", key, value, ErrInvalidParameterType) } case ParameterTypeFloat: _, err := strconv.ParseFloat(value, 64) if err != nil { - return fmt.Errorf("error validating %q: %q value is not a float: %w", key, value, ErrInvalidParamType) + return fmt.Errorf("error validating %q: %q value is not a float: %w", key, value, ErrInvalidParameterType) } case ParameterTypeDuration: _, err := time.ParseDuration(value) if err != nil { - return fmt.Errorf("error validating %q: %q value is not a duration: %w", key, value, ErrInvalidParamType) + return fmt.Errorf("error validating %q: %q value is not a duration: %w", key, value, ErrInvalidParameterType) } case ParameterTypeBool: _, err := strconv.ParseBool(value) if err != nil { - return fmt.Errorf("error validating %q: %q value is not a boolean: %w", key, value, ErrInvalidParamType) + return fmt.Errorf("error validating %q: %q value is not a boolean: %w", key, value, ErrInvalidParameterType) } } return nil diff --git a/config/config_test.go b/config/config_test.go index 69d9edf..b4d9ad0 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -102,7 +102,7 @@ func TestConfig_Validate_ParameterType(t *testing.T) { Validate(tt.params) if err != nil && tt.wantErr { - is.True(errors.Is(err, ErrInvalidParamType)) + is.True(errors.Is(err, ErrInvalidParameterType)) } else if err != nil || tt.wantErr { t.Errorf("UtilityFunc() error = %v, wantErr %v", err, tt.wantErr) } diff --git a/config/errors.go b/config/errors.go index e2de977..edd2a9f 100644 --- a/config/errors.go +++ b/config/errors.go @@ -18,8 +18,9 @@ import "errors" var ( ErrUnrecognizedParameter = errors.New("unrecognized parameter") - ErrInvalidParamValue = errors.New("invalid parameter value") - ErrInvalidParamType = errors.New("invalid parameter type") + ErrInvalidParameterValue = errors.New("invalid parameter value") + ErrInvalidParameterType = errors.New("invalid parameter type") + ErrInvalidValidationType = errors.New("invalid validation type") ErrRequiredParameterMissing = errors.New("required parameter is not provided") ErrLessThanValidationFail = errors.New("less-than validation failed") diff --git a/config/proto.go b/config/proto.go index ea471ae..e0c94e8 100644 --- a/config/proto.go +++ b/config/proto.go @@ -107,7 +107,7 @@ func validationsFromProto(proto []*parameterv1.Validation) ([]Validation, error) func validationFromProto(proto *parameterv1.Validation) (Validation, error) { if proto == nil { - return nil, nil + return nil, nil //nolint:nilnil // This is the expected behavior. } switch proto.Type { @@ -135,8 +135,10 @@ func validationFromProto(proto *parameterv1.Validation) (Validation, error) { return nil, fmt.Errorf("error compiling regex: %w", err) } return ValidationRegex{Regex: regex}, nil + case parameterv1.Validation_TYPE_UNSPECIFIED: + fallthrough default: - return nil, fmt.Errorf("unknown validation type: %v", proto.Type) + return nil, fmt.Errorf("%v: %w", proto.Type, ErrInvalidValidationType) } } diff --git a/config/proto_test.go b/config/proto_test.go index 910a363..112a3a0 100644 --- a/config/proto_test.go +++ b/config/proto_test.go @@ -15,6 +15,7 @@ package config import ( + "errors" "regexp" "testing" @@ -88,8 +89,6 @@ func TestParameter_ToProto(t *testing.T) { } func TestParameter_ParameterTypes(t *testing.T) { - is := is.New(t) - testCases := []struct { protoType parameterv1.Parameter_Type goType ParameterType @@ -106,7 +105,8 @@ func TestParameter_ParameterTypes(t *testing.T) { t.Run("FromProto", func(t *testing.T) { for _, tc := range testCases { - t.Run(tc.goType.String(), func(t *testing.T) { + t.Run(tc.goType.String(), func(*testing.T) { + is := is.New(t) have := ¶meterv1.Parameter{Type: tc.protoType} want := Parameter{Type: tc.goType} @@ -121,6 +121,7 @@ func TestParameter_ParameterTypes(t *testing.T) { t.Run("ToProto", func(t *testing.T) { for _, tc := range testCases { t.Run(tc.goType.String(), func(t *testing.T) { + is := is.New(t) have := Parameter{Type: tc.goType} want := ¶meterv1.Parameter{Type: tc.protoType} @@ -133,8 +134,6 @@ func TestParameter_ParameterTypes(t *testing.T) { } func TestParameter_Validation(t *testing.T) { - is := is.New(t) - testCases := []struct { protoType *parameterv1.Validation goType Validation @@ -168,6 +167,7 @@ func TestParameter_Validation(t *testing.T) { t.Run("FromProto", func(t *testing.T) { for _, tc := range testCases { t.Run(tc.goType.Type().String(), func(t *testing.T) { + is := is.New(t) have := ¶meterv1.Parameter{ Validations: []*parameterv1.Validation{tc.protoType}, } @@ -186,6 +186,7 @@ func TestParameter_Validation(t *testing.T) { t.Run("ToProto", func(t *testing.T) { for _, tc := range testCases { t.Run(tc.goType.Type().String(), func(t *testing.T) { + is := is.New(t) have := Parameter{ Validations: []Validation{tc.goType}, } @@ -200,3 +201,15 @@ func TestParameter_Validation(t *testing.T) { } }) } + +func TestParameter_Validation_InvalidType(t *testing.T) { + is := is.New(t) + have := ¶meterv1.Parameter{ + Validations: []*parameterv1.Validation{ + {Type: parameterv1.Validation_TYPE_UNSPECIFIED}, + }, + } + var got Parameter + err := got.FromProto(have) + is.True(errors.Is(err, ErrInvalidValidationType)) +} diff --git a/config/validation.go b/config/validation.go index 7df011a..1425022 100644 --- a/config/validation.go +++ b/config/validation.go @@ -62,7 +62,7 @@ func (v ValidationGreaterThan) Value() string { return strconv.FormatFloa func (v ValidationGreaterThan) Validate(value string) error { val, err := strconv.ParseFloat(value, 64) if err != nil { - return fmt.Errorf("%q value should be a number: %w", value, ErrInvalidParamValue) + return fmt.Errorf("%q value should be a number: %w", value, ErrInvalidParameterValue) } if !(val > v.V) { formatted := strconv.FormatFloat(v.V, 'f', -1, 64) @@ -80,7 +80,7 @@ func (v ValidationLessThan) Value() string { return strconv.FormatFloat(v func (v ValidationLessThan) Validate(value string) error { val, err := strconv.ParseFloat(value, 64) if err != nil { - return fmt.Errorf("%q value should be a number: %w", value, ErrInvalidParamValue) + return fmt.Errorf("%q value should be a number: %w", value, ErrInvalidParameterValue) } if !(val < v.V) { formatted := strconv.FormatFloat(v.V, 'f', -1, 64)