mirror of https://github.com/jackc/pgx.git
96 lines
2.6 KiB
Go
96 lines
2.6 KiB
Go
package pgtype
|
|
|
|
import (
|
|
"reflect"
|
|
"time"
|
|
)
|
|
|
|
// underlyingIntType gets the underlying type that can be converted to Int2, Int4, or Int8
|
|
func underlyingIntType(val interface{}) (interface{}, bool) {
|
|
refVal := reflect.ValueOf(val)
|
|
|
|
switch refVal.Kind() {
|
|
case reflect.Ptr:
|
|
if refVal.IsNil() {
|
|
return nil, false
|
|
}
|
|
convVal := refVal.Elem().Interface()
|
|
return convVal, true
|
|
case reflect.Int:
|
|
convVal := int(refVal.Int())
|
|
return convVal, reflect.TypeOf(convVal) != refVal.Type()
|
|
case reflect.Int8:
|
|
convVal := int8(refVal.Int())
|
|
return convVal, reflect.TypeOf(convVal) != refVal.Type()
|
|
case reflect.Int16:
|
|
convVal := int16(refVal.Int())
|
|
return convVal, reflect.TypeOf(convVal) != refVal.Type()
|
|
case reflect.Int32:
|
|
convVal := int32(refVal.Int())
|
|
return convVal, reflect.TypeOf(convVal) != refVal.Type()
|
|
case reflect.Int64:
|
|
convVal := int64(refVal.Int())
|
|
return convVal, reflect.TypeOf(convVal) != refVal.Type()
|
|
case reflect.Uint:
|
|
convVal := uint(refVal.Uint())
|
|
return convVal, reflect.TypeOf(convVal) != refVal.Type()
|
|
case reflect.Uint8:
|
|
convVal := uint8(refVal.Uint())
|
|
return convVal, reflect.TypeOf(convVal) != refVal.Type()
|
|
case reflect.Uint16:
|
|
convVal := uint16(refVal.Uint())
|
|
return convVal, reflect.TypeOf(convVal) != refVal.Type()
|
|
case reflect.Uint32:
|
|
convVal := uint32(refVal.Uint())
|
|
return convVal, reflect.TypeOf(convVal) != refVal.Type()
|
|
case reflect.Uint64:
|
|
convVal := uint64(refVal.Uint())
|
|
return convVal, reflect.TypeOf(convVal) != refVal.Type()
|
|
case reflect.String:
|
|
convVal := refVal.String()
|
|
return convVal, reflect.TypeOf(convVal) != refVal.Type()
|
|
}
|
|
|
|
return nil, false
|
|
}
|
|
|
|
// underlyingBoolType gets the underlying type that can be converted to Bool
|
|
func underlyingBoolType(val interface{}) (interface{}, bool) {
|
|
refVal := reflect.ValueOf(val)
|
|
|
|
switch refVal.Kind() {
|
|
case reflect.Ptr:
|
|
if refVal.IsNil() {
|
|
return nil, false
|
|
}
|
|
convVal := refVal.Elem().Interface()
|
|
return convVal, true
|
|
case reflect.Bool:
|
|
convVal := refVal.Bool()
|
|
return convVal, reflect.TypeOf(convVal) != refVal.Type()
|
|
}
|
|
|
|
return nil, false
|
|
}
|
|
|
|
// underlyingTimeType gets the underlying type that can be converted to time.Time
|
|
func underlyingTimeType(val interface{}) (interface{}, bool) {
|
|
refVal := reflect.ValueOf(val)
|
|
|
|
switch refVal.Kind() {
|
|
case reflect.Ptr:
|
|
if refVal.IsNil() {
|
|
return time.Time{}, false
|
|
}
|
|
convVal := refVal.Elem().Interface()
|
|
return convVal, true
|
|
}
|
|
|
|
timeType := reflect.TypeOf(time.Time{})
|
|
if refVal.Type().ConvertibleTo(timeType) {
|
|
return refVal.Convert(timeType).Interface(), true
|
|
}
|
|
|
|
return time.Time{}, false
|
|
}
|