iterx: Allow forcing scanning as struct

We have a structure type that implements UnmarshalCQL method.
We use it to unmarshal a user defined type. We also want to use the same struct for scanning an entire row.

There is StructScan method available in gocqlx for this purpose when iterating over rows, but no equivalent when doing a Select.
This commit introduces the possibility when doing select/get as well.

Co-authored-by: Michał Matczuk <michal@scylladb.com>
This commit is contained in:
Martin Sucha
2019-11-06 15:33:37 +01:00
committed by Michal Jan Matczuk
parent a9ce16bfc6
commit a08a66ee85
4 changed files with 260 additions and 42 deletions

View File

@@ -200,10 +200,17 @@ func (q *Queryx) ExecRelease() error {
return q.Exec()
}
// Get scans first row into a destination. If the destination type is a struct
// pointer, then Iter.StructScan will be used. If the destination is some
// other type, then the row must only have one column which can scan into that
// type.
// Get scans first row into a destination and closes the iterator.
//
// If the destination type is a struct pointer, then Iter.StructScan will be
// used.
// If the destination is some other type, then the row must only have one column
// which can scan into that type.
// This includes types that implement gocql.Unmarshaler and gocql.UDTUnmarshaler.
//
// If you'd like to treat a type that implements gocql.Unmarshaler or
// gocql.UDTUnmarshaler as an ordinary struct you should call
// Iter().StructOnly().Get(dest) instead.
//
// If no rows were selected, ErrNotFound is returned.
func (q *Queryx) Get(dest interface{}) error {
@@ -221,9 +228,17 @@ func (q *Queryx) GetRelease(dest interface{}) error {
}
// Select scans all rows into a destination, which must be a pointer to slice
// of any type. If the destination slice type is a struct, then Iter.StructScan
// will be used on each row. If the destination is some other type, then each
// row must only have one column which can scan into that type.
// of any type, and closes the iterator.
//
// If the destination slice type is a struct, then Iter.StructScan will be used
// on each row.
// If the destination is some other type, then each row must only have one
// column which can scan into that type.
// This includes types that implement gocql.Unmarshaler and gocql.UDTUnmarshaler.
//
// If you'd like to treat a type that implements gocql.Unmarshaler or
// gocql.UDTUnmarshaler as an ordinary struct you should call
// Iter().StructOnly().Select(dest) instead.
//
// If no rows were selected, ErrNotFound is NOT returned.
func (q *Queryx) Select(dest interface{}) error {