iter: unsafe

This commit is contained in:
Michał Matczuk
2017-08-23 15:26:16 +02:00
parent de9287b6e7
commit 4be0a35207
2 changed files with 13 additions and 4 deletions

View File

@@ -71,7 +71,6 @@ func fieldsByTraversal(v reflect.Value, traversals [][]int, values []interface{}
for i, traversal := range traversals { for i, traversal := range traversals {
if len(traversal) == 0 { if len(traversal) == 0 {
values[i] = new(interface{})
continue continue
} }
f := reflectx.FieldByIndexes(v, traversal) f := reflectx.FieldByIndexes(v, traversal)

View File

@@ -42,6 +42,14 @@ func Iter(q *gocql.Query) *Iterx {
} }
} }
// Unsafe forces the iterator to ignore missing fields. By default when scanning
// a struct if result row has a column that cannot be mapped to any destination
// field an error is reported. With unsafe such columns are ignored.
func (iter *Iterx) Unsafe() *Iterx {
iter.unsafe = true
return iter
}
// Get scans first row into a destination and closes the iterator. If the // Get scans first row into a destination and closes the iterator. If the
// destination type is a Struct, then StructScan will be used. If the // destination type is a Struct, then StructScan will be used. If the
// destination is some other type, then the row must only have one column which // destination is some other type, then the row must only have one column which
@@ -196,9 +204,11 @@ func (iter *Iterx) StructScan(dest interface{}) bool {
iter.fields = m.TraversalsByName(v.Type(), columns) iter.fields = m.TraversalsByName(v.Type(), columns)
// if we are not unsafe and are missing fields, return an error // if we are not unsafe and are missing fields, return an error
if f, err := missingFields(iter.fields); err != nil && !iter.unsafe { if !iter.unsafe {
iter.err = fmt.Errorf("missing destination name %s in %T", columns[f], dest) if f, err := missingFields(iter.fields); err != nil {
return false iter.err = fmt.Errorf("missing destination name %s in %T", columns[f], dest)
return false
}
} }
iter.values = make([]interface{}, len(columns)) iter.values = make([]interface{}, len(columns))
iter.started = true iter.started = true