Skip to content

Commit

Permalink
rename proto folder parameter to config
Browse files Browse the repository at this point in the history
  • Loading branch information
lovromazgon committed Feb 15, 2024
1 parent 13f4b2a commit 73990fa
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 76 deletions.
66 changes: 33 additions & 33 deletions config/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,37 @@ import (
"strconv"
"strings"

parameterv1 "github.com/conduitio/conduit-commons/proto/parameter/v1"
configv1 "github.com/conduitio/conduit-commons/proto/config/v1"
)

func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
var cTypes [1]struct{}
_ = cTypes[int(ParameterTypeString)-int(parameterv1.Parameter_TYPE_STRING)]
_ = cTypes[int(ParameterTypeInt)-int(parameterv1.Parameter_TYPE_INT)]
_ = cTypes[int(ParameterTypeFloat)-int(parameterv1.Parameter_TYPE_FLOAT)]
_ = cTypes[int(ParameterTypeBool)-int(parameterv1.Parameter_TYPE_BOOL)]
_ = cTypes[int(ParameterTypeFile)-int(parameterv1.Parameter_TYPE_FILE)]
_ = cTypes[int(ParameterTypeDuration)-int(parameterv1.Parameter_TYPE_DURATION)]
_ = cTypes[int(ParameterTypeString)-int(configv1.Parameter_TYPE_STRING)]
_ = cTypes[int(ParameterTypeInt)-int(configv1.Parameter_TYPE_INT)]
_ = cTypes[int(ParameterTypeFloat)-int(configv1.Parameter_TYPE_FLOAT)]
_ = cTypes[int(ParameterTypeBool)-int(configv1.Parameter_TYPE_BOOL)]
_ = cTypes[int(ParameterTypeFile)-int(configv1.Parameter_TYPE_FILE)]
_ = cTypes[int(ParameterTypeDuration)-int(configv1.Parameter_TYPE_DURATION)]
}

func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
var cTypes [1]struct{}
_ = cTypes[int(ValidationTypeRequired)-int(parameterv1.Validation_TYPE_REQUIRED)]
_ = cTypes[int(ValidationTypeGreaterThan)-int(parameterv1.Validation_TYPE_GREATER_THAN)]
_ = cTypes[int(ValidationTypeLessThan)-int(parameterv1.Validation_TYPE_LESS_THAN)]
_ = cTypes[int(ValidationTypeInclusion)-int(parameterv1.Validation_TYPE_INCLUSION)]
_ = cTypes[int(ValidationTypeExclusion)-int(parameterv1.Validation_TYPE_EXCLUSION)]
_ = cTypes[int(ValidationTypeRegex)-int(parameterv1.Validation_TYPE_REGEX)]
_ = cTypes[int(ValidationTypeRequired)-int(configv1.Validation_TYPE_REQUIRED)]
_ = cTypes[int(ValidationTypeGreaterThan)-int(configv1.Validation_TYPE_GREATER_THAN)]
_ = cTypes[int(ValidationTypeLessThan)-int(configv1.Validation_TYPE_LESS_THAN)]
_ = cTypes[int(ValidationTypeInclusion)-int(configv1.Validation_TYPE_INCLUSION)]
_ = cTypes[int(ValidationTypeExclusion)-int(configv1.Validation_TYPE_EXCLUSION)]
_ = cTypes[int(ValidationTypeRegex)-int(configv1.Validation_TYPE_REGEX)]
}

// -- From Proto To Parameter --------------------------------------------------

// FromProto takes data from the supplied proto object and populates the
// receiver. If the proto object is nil, the receiver is set to its zero value.
// If the function returns an error, the receiver could be partially populated.
func (p *Parameters) FromProto(proto map[string]*parameterv1.Parameter) error {
func (p *Parameters) FromProto(proto map[string]*configv1.Parameter) error {
if proto == nil {
*p = nil
return nil
Expand All @@ -71,7 +71,7 @@ func (p *Parameters) FromProto(proto map[string]*parameterv1.Parameter) error {
// FromProto takes data from the supplied proto object and populates the
// receiver. If the proto object is nil, the receiver is set to its zero value.
// If the function returns an error, the receiver could be partially populated.
func (p *Parameter) FromProto(proto *parameterv1.Parameter) error {
func (p *Parameter) FromProto(proto *configv1.Parameter) error {
if proto == nil {
*p = Parameter{}
return nil
Expand All @@ -89,7 +89,7 @@ func (p *Parameter) FromProto(proto *parameterv1.Parameter) error {
return nil
}

func validationsFromProto(proto []*parameterv1.Validation) ([]Validation, error) {
func validationsFromProto(proto []*configv1.Validation) ([]Validation, error) {
if proto == nil {
return nil, nil
}
Expand All @@ -105,37 +105,37 @@ func validationsFromProto(proto []*parameterv1.Validation) ([]Validation, error)
return validations, nil
}

func validationFromProto(proto *parameterv1.Validation) (Validation, error) {
func validationFromProto(proto *configv1.Validation) (Validation, error) {
if proto == nil {
return nil, nil //nolint:nilnil // This is the expected behavior.
}

switch proto.Type {
case parameterv1.Validation_TYPE_REQUIRED:
case configv1.Validation_TYPE_REQUIRED:
return ValidationRequired{}, nil
case parameterv1.Validation_TYPE_GREATER_THAN:
case configv1.Validation_TYPE_GREATER_THAN:
v, err := strconv.ParseFloat(proto.Value, 64)
if err != nil {
return nil, fmt.Errorf("error parsing greater than value: %w", err)
}
return ValidationGreaterThan{V: v}, nil
case parameterv1.Validation_TYPE_LESS_THAN:
case configv1.Validation_TYPE_LESS_THAN:
v, err := strconv.ParseFloat(proto.Value, 64)
if err != nil {
return nil, fmt.Errorf("error parsing less than value: %w", err)
}
return ValidationLessThan{V: v}, nil
case parameterv1.Validation_TYPE_INCLUSION:
case configv1.Validation_TYPE_INCLUSION:
return ValidationInclusion{List: strings.Split(proto.Value, ",")}, nil
case parameterv1.Validation_TYPE_EXCLUSION:
case configv1.Validation_TYPE_EXCLUSION:
return ValidationExclusion{List: strings.Split(proto.Value, ",")}, nil
case parameterv1.Validation_TYPE_REGEX:
case configv1.Validation_TYPE_REGEX:
regex, err := regexp.Compile(proto.Value)
if err != nil {
return nil, fmt.Errorf("error compiling regex: %w", err)
}
return ValidationRegex{Regex: regex}, nil
case parameterv1.Validation_TYPE_UNSPECIFIED:
case configv1.Validation_TYPE_UNSPECIFIED:
fallthrough
default:
return nil, fmt.Errorf("%v: %w", proto.Type, ErrInvalidValidationType)
Expand All @@ -145,38 +145,38 @@ func validationFromProto(proto *parameterv1.Validation) (Validation, error) {
// -- From Parameter To Proto --------------------------------------------------

// ToProto takes data from the receiver and populates the supplied proto object.
func (p Parameters) ToProto(proto map[string]*parameterv1.Parameter) {
func (p Parameters) ToProto(proto map[string]*configv1.Parameter) {
clear(proto)
for k, param := range p {
var v parameterv1.Parameter
var v configv1.Parameter
param.ToProto(&v)
proto[k] = &v
}
}

// ToProto takes data from the receiver and populates the supplied proto object.
func (p Parameter) ToProto(proto *parameterv1.Parameter) {
func (p Parameter) ToProto(proto *configv1.Parameter) {
proto.Default = p.Default
proto.Description = p.Description
proto.Type = parameterv1.Parameter_Type(p.Type)
proto.Type = configv1.Parameter_Type(p.Type)
proto.Validations = validationsToProto(p.Validations)
}

func validationsToProto(validations []Validation) []*parameterv1.Validation {
func validationsToProto(validations []Validation) []*configv1.Validation {
if validations == nil {
return nil
}

proto := make([]*parameterv1.Validation, len(validations))
proto := make([]*configv1.Validation, len(validations))
for i, v := range validations {
proto[i] = validationToProto(v)
}
return proto
}

func validationToProto(validation Validation) *parameterv1.Validation {
return &parameterv1.Validation{
Type: parameterv1.Validation_Type(validation.Type()),
func validationToProto(validation Validation) *configv1.Validation {
return &configv1.Validation{
Type: configv1.Validation_Type(validation.Type()),
Value: validation.Value(),
}
}
86 changes: 43 additions & 43 deletions config/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ import (
"regexp"
"testing"

parameterv1 "github.com/conduitio/conduit-commons/proto/parameter/v1"
configv1 "github.com/conduitio/conduit-commons/proto/config/v1"
"github.com/matryer/is"
)

func TestParameter_FromProto(t *testing.T) {
is := is.New(t)

have := &parameterv1.Parameter{
have := &configv1.Parameter{
Description: "test-description",
Default: "test-default",
Type: parameterv1.Parameter_TYPE_STRING,
Validations: []*parameterv1.Validation{
{Type: parameterv1.Validation_TYPE_REQUIRED},
{Type: parameterv1.Validation_TYPE_GREATER_THAN, Value: "1.2"},
{Type: parameterv1.Validation_TYPE_LESS_THAN, Value: "3.4"},
{Type: parameterv1.Validation_TYPE_INCLUSION, Value: "1,2,3"},
{Type: parameterv1.Validation_TYPE_EXCLUSION, Value: "4,5,6"},
{Type: parameterv1.Validation_TYPE_REGEX, Value: "test-regex"},
Type: configv1.Parameter_TYPE_STRING,
Validations: []*configv1.Validation{
{Type: configv1.Validation_TYPE_REQUIRED},
{Type: configv1.Validation_TYPE_GREATER_THAN, Value: "1.2"},
{Type: configv1.Validation_TYPE_LESS_THAN, Value: "3.4"},
{Type: configv1.Validation_TYPE_INCLUSION, Value: "1,2,3"},
{Type: configv1.Validation_TYPE_EXCLUSION, Value: "4,5,6"},
{Type: configv1.Validation_TYPE_REGEX, Value: "test-regex"},
},
}

Expand Down Expand Up @@ -73,41 +73,41 @@ func TestParameter_ToProto(t *testing.T) {
},
}

want := &parameterv1.Parameter{
want := &configv1.Parameter{
Description: "test-description",
Default: "test-default",
Type: parameterv1.Parameter_TYPE_STRING,
Validations: []*parameterv1.Validation{
{Type: parameterv1.Validation_TYPE_REQUIRED},
{Type: parameterv1.Validation_TYPE_REGEX, Value: "test-regex"},
Type: configv1.Parameter_TYPE_STRING,
Validations: []*configv1.Validation{
{Type: configv1.Validation_TYPE_REQUIRED},
{Type: configv1.Validation_TYPE_REGEX, Value: "test-regex"},
},
}

got := &parameterv1.Parameter{}
got := &configv1.Parameter{}
have.ToProto(got)
is.Equal(want, got)
}

func TestParameter_ParameterTypes(t *testing.T) {
testCases := []struct {
protoType parameterv1.Parameter_Type
protoType configv1.Parameter_Type
goType ParameterType
}{
{protoType: parameterv1.Parameter_TYPE_UNSPECIFIED, goType: 0},
{protoType: parameterv1.Parameter_TYPE_STRING, goType: ParameterTypeString},
{protoType: parameterv1.Parameter_TYPE_INT, goType: ParameterTypeInt},
{protoType: parameterv1.Parameter_TYPE_FLOAT, goType: ParameterTypeFloat},
{protoType: parameterv1.Parameter_TYPE_BOOL, goType: ParameterTypeBool},
{protoType: parameterv1.Parameter_TYPE_FILE, goType: ParameterTypeFile},
{protoType: parameterv1.Parameter_TYPE_DURATION, goType: ParameterTypeDuration},
{protoType: parameterv1.Parameter_Type(100), goType: 100},
{protoType: configv1.Parameter_TYPE_UNSPECIFIED, goType: 0},
{protoType: configv1.Parameter_TYPE_STRING, goType: ParameterTypeString},
{protoType: configv1.Parameter_TYPE_INT, goType: ParameterTypeInt},
{protoType: configv1.Parameter_TYPE_FLOAT, goType: ParameterTypeFloat},
{protoType: configv1.Parameter_TYPE_BOOL, goType: ParameterTypeBool},
{protoType: configv1.Parameter_TYPE_FILE, goType: ParameterTypeFile},
{protoType: configv1.Parameter_TYPE_DURATION, goType: ParameterTypeDuration},
{protoType: configv1.Parameter_Type(100), goType: 100},
}

t.Run("FromProto", func(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.goType.String(), func(*testing.T) {
is := is.New(t)
have := &parameterv1.Parameter{Type: tc.protoType}
have := &configv1.Parameter{Type: tc.protoType}
want := Parameter{Type: tc.goType}

var got Parameter
Expand All @@ -123,9 +123,9 @@ func TestParameter_ParameterTypes(t *testing.T) {
t.Run(tc.goType.String(), func(t *testing.T) {
is := is.New(t)
have := Parameter{Type: tc.goType}
want := &parameterv1.Parameter{Type: tc.protoType}
want := &configv1.Parameter{Type: tc.protoType}

got := &parameterv1.Parameter{}
got := &configv1.Parameter{}
have.ToProto(got)
is.Equal(want, got)
})
Expand All @@ -135,31 +135,31 @@ func TestParameter_ParameterTypes(t *testing.T) {

func TestParameter_Validation(t *testing.T) {
testCases := []struct {
protoType *parameterv1.Validation
protoType *configv1.Validation
goType Validation
}{
{
protoType: &parameterv1.Validation{Type: parameterv1.Validation_TYPE_REQUIRED},
protoType: &configv1.Validation{Type: configv1.Validation_TYPE_REQUIRED},
goType: ValidationRequired{},
},
{
protoType: &parameterv1.Validation{Type: parameterv1.Validation_TYPE_GREATER_THAN, Value: "1.2"},
protoType: &configv1.Validation{Type: configv1.Validation_TYPE_GREATER_THAN, Value: "1.2"},
goType: ValidationGreaterThan{V: 1.2},
},
{
protoType: &parameterv1.Validation{Type: parameterv1.Validation_TYPE_LESS_THAN, Value: "3.4"},
protoType: &configv1.Validation{Type: configv1.Validation_TYPE_LESS_THAN, Value: "3.4"},
goType: ValidationLessThan{V: 3.4},
},
{
protoType: &parameterv1.Validation{Type: parameterv1.Validation_TYPE_INCLUSION, Value: "1,2,3"},
protoType: &configv1.Validation{Type: configv1.Validation_TYPE_INCLUSION, Value: "1,2,3"},
goType: ValidationInclusion{List: []string{"1", "2", "3"}},
},
{
protoType: &parameterv1.Validation{Type: parameterv1.Validation_TYPE_EXCLUSION, Value: "4,5,6"},
protoType: &configv1.Validation{Type: configv1.Validation_TYPE_EXCLUSION, Value: "4,5,6"},
goType: ValidationExclusion{List: []string{"4", "5", "6"}},
},
{
protoType: &parameterv1.Validation{Type: parameterv1.Validation_TYPE_REGEX, Value: "test-regex"},
protoType: &configv1.Validation{Type: configv1.Validation_TYPE_REGEX, Value: "test-regex"},
goType: ValidationRegex{Regex: regexp.MustCompile("test-regex")},
},
}
Expand All @@ -168,8 +168,8 @@ func TestParameter_Validation(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},
have := &configv1.Parameter{
Validations: []*configv1.Validation{tc.protoType},
}
want := Parameter{
Validations: []Validation{tc.goType},
Expand All @@ -190,11 +190,11 @@ func TestParameter_Validation(t *testing.T) {
have := Parameter{
Validations: []Validation{tc.goType},
}
want := &parameterv1.Parameter{
Validations: []*parameterv1.Validation{tc.protoType},
want := &configv1.Parameter{
Validations: []*configv1.Validation{tc.protoType},
}

got := &parameterv1.Parameter{}
got := &configv1.Parameter{}
have.ToProto(got)
is.Equal(want, got)
})
Expand All @@ -204,9 +204,9 @@ 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},
have := &configv1.Parameter{
Validations: []*configv1.Validation{
{Type: configv1.Validation_TYPE_UNSPECIFIED},
},
}
var got Parameter
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 73990fa

Please sign in to comment.