Skip to content

Commit

Permalink
Refactor converter
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Jun 19, 2024
1 parent 1731a03 commit 9a4d075
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
67 changes: 34 additions & 33 deletions pkg/connection/prepared_stmt_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,46 @@ import (

func convertArg(arg driver.Value, colType types.SqlQueryColumnType) (interface{}, error) {
dataType := colType.Type
if dataType == "DOUBLE" {
if intArg, ok := arg.(int64); ok {
return jsonDoubleValue(float64(intArg)), nil
switch dataType {
case "DOUBLE":
switch arg := arg.(type) {
case int:
return jsonDoubleValue(float64(arg)), nil
case int32:
return jsonDoubleValue(float64(arg)), nil
case int64:
return jsonDoubleValue(float64(arg)), nil
case float32:
return jsonDoubleValue(float64(arg)), nil
case float64:
return jsonDoubleValue(arg), nil
default:
return nil, errors.NewInvalidArgType(arg, dataType)
}
if intArg, ok := arg.(int32); ok {
return jsonDoubleValue(float64(intArg)), nil
}
if intArg, ok := arg.(int); ok {
return jsonDoubleValue(float64(intArg)), nil
}
if floatArg, ok := arg.(float64); ok {
return jsonDoubleValue(floatArg), nil
}
if floatArg, ok := arg.(float32); ok {
return jsonDoubleValue(float64(floatArg)), nil
}
return nil, errors.NewInvalidArgType(arg, dataType)
}
if dataType == "TIMESTAMP" || dataType == "TIMESTAMP WITH LOCAL TIME ZONE" {
if timeArg, ok := arg.(time.Time); ok {
return jsonTimestampValue(timeArg), nil
}
if stringArg, ok := arg.(string); ok {
case "TIMESTAMP", "TIMESTAMP WITH LOCAL TIME ZONE":
switch arg := arg.(type) {
case time.Time:
return jsonTimestampValue(arg), nil
case string:
// We assume strings are already formatted correctly
return stringArg, nil
}
return nil, errors.NewInvalidArgType(arg, dataType)
}
if dataType == "DATE" {
if timeArg, ok := arg.(time.Time); ok {
return jsonDateValue(timeArg), nil
return arg, nil
default:
return nil, errors.NewInvalidArgType(arg, dataType)
}
if stringArg, ok := arg.(string); ok {
case "DATE":
switch arg := arg.(type) {
case time.Time:
return jsonDateValue(arg), nil
case string:
// We assume strings are already formatted correctly
return stringArg, nil
return arg, nil
default:
return nil, errors.NewInvalidArgType(arg, dataType)
}
return nil, errors.NewInvalidArgType(arg, dataType)
default:
// No need to convert other types
return arg, nil
}
return arg, nil
}

func jsonDoubleValue(value float64) json.Marshaler {
Expand Down
4 changes: 4 additions & 0 deletions pkg/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,7 @@ func (suite *ErrorsTestSuite) TestLogJsonDecodingError() {
func (suite *ErrorsTestSuite) TestNewInvalidConnectionStringInvalidPort() {
suite.EqualError(NewInvalidConnectionStringInvalidPort("port"), "E-EGOD-23: invalid `port` value 'port', numeric port expected")
}

func (suite *ErrorsTestSuite) TestNewInvalidArgType() {
suite.EqualError(NewInvalidArgType("arg", "expected Type"), "E-EGOD-30: cannot convert argument 'arg' of type 'string' to 'expected Type' type")
}

0 comments on commit 9a4d075

Please sign in to comment.