iterx: Move isScannable to Iterx
Signed-off-by: Michał Matczuk <michal@scylladb.com>
This commit is contained in:
committed by
Michal Jan Matczuk
parent
bd53297a6c
commit
7c0a35d621
20
gocqlx.go
20
gocqlx.go
@@ -39,26 +39,6 @@ func baseType(t reflect.Type, expected reflect.Kind) (reflect.Type, error) {
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// isScannable takes the reflect.Type and the actual dest value and returns
|
||||
// whether or not it's Scannable. Something is scannable if:
|
||||
// * it is not a struct
|
||||
// * it implements gocql.Unmarshaler
|
||||
// * it has no exported fields
|
||||
func isScannable(t reflect.Type) bool {
|
||||
if reflect.PtrTo(t).Implements(_unmarshallerInterface) {
|
||||
return true
|
||||
}
|
||||
if t.Kind() != reflect.Struct {
|
||||
return true
|
||||
}
|
||||
|
||||
// it's not important that we use the right mapper for this particular object,
|
||||
// we're only concerned on how many exported fields this struct has
|
||||
m := DefaultMapper
|
||||
|
||||
return len(m.TypeMap(t).Index) == 0
|
||||
}
|
||||
|
||||
// fieldsByName fills a values interface with fields from the passed value based
|
||||
// on the traversals in int. If ptrs is true, return addresses instead of values.
|
||||
// We write this instead of using FieldsByName to save allocations and map lookups
|
||||
|
||||
20
iterx.go
20
iterx.go
@@ -74,6 +74,22 @@ func (iter *Iterx) Get(dest interface{}) error {
|
||||
return iter.checkErrAndNotFound()
|
||||
}
|
||||
|
||||
// isScannable takes the reflect.Type and the actual dest value and returns
|
||||
// whether or not it's Scannable. t is scannable if:
|
||||
// * ptr to t implements gocql.Unmarshaler
|
||||
// * it is not a struct
|
||||
// * it has no exported fields
|
||||
func (iter *Iterx) isScannable(t reflect.Type) bool {
|
||||
if reflect.PtrTo(t).Implements(_unmarshallerInterface) {
|
||||
return true
|
||||
}
|
||||
if t.Kind() != reflect.Struct {
|
||||
return true
|
||||
}
|
||||
|
||||
return len(iter.Mapper.TypeMap(t).Index) == 0
|
||||
}
|
||||
|
||||
func (iter *Iterx) scanAny(dest interface{}, structOnly bool) bool {
|
||||
value := reflect.ValueOf(dest)
|
||||
if value.Kind() != reflect.Ptr {
|
||||
@@ -86,7 +102,7 @@ func (iter *Iterx) scanAny(dest interface{}, structOnly bool) bool {
|
||||
}
|
||||
|
||||
base := reflectx.Deref(value.Type())
|
||||
scannable := isScannable(base)
|
||||
scannable := iter.isScannable(base)
|
||||
|
||||
if structOnly && scannable {
|
||||
iter.err = structOnlyError(base)
|
||||
@@ -140,7 +156,7 @@ func (iter *Iterx) scanAll(dest interface{}, structOnly bool) bool {
|
||||
|
||||
isPtr := slice.Elem().Kind() == reflect.Ptr
|
||||
base := reflectx.Deref(slice.Elem())
|
||||
scannable := isScannable(base)
|
||||
scannable := iter.isScannable(base)
|
||||
|
||||
if structOnly && scannable {
|
||||
iter.err = structOnlyError(base)
|
||||
|
||||
Reference in New Issue
Block a user