Documentation ¶
Index ¶
- Variables
- func All[T any](ctx context.Context, exec Queryer, m Mapper[T], query string, args ...any) ([]T, error)
- func Collect(ctx context.Context, exec Queryer, collector func(context.Context, cols) any, ...) ([]any, error)
- func ColumnMapper[T any](name string) func(context.Context, cols) func(*Values) (T, error)
- func MapMapper[T any](ctx context.Context, c cols) func(*Values) (map[string]T, error)
- func NewStructMapperSource(opts ...MappingSourceOption) (mapperSourceImpl, error)
- func One[T any](ctx context.Context, exec Queryer, m Mapper[T], query string, args ...any) (T, error)
- func ReflectedValue(v *Values, name string, typ reflect.Type) reflect.Value
- func SingleColumnMapper[T any](ctx context.Context, c cols) func(*Values) (T, error)
- func SliceMapper[T any](ctx context.Context, c cols) func(*Values) ([]T, error)
- func Value[T any](v *Values, name string) T
- func ValueCallback(v *Values, name string, f func() reflect.Value) reflect.Value
- type ICursor
- type Mappable
- type Mapper
- type MapperMod
- type MapperModFunc
- type MappingError
- type MappingOption
- type MappingSourceOption
- type NameMapperFunc
- type Queryer
- type Row
- type RowValidator
- type Rows
- type StructMapperSource
- type TypeConverter
- type Values
Constants ¶
This section is empty.
Variables ¶
var ( ErrBadCollectorReturn = errors.New("collector does not return a function") ErrBadCollectFuncInput = errors.New("collect func must only take *Values as input") ErrBadCollectFuncOutput = errors.New("collect func must return at least 2 values with the last being an error") )
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 Collect ¶
func Collect(ctx context.Context, exec Queryer, collector func(context.Context, cols) any, query string, args ...any) ([]any, error)
Collect multiple slices of values from a single query collector must be of the structure func(context.Context, map[string]int) func(*Values) (t1, t2, ..., error) The returned slice contains values like this {[]t1, []t2}
func ColumnMapper ¶
Map a column by name.
func MapMapper ¶
Maps all rows into map[string]T Most likely used with interface{} to get a map[string]interface{}
func NewStructMapperSource ¶
func NewStructMapperSource(opts ...MappingSourceOption) (mapperSourceImpl, error)
NewStructMapperSource creates a new Mapping object with provided list of options.
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 ReflectedValue ¶
ReflectedValue returns the named value as an reflect.Value When not recording, it will panic if the requested type does not match what was recorded
func SingleColumnMapper ¶
For queries that return only one column throws an error if there is more than one column
func SliceMapper ¶
Maps each row into []any in the order
func Value ¶
Value retrieves a value from Values with the specified name if Values.IsRecording returns true, it will ALWAYS return the zero value of that type When not recording, it will panic if the requested type does not match what was recorded
Types ¶
type ICursor ¶
type Mappable ¶
Mappable is an interface of a type that can map its own values if a struct implement IMapper, using StructMapper to map its values will use the MapValues method instead
type Mapper ¶
Mapper is a function that return the mapping function. 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 function which is then used to map every row.
The generator function does not return an error itself to make it less cumbersome It is recommended to instead return a mapping function that returns an error the ErrorMapper is provider for this
The returned function is called once with values.IsRecording() == true to record the expected types For each row, the DB values are then scanned into Values before calling the returned function.
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) MapperModFunc
type MapperModFunc ¶
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 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 NameMapperFunc) 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 ...interface{}) 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 NameMapperFunc ¶
NameMapperFunc is a function type that maps a struct field name to the database column name.
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 ¶
Row represents a single row in the Rows results can be scanned into a number of given values
type RowValidator ¶
RowValidator is called with 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 StructMapperSource ¶
type StructMapperSource interface {
// contains filtered or unexported methods
}
type TypeConverter ¶
type TypeConverter interface { // ConvertType modifies the type of the struct // the method is called with the expected type of the column ConvertType(reflect.Type) reflect.Value // OriginalValue retrieves the original value from the converted type // the value given is a value of the type returned by ConvertType OriginalValue(reflect.Value) reflect.Value }
type Values ¶
type Values struct {
// contains filtered or unexported fields
}
Values holds the values of a row use Value() to retrieve a value by column name Column names must be unique, so if multiple columns have the same name, only the last one remains
func (*Values) IsRecording ¶
IsRecording returns wether the values are currently in recording mode When recording, calls to Get() will record the expected type