Replace Unsafe with Strict mechanism

Previously by default the presence of a missing field in
a udt would result in an error reported. The Unsafe
mechanism could be used to ignore these fields.

This PR changes the default behavior to ignoring missing
fields and only reporting an error if Strict mode is used.
This approach is in line with the gocql.
This commit is contained in:
sylwiaszunejko
2024-06-25 13:34:50 +02:00
committed by Sylwia Szunejko
parent 6a60650668
commit 7072863b0c
6 changed files with 69 additions and 70 deletions

View File

@@ -13,9 +13,9 @@ import (
"github.com/scylladb/go-reflectx"
)
// DefaultUnsafe enables the behavior of forcing queries and iterators to ignore
// missing fields for all queries. See Unsafe below for more information.
var DefaultUnsafe bool
// DefaultStrict disables the behavior of forcing queries and iterators to ignore
// missing fields for all queries. See Strict below for more information.
var DefaultStrict bool
// Iterx is a wrapper around gocql.Iter which adds struct scanning capabilities.
type Iterx struct {
@@ -26,16 +26,16 @@ type Iterx struct {
// Cache memory for a rows during iteration in structScan.
fields [][]int
values []interface{}
unsafe bool
strict bool
structOnly bool
applied bool
}
// 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
// Strict forces the iterator to disable ignoring missing fields. In Strict mode
// when scanning a struct if result row has a column that cannot be mapped to any
// destination field an error is reported. By default such columns are ignored.
func (iter *Iterx) Strict() *Iterx {
iter.strict = true
return iter
}
@@ -228,7 +228,7 @@ func (iter *Iterx) scan(value reflect.Value) bool {
if value.Kind() != reflect.Ptr {
panic("value must be a pointer")
}
return iter.Iter.Scan(udtWrapValue(value, iter.Mapper, iter.unsafe))
return iter.Iter.Scan(udtWrapValue(value, iter.Mapper, iter.strict))
}
// StructScan is like gocql.Iter.Scan, but scans a single row into a single
@@ -264,8 +264,8 @@ func (iter *Iterx) structScan(value reflect.Value) bool {
cas := len(columns) > 0 && columns[0] == appliedColumn
iter.fields = iter.Mapper.TraversalsByName(value.Type(), columns)
// if we are not unsafe and it's not CAS query and are missing fields, return an error
if !iter.unsafe && !cas {
// if we are strict and it's not CAS query and are missing fields, return an error
if iter.strict && !cas {
if f, err := missingFields(iter.fields); err != nil {
iter.err = fmt.Errorf("missing destination name %q in %s", columns[f], reflect.Indirect(value).Type())
return false
@@ -302,7 +302,7 @@ func (iter *Iterx) fieldsByTraversal(value reflect.Value, traversals [][]int, va
continue
}
f := reflectx.FieldByIndexes(value, traversal).Addr()
values[i] = udtWrapValue(f, iter.Mapper, iter.unsafe)
values[i] = udtWrapValue(f, iter.Mapper, iter.strict)
}
return nil
@@ -325,7 +325,7 @@ func columnNames(ci []gocql.ColumnInfo) []string {
// end of the result set was reached or if an error occurred. Close should
// be called afterwards to retrieve any potential errors.
func (iter *Iterx) Scan(dest ...interface{}) bool {
return iter.Iter.Scan(udtWrapSlice(iter.Mapper, iter.unsafe, dest)...)
return iter.Iter.Scan(udtWrapSlice(iter.Mapper, iter.strict, dest)...)
}
// Close closes the iterator and returns any errors that happened during