Skip to content

Commit

Permalink
fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lovromazgon committed Feb 15, 2024
1 parent 9288f51 commit 4b5e562
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
8 changes: 4 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions config/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 4 additions & 2 deletions config/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand Down
23 changes: 18 additions & 5 deletions config/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package config

import (
"errors"
"regexp"
"testing"

Expand Down Expand Up @@ -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
Expand All @@ -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 := &parameterv1.Parameter{Type: tc.protoType}
want := Parameter{Type: tc.goType}

Expand All @@ -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 := &parameterv1.Parameter{Type: tc.protoType}

Expand All @@ -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
Expand Down Expand Up @@ -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 := &parameterv1.Parameter{
Validations: []*parameterv1.Validation{tc.protoType},
}
Expand All @@ -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},
}
Expand All @@ -200,3 +201,15 @@ func TestParameter_Validation(t *testing.T) {
}
})
}

func TestParameter_Validation_InvalidType(t *testing.T) {
is := is.New(t)
have := &parameterv1.Parameter{
Validations: []*parameterv1.Validation{
{Type: parameterv1.Validation_TYPE_UNSPECIFIED},
},
}
var got Parameter
err := got.FromProto(have)
is.True(errors.Is(err, ErrInvalidValidationType))
}
4 changes: 2 additions & 2 deletions config/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 4b5e562

Please sign in to comment.