Skip to content

Commit

Permalink
refactoring changes
Browse files Browse the repository at this point in the history
  • Loading branch information
maksenius committed Feb 6, 2023
1 parent 5b8e563 commit bb93f2c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 36 deletions.
13 changes: 7 additions & 6 deletions columntypes/columntypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type TableInfo struct {
}

// GetColumnQueryPart prepare query part about creation column for tracking table.
// For example: NAME VARCHAR(40), AGE INT, ADDRESS VARCHAR(120).
func (t TableInfo) GetColumnQueryPart() string {
var columns []string
for key, val := range t.ColumnTypes {
Expand Down Expand Up @@ -148,9 +149,9 @@ func GetTableInfo(ctx context.Context, querier Querier, tableName string) (Table

for rows.Next() {
var count int
er := rows.Scan(&count)
if er != nil {
return TableInfo{}, fmt.Errorf("scan: %w", err)
scanErr := rows.Scan(&count)
if scanErr != nil {
return TableInfo{}, fmt.Errorf("scan: %w", scanErr)
}

if count == 0 {
Expand Down Expand Up @@ -206,8 +207,8 @@ func GetTableInfo(ctx context.Context, querier Querier, tableName string) (Table
}, nil
}

// ConvertStructureData converts a sdk.StructureData values to a proper database types.
func ConvertStructureData(
// ConvertStructuredData converts a sdk.StructureData values to a proper database types.
func ConvertStructuredData(
ctx context.Context,
columnTypes map[string]string,
data sdk.StructuredData,
Expand Down Expand Up @@ -236,7 +237,7 @@ func ConvertStructureData(
}

// Converting value to time if it is string.
switch columnTypes[strings.ToLower(key)] {
switch columnTypes[strings.ToUpper(key)] {
case dateType, timeType, secondDateType, timestampType:
_, ok := value.(time.Time)
if ok {
Expand Down
26 changes: 17 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

package config

import (
"fmt"
)

const (
// DSNAuthType name of DSN auth.
DSNAuthType string = "DSN"
Expand Down Expand Up @@ -59,45 +63,49 @@ func (a *AuthConfig) Validate() error {
switch a.Mechanism {
case DSNAuthType:
if a.DSN == "" {
return errRequiredDSNParameter
return requiredAuthParam(DSNAuthType, "DSN")
}

return nil
case BasicAuthType:
if a.Host == "" {
return errRequiredHostParameter
return requiredAuthParam(BasicAuthType, "host")
}
if a.Username == "" {
return errRequiredUsernameParameter
return requiredAuthParam(BasicAuthType, "username")
}
if a.Password == "" {
return errRequiredPasswordParameter
return requiredAuthParam(BasicAuthType, "password")
}

return nil
case JWTAuthType:
if a.Host == "" {
return errRequiredHostParameter
return requiredAuthParam(JWTAuthType, "host")
}
if a.Token == "" {
return errRequiredTokenParameter
return requiredAuthParam(JWTAuthType, "token")
}

return nil

case X509AuthType:
if a.Host == "" {
return errRequiredHostParameter
return requiredAuthParam(X509AuthType, "host")
}
if a.ClientKeyFilePath == "" {
return errRequiredClientKeyFileParameter
return requiredAuthParam(X509AuthType, "client key file path")
}
if a.ClientCertFilePath == "" {
return errRequiredClientCertFileParameter
return requiredAuthParam(X509AuthType, "client cert file path")
}

return nil
default:
return ErrInvalidAuthMechanism
}
}

func requiredAuthParam(auth, param string) error {
return fmt.Errorf("%s is required for %s auth", param, auth)
}
20 changes: 2 additions & 18 deletions config/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,5 @@ import (
"errors"
)

var (
// ErrInvalidAuthMechanism occurs when there's invalid mechanism config value.
ErrInvalidAuthMechanism = errors.New("invalid auth mechanism")
// errRequiredDSNParameter occurs when there's empty dsn parameter for dsn auth.
errRequiredDSNParameter = errors.New("dsn is required parameter for dsn auth")
// errRequiredHostParameter occurs when there's empty host parameter for basic or jwt or x509 auth.
errRequiredHostParameter = errors.New("host is required parameter for basic, jwt, x509 auth")
// errRequiredUsernameParameter occurs when there's empty username parameter for basic auth.
errRequiredUsernameParameter = errors.New("username is required parameter for basic auth")
// errRequiredPasswordParameter occurs when there's empty password parameter for basic auth.
errRequiredPasswordParameter = errors.New("password is required parameter for basic auth")
// errRequiredTokenParameter occurs when there's empty token parameter for jwt auth.
errRequiredTokenParameter = errors.New("token is required for jwt auth")
// errRequiredClientCertFileParameter occurs when there's empty client cert file parameter for x509 auth.
errRequiredClientCertFileParameter = errors.New("client cert file path is required for x509 auth")
// errRequiredClientCertFileParameter occurs when there's empty client key file path parameter for x509 auth.
errRequiredClientKeyFileParameter = errors.New("client key file path is required for x509 auth")
)
// ErrInvalidAuthMechanism occurs when there's invalid mechanism config value.
var ErrInvalidAuthMechanism = errors.New("invalid auth mechanism")
2 changes: 1 addition & 1 deletion helper/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ func ConnectToDB(c config.AuthConfig) (*sqlx.DB, error) {

return sqlx.NewDb(sql.OpenDB(con), driverName), nil
default:
return nil, config.ErrInvalidAuthMechanism
return nil, fmt.Errorf("%w:%s", config.ErrInvalidAuthMechanism, c.Mechanism)
}
}
2 changes: 1 addition & 1 deletion source/position/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func ParseSDKPosition(p sdk.Position) (*Position, error) {
var pos Position

if p == nil {
return nil, nil //nolint:nilnil // it is ok to return nils on this case
return &Position{}, nil
}

err := json.Unmarshal(p, &pos)
Expand Down
2 changes: 1 addition & 1 deletion source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func TestSource_Read(t *testing.T) {

it := mock.NewMockIterator(ctrl)
it.EXPECT().HasNext(ctx).Return(true, nil)
it.EXPECT().Next(ctx).Return(sdk.Record{}, errors.New("key is not exist"))
it.EXPECT().Next(ctx).Return(sdk.Record{}, errors.New("key does not exist"))

s := Source{
iterator: it,
Expand Down

0 comments on commit bb93f2c

Please sign in to comment.