spxapi

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2026 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

package spxapi implements the core logic of the API type and exposes the functionality available in the package. With the API type, users can create a custom API instance and override default settings to configure spxscan.

Index

Constants

This section is empty.

Variables

View Source
var Default = &API{lenient: false} //nolint:gochecknoglobals // Default is a package-level singleton providing default configuration.

Default is the default instance of API with all configuration settings set to default.

View Source
var ErrNotFound = errors.New("spxscan: no row was found")

ErrNotFound is returned by ScanOne if there were no rows.

Functions

func NotFound

func NotFound(err error) bool

NotFound returns true if err is a not found error. This error is returned by ScanOne if there were no rows.

func ScanSeq

func ScanSeq[T any](api *API, rowIter *spanner.RowIterator) iter.Seq2[*T, error]

ScanSeq returns a interator that iterates all rows to the end. After iterating it closes the interator, and propagates any errors that could pop up.

func SelectSeq

func SelectSeq[T any](ctx context.Context, api *API, db Querier, statement spanner.Statement) iter.Seq2[*T, error]

Select is a high-level function that queries rows from Querier and calls the ScanAll function. See ScanAll for details.

Types

type API

type API struct {
	// contains filtered or unexported fields
}

API is the core type in spxscan. 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 spxscan.

func NewAPI

func NewAPI(opts ...APIOption) *API

NewAPI creates a new API object with provided list of options.

func (*API) Get

func (api *API) Get(ctx context.Context, db Querier, dst any, statement spanner.Statement) error

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.

func (*API) Select

func (api *API) Select(ctx context.Context, db Querier, dst any, statement spanner.Statement) error

Select is a high-level function that queries rows from Querier and calls the ScanAll function. See ScanAll for details.

func (*API) UpdateAndGet

func (api *API) UpdateAndGet(ctx context.Context, client TxnRunner, dst any, statement spanner.Statement) error

UpdateAndGet is a package-level helper function that uses the API.Get() call inside a transaction. This should be used when data is being returned after an update using the THEN RETURN clause.

func (*API) UpdateAndSelect

func (api *API) UpdateAndSelect(ctx context.Context, client TxnRunner, dst any, statement spanner.Statement) error

UpdateAndSelect is a high-level helper function that uses the API.Select() call inside a transaction. This should be used when data is being returned after an update using the THEN RETURN clause.

type APIOption

type APIOption func(api *API)

APIOption is a function type that changes API configuration.

func WithStructLenient

func WithStructLenient(lenient bool) APIOption

WithStructLenient uses Row.ToStructLenient() for scanning to destination.

type Querier

type Querier interface {
	Query(ctx context.Context, statement spanner.Statement) *spanner.RowIterator
}

type TxnRunner

type TxnRunner interface {
	ReadWriteTransaction(ctx context.Context, f func(context.Context, *spanner.ReadWriteTransaction) error) (commitTimestamp time.Time, err error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL