rowmap

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package rowmap maps database/sql rows onto structs, maps and scalars.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func First

func First(rows *sql.Rows, dest any) error

First scans the first row into dest (*struct, *map[string]any or *scalar); it returns sql.ErrNoRows when there are no rows. The remaining rows are ignored.

func Slice

func Slice(rows *sql.Rows, dest any) error

Slice scans every row into *[]T (T: struct, *struct, map[string]any or scalar).

func StructType

func StructType(t reflect.Type) (reflect.Type, error)

StructType dereferences pointer types and requires a struct.

func StructValue

func StructValue(v reflect.Value) (reflect.Value, error)

StructValue dereferences pointers and interfaces and requires the result to be a struct.

func Target

func Target(dst reflect.Value) (any, func())

Target returns what to hand rows.Scan for dst, plus an optional step to run afterwards. Plain value types are scanned through a **T so that NULL leaves the zero value rather than failing.

func ToRows

func ToRows(t reflect.Type, vals []reflect.Value) ([]map[string]any, error)

ToRows turns structs of type t into insert rows, one map per value. A column is kept when at least one value sets it, so `omitempty` fields that are empty everywhere are dropped.

func ValueOfColumn

func ValueOfColumn(elem reflect.Value, column string) (any, error)

ValueOfColumn reads a column from a scanned element (struct, *struct or map).

Types

type Field

type Field struct {
	Name      string
	OmitEmpty bool
	// contains filtered or unexported fields
}

Field is one writable column of a struct.

func Fields

func Fields(t reflect.Type) []Field

Fields lists the columns of struct type t (a struct, not a pointer) in discovery order, using the same tag rules as scanning.

func (Field) Value

func (f Field) Value(v reflect.Value) (val reflect.Value, ok bool)

Value reads the field from struct value v. ok is false when the field sits behind a nil embedded pointer, so there is no value to read.

type Mapper

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

Mapper scans successive rows into values of one element type. It keeps scratch state between rows, so one Mapper must not be used from several goroutines.

func NewMapper

func NewMapper(t reflect.Type, cols []string) (*Mapper, error)

NewMapper returns a Mapper that scans result columns cols into values of type t: a struct, a map[string]any or a scalar.

func (*Mapper) Scan

func (m *Mapper) Scan(rows *sql.Rows, dst reflect.Value) error

Scan scans the current row into dst (addressable, of m.typ). NULL in a plain (non-pointer, non-Scanner) field leaves the zero value instead of failing.

Jump to

Keyboard shortcuts

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