Refactor code to use 'any' type and improve context handling
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
18
db/db.go
18
db/db.go
@@ -168,7 +168,7 @@ func looksLikePostgresDSN(dsn string) bool {
|
||||
}
|
||||
|
||||
// ConvertToSQLParams is a utility function that generically converts a struct to a corresponding sqlc-generated struct
|
||||
func ConvertToSQLParams(input interface{}, output interface{}) {
|
||||
func ConvertToSQLParams(input any, output any) {
|
||||
inputVal := reflect.ValueOf(input).Elem()
|
||||
outputVal := reflect.ValueOf(output).Elem()
|
||||
|
||||
@@ -182,15 +182,15 @@ func ConvertToSQLParams(input interface{}, output interface{}) {
|
||||
|
||||
// Handle fields of type sql.NullString, sql.NullInt64, and normal string/int64 fields
|
||||
switch outputField.Type() {
|
||||
case reflect.TypeOf(sql.NullString{}):
|
||||
case reflect.TypeFor[sql.NullString]():
|
||||
// Handle sql.NullString
|
||||
if inputField.Kind() == reflect.Ptr && inputField.IsNil() {
|
||||
if inputField.Kind() == reflect.Pointer && inputField.IsNil() {
|
||||
outputField.Set(reflect.ValueOf(sql.NullString{Valid: false}))
|
||||
} else {
|
||||
outputField.Set(reflect.ValueOf(sql.NullString{String: inputField.String(), Valid: true}))
|
||||
}
|
||||
|
||||
case reflect.TypeOf(sql.NullInt64{}):
|
||||
case reflect.TypeFor[sql.NullInt64]():
|
||||
// Handle sql.NullInt64
|
||||
if inputField.Int() == 0 {
|
||||
outputField.Set(reflect.ValueOf(sql.NullInt64{Valid: false}))
|
||||
@@ -198,7 +198,7 @@ func ConvertToSQLParams(input interface{}, output interface{}) {
|
||||
outputField.Set(reflect.ValueOf(sql.NullInt64{Int64: inputField.Int(), Valid: true}))
|
||||
}
|
||||
|
||||
case reflect.TypeOf(sql.NullFloat64{}):
|
||||
case reflect.TypeFor[sql.NullFloat64]():
|
||||
// Handle sql.NullFloat64
|
||||
if inputField.Float() == 0 {
|
||||
outputField.Set(reflect.ValueOf(sql.NullFloat64{Valid: false}))
|
||||
@@ -206,19 +206,19 @@ func ConvertToSQLParams(input interface{}, output interface{}) {
|
||||
outputField.Set(reflect.ValueOf(sql.NullFloat64{Float64: inputField.Float(), Valid: true}))
|
||||
}
|
||||
|
||||
case reflect.TypeOf(""):
|
||||
case reflect.TypeFor[string]():
|
||||
// Handle normal string fields
|
||||
if inputField.Kind() == reflect.Ptr && inputField.IsNil() {
|
||||
if inputField.Kind() == reflect.Pointer && inputField.IsNil() {
|
||||
outputField.SetString("") // Set to empty string if input is nil
|
||||
} else {
|
||||
outputField.SetString(inputField.String())
|
||||
}
|
||||
|
||||
case reflect.TypeOf(int64(0)):
|
||||
case reflect.TypeFor[int64]():
|
||||
// Handle normal int64 fields
|
||||
outputField.SetInt(inputField.Int())
|
||||
|
||||
case reflect.TypeOf(float64(0)):
|
||||
case reflect.TypeFor[float64]():
|
||||
// Handle normal float64 fields
|
||||
outputField.SetFloat(inputField.Float())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user