Documentation ¶
Overview ¶
Package scan allows scanning data into Go structs and other composite types, when working with spanner library native interface.
Index ¶
- Variables
- func Get(ctx context.Context, db Querier, dst any, statement spanner.Statement) error
- func NotFound(err error) bool
- func Select(ctx context.Context, db Querier, dst any, statement spanner.Statement) error
- type API
- func (api *API) Get(ctx context.Context, db Querier, dst any, statement spanner.Statement) error
- func (api *API) ScanAll(dst any, iter *spanner.RowIterator) error
- func (api *API) ScanOne(dst any, iter *spanner.RowIterator) error
- func (api *API) Select(ctx context.Context, db Querier, dst any, statement spanner.Statement) error
- type APIOption
- type Querier
Constants ¶
This section is empty.
Variables ¶
var DefaultAPI = &API{}
DefaultAPI is the default instance of API with all configuration settings set to default.
var ErrNotFound = errors.New("spxscan: no row was found")
ErrNotFound is returned by ScanOne if there were no rows.
Functions ¶
func Get ¶
Get is a package-level helper function that uses the DefaultAPI object. See API.Get for details.
Types ¶
type API ¶
type API struct {
// contains filtered or unexported fields
}
API is the core type in scanapi. It implements all the logic and exposes functionality available in the package. With API type users can create a custom API instance and override default settings hence configure scanapi.
func (*API) Get ¶
Get is a high-level function that queries rows from Querier and calls the ScanOne function. See ScanOne for details.
func (*API) ScanAll ¶
func (api *API) ScanAll(dst any, iter *spanner.RowIterator) error
ScanAll iterates all rows to the end. After iterating it closes the interator, and propagates any errors that could pop up. It expects that destination should be a slice. For each row it scans data and appends it to the destination slice. ScanAll supports both types of slices: slice of structs by a pointer and slice of structs by value, for example:
type User struct { ID string Name string } var usersByPtr []*User var usersByValue []User
Both usersByPtr and usersByValue are valid destinations for ScanAll function.
Before starting, ScanAll resets the destination slice, so if it's not empty it will overwrite all existing elements.
func (*API) ScanOne ¶
func (api *API) ScanOne(dst any, iter *spanner.RowIterator) error
ScanOne makes sure that there was exactly one row otherwise it returns an error. Use NotFound function to check if there were no rows. After iterating ScanOne closes the interator, and propagates any errors that could pop up. It scans data from that single row into the destination.
type APIOption ¶
type APIOption func(api *API)
APIOption is a function type that changes API configuration.
func WithStructLenient ¶
WithStructLenient uses Row.ToStructLenient() for scanning to destination.