Documentation ¶
Index ¶
- Variables
- func All[T any](ctx context.Context, exec Queryer, m Mapper[T], query string, args ...any) ([]T, error)
- func AllFromRows[T any](ctx context.Context, m Mapper[T], rows Rows) ([]T, error)
- func ColumnMapper[T any](name string) ...
- func Each[T any](ctx context.Context, exec Queryer, m Mapper[T], query string, args ...any) func(func(T, error) bool)
- func ErrorMapper[T any](err error, meta ...string) (func(*Row) (any, error), func(any) (T, error))
- func MapMapper[T any](ctx context.Context, c cols) (before func(*Row) (any, error), after func(any) (map[string]T, error))
- func One[T any](ctx context.Context, exec Queryer, m Mapper[T], query string, args ...any) (T, error)
- func OneFromRows[T any](ctx context.Context, m Mapper[T], rows Rows) (T, error)
- func SingleColumnMapper[T any](ctx context.Context, c cols) (before func(*Row) (any, error), after func(any) (T, error))
- func SliceMapper[T any](ctx context.Context, c cols) (before func(*Row) (any, error), after func(any) ([]T, error))
- type AfterMod
- type BeforeFunc
- type ICursor
- type Mapper
- type MapperMod
- type MappingError
- type MappingOption
- type MappingSourceOption
- type Queryer
- type Row
- type RowValidator
- type Rows
- type StructMapperSource
- type TypeConverter
Constants ¶
This section is empty.
Variables ¶
var CtxKeyAllowUnknownColumns contextKey = "allow unknown columns"
CtxKeyAllowUnknownColumns makes it possible to allow unknown columns using the context
Functions ¶
func All ¶
func All[T any](ctx context.Context, exec Queryer, m Mapper[T], query string, args ...any) ([]T, error)
All scans all rows from the query and returns a slice []T of all rows using a Queryer
func AllFromRows ¶ added in v0.2.0
AllFromRows scans all rows from the given Rows and returns a slice []T of all rows using a Queryer
func ColumnMapper ¶
func ColumnMapper[T any](name string) func(ctx context.Context, c cols) (before func(*Row) (any, error), after func(any) (T, error))
Map a column by name.
func Each ¶ added in v0.6.0
func Each[T any](ctx context.Context, exec Queryer, m Mapper[T], query string, args ...any) func(func(T, error) bool)
Each returns a function that can be used to iterate over the rows of a query this function works with range-over-func so it is possible to do
for val, err := range scan.Each(ctx, exec, m, query, args...) { if err != nil { return err } // do something with val }
func ErrorMapper ¶ added in v0.3.0
The generator function does not return an error itself to make it less cumbersome so we return a function that only returns an error instead This function makes it easy to return this error
func MapMapper ¶
func MapMapper[T any](ctx context.Context, c cols) (before func(*Row) (any, error), after func(any) (map[string]T, error))
Maps all rows into map[string]T Most likely used with interface{} to get a map[string]interface{}
func One ¶
func One[T any](ctx context.Context, exec Queryer, m Mapper[T], query string, args ...any) (T, error)
One scans a single row from the query and maps it to T using a Queryer
func OneFromRows ¶ added in v0.2.0
OneFromRows scans a single row from the given Rows result and maps it to T using a Queryer
Types ¶
type AfterMod ¶ added in v0.3.0
AfterMod receives both the link of the MapperMod and the retrieved value from the original mapper
type BeforeFunc ¶ added in v0.3.0
BeforeFunc is returned by a mapper and is called before a row is scanned Scans should be scheduled with either the *Row.ScheduleScan or *Row.ScheduleScanx methods
type ICursor ¶
type ICursor[T any] interface { // Close the underlying rows Close() error // Prepare the next row Next() bool // Get the values of the current row Get() (T, error) // Return any error with the underlying rows Err() error }
type Mapper ¶
Mapper is a function that return the mapping functions. Any expensive operation, like reflection should be done outside the returned function. It is called with the columns from the query to get the mapping functions which is then used to map every row.
The Mapper does not return an error itself to make it less cumbersome It is recommended to instead return a function that returns an error the ErrorMapper is provider for this
func CustomStructMapper ¶
func CustomStructMapper[T any](src StructMapperSource, optMod ...MappingOption) Mapper[T]
Uses reflection to create a mapping function for a struct type using with custom options
func StructMapper ¶
func StructMapper[T any](opts ...MappingOption) Mapper[T]
Uses reflection to create a mapping function for a struct type using the default options
type MapperMod ¶
type MapperMod = func(context.Context, cols) (BeforeFunc, AfterMod)
MapperMod is a function that can be used to convert an existing mapper into a new mapper using Mod
type MappingError ¶
type MappingError struct {
// contains filtered or unexported fields
}
MappingError wraps another error and holds some additional metadata
func (*MappingError) Error ¶
func (m *MappingError) Error() string
Error implements the error interface
func (*MappingError) Unwrap ¶
func (m *MappingError) Unwrap() error
Unwrap returns the wrapped error
type MappingOption ¶
type MappingOption func(*mappingOptions)
MappingeOption is a function type that changes how the mapper is generated
func WithMapperMods ¶ added in v0.4.0
func WithMapperMods(mods ...MapperMod) MappingOption
WithMapperMods accepts mods used to modify the mapper
func WithRowValidator ¶
func WithRowValidator(rv RowValidator) MappingOption
WithRowValidator sets the RowValidator for the struct mapper after scanning all values in a row, they are passed to the RowValidator if it returns false, the zero value for that row is returned
func WithStructTagPrefix ¶
func WithStructTagPrefix(prefix string) MappingOption
WithStructTagPrefix should be used when every column from the database has a prefix.
func WithTypeConverter ¶
func WithTypeConverter(tc TypeConverter) MappingOption
TypeConverter sets the TypeConverter for the struct mapper it is called to modify the type of a column and get the original value back
type MappingSourceOption ¶
type MappingSourceOption func(src *mapperSourceImpl) error
MappingSourceOption are options to modify how a struct's mappings are interpreted
func WithColumnSeparator ¶
func WithColumnSeparator(separator string) MappingSourceOption
WithColumnSeparator allows to use a custom separator character for column name when combining nested structs. The default separator is "." character.
func WithFieldNameMapper ¶
func WithFieldNameMapper(mapperFn func(string) string) MappingSourceOption
WithFieldNameMapper allows to use a custom function to map field name to column names. The default function maps fields names to "snake_case"
func WithScannableTypes ¶
func WithScannableTypes(scannableTypes ...any) MappingSourceOption
WithScannableTypes specifies a list of interfaces that underlying database library can scan into. In case the destination type passed to scan implements one of those interfaces, scan will handle it as primitive type case i.e. simply pass the destination to the database library. Instead of attempting to map database columns to destination struct fields or map keys. In order for reflection to capture the interface type, you must pass it by pointer.
For example your database library defines a scanner interface like this:
type Scanner interface { Scan(...) error }
You can pass it to scan this way: scan.WithScannableTypes((*Scanner)(nil)).
func WithStructTagKey ¶
func WithStructTagKey(tagKey string) MappingSourceOption
WithStructTagKey allows to use a custom struct tag key. The default tag key is `db`.
type Queryer ¶
type Queryer interface {
QueryContext(ctx context.Context, query string, args ...any) (Rows, error)
}
Queryer is the main interface used in this package it is expected to run the query and args and return a set of Rows
type Row ¶
type Row struct {
// contains filtered or unexported fields
}
Row represents a single row from the query and is passed to the BeforeFunc when sent to a mapper's before function, scans should be scheduled with either the [ScheduleScan] or [ScheduleScanx] methods
func (*Row) ScheduleScan ¶ added in v0.3.0
ScheduleScan schedules a scan for the column name into the given value val should be a pointer
type RowValidator ¶
RowValidator is called with pointer to all the values from a row to determine if the row is valid if it is not, the zero type for that row is returned
type Rows ¶
type Rows interface { Scan(...any) error Columns() ([]string, error) Next() bool Close() error Err() error }
Rows is an interface that is expected to be returned as the result of a query
type StructMapperSource ¶
type StructMapperSource interface {
// contains filtered or unexported methods
}
func NewStructMapperSource ¶
func NewStructMapperSource(opts ...MappingSourceOption) (StructMapperSource, error)
NewStructMapperSource creates a new Mapping object with provided list of options.
type TypeConverter ¶
type TypeConverter interface { // TypeToDestination is called with the expected type of the column // it is expected to return a pointer to the desired value to scan into // the returned destination is directly scanned into TypeToDestination(reflect.Type) reflect.Value // ValueFromDestination retrieves the original value from the destination // the returned value is set back to the appropriate struct field ValueFromDestination(reflect.Value) reflect.Value }