Files
gocqlx/gocqlx.go

83 lines
2.5 KiB
Go
Raw Normal View History

2017-09-21 21:43:27 +02:00
// Copyright (C) 2017 ScyllaDB
// Use of this source code is governed by a ALv2-style
// license that can be found in the LICENSE file.
2017-07-20 15:55:19 +02:00
package gocqlx
import (
"errors"
"fmt"
"reflect"
"github.com/gocql/gocql"
"github.com/scylladb/go-reflectx"
2017-07-20 15:55:19 +02:00
)
// structOnlyError returns an error appropriate for type when a non-scannable
// struct is expected but something else is given.
2017-07-20 15:55:19 +02:00
func structOnlyError(t reflect.Type) error {
if isStruct := t.Kind() == reflect.Struct; !isStruct {
return fmt.Errorf("expected a struct but got %s", t.Kind())
2017-07-20 15:55:19 +02:00
}
if isUnmarshaller := reflect.PtrTo(t).Implements(unmarshallerInterface); isUnmarshaller {
return fmt.Errorf("expected a struct but the provided struct type %s implements gocql.Unmarshaler", t.Name())
}
if isUDTUnmarshaller := reflect.PtrTo(t).Implements(udtUnmarshallerInterface); isUDTUnmarshaller {
return fmt.Errorf("expected a struct but the provided struct type %s implements gocql.UDTUnmarshaler", t.Name())
2017-07-20 15:55:19 +02:00
}
2017-07-20 15:55:19 +02:00
return fmt.Errorf("expected a struct, but struct %s has no exported fields", t.Name())
}
// reflect helpers
var (
unmarshallerInterface = reflect.TypeOf((*gocql.Unmarshaler)(nil)).Elem()
udtUnmarshallerInterface = reflect.TypeOf((*gocql.UDTUnmarshaler)(nil)).Elem()
)
2017-07-20 15:55:19 +02:00
func baseType(t reflect.Type, expected reflect.Kind) (reflect.Type, error) {
t = reflectx.Deref(t)
if t.Kind() != expected {
return nil, fmt.Errorf("expected %s but got %s", expected, t.Kind())
}
return t, nil
}
// fieldsByName fills a values interface with fields from the passed value based
2017-08-23 16:07:55 +02:00
// on the traversals in int. If ptrs is true, return addresses instead of values.
2017-07-20 15:55:19 +02:00
// We write this instead of using FieldsByName to save allocations and map lookups
2017-08-23 16:07:55 +02:00
// when iterating over many rows. Empty traversals will get an interface pointer.
2017-07-20 15:55:19 +02:00
// Because of the necessity of requesting ptrs or values, it's considered a bit too
// specialized for inclusion in reflectx itself.
func fieldsByTraversal(v reflect.Value, traversals [][]int, values []interface{}, ptrs bool) error {
v = reflect.Indirect(v)
if v.Kind() != reflect.Struct {
return errors.New("argument not a struct")
}
for i, traversal := range traversals {
if len(traversal) == 0 {
continue
}
f := reflectx.FieldByIndexes(v, traversal)
if ptrs {
values[i] = f.Addr().Interface()
} else {
values[i] = f.Interface()
}
}
return nil
}
func missingFields(transversals [][]int) (field int, err error) {
for i, t := range transversals {
if len(t) == 0 {
return i, errors.New("missing field")
}
}
return 0, nil
}