Special case the underlying type of []byte
Underlying types were already tried. But []byte is not a normal underlying type. It is a slice. But since is can be treated as a scalar instead of an array / slice we need to special case it. https://github.com/jackc/pgx/issues/1763
This commit is contained in:
@@ -1358,6 +1358,8 @@ var kindToTypes map[reflect.Kind]reflect.Type = map[reflect.Kind]reflect.Type{
|
||||
reflect.Bool: reflect.TypeOf(false),
|
||||
}
|
||||
|
||||
var byteSliceType = reflect.TypeOf([]byte{})
|
||||
|
||||
type underlyingTypeEncodePlan struct {
|
||||
nextValueType reflect.Type
|
||||
next EncodePlan
|
||||
@@ -1372,6 +1374,10 @@ func (plan *underlyingTypeEncodePlan) Encode(value any, buf []byte) (newBuf []by
|
||||
// TryWrapFindUnderlyingTypeEncodePlan tries to convert to a Go builtin type. e.g. If value was of type MyString and
|
||||
// MyString was defined as a string then a wrapper plan would be returned that converts MyString to string.
|
||||
func TryWrapFindUnderlyingTypeEncodePlan(value any) (plan WrappedEncodePlanNextSetter, nextValue any, ok bool) {
|
||||
if value == nil {
|
||||
return nil, nil, false
|
||||
}
|
||||
|
||||
if _, ok := value.(driver.Valuer); ok {
|
||||
return nil, nil, false
|
||||
}
|
||||
@@ -1387,6 +1393,15 @@ func TryWrapFindUnderlyingTypeEncodePlan(value any) (plan WrappedEncodePlanNextS
|
||||
return &underlyingTypeEncodePlan{nextValueType: nextValueType}, refValue.Convert(nextValueType).Interface(), true
|
||||
}
|
||||
|
||||
// []byte is a special case. It is a slice but we treat it as a scalar type. In the case of a named type like
|
||||
// json.RawMessage which is defined as []byte the underlying type should be considered as []byte. But any other slice
|
||||
// does not have a special underlying type.
|
||||
//
|
||||
// https://github.com/jackc/pgx/issues/1763
|
||||
if refValue.Type() != byteSliceType && refValue.Type().AssignableTo(byteSliceType) {
|
||||
return &underlyingTypeEncodePlan{nextValueType: byteSliceType}, refValue.Convert(byteSliceType).Interface(), true
|
||||
}
|
||||
|
||||
return nil, nil, false
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user