Documentation
¶
Overview ¶
Package godotils provides utility functions for working with godog tables, including table creation, iteration, and unmarshaling into Go structs.
Index ¶
- func Column(table *godog.Table, index int) []string
- func Columns(table *godog.Table) iter.Seq[[]string]
- func Header(table *godog.Table) []string
- func Rows(table *godog.Table, skipHeader ...bool) iter.Seq[[]string]
- func Table(rows [][]string) *godog.Table
- func UnmarshalTable(table *godog.Table, dest any, options ...UnmarshalOption) error
- type UnmarshalFunc
- type UnmarshalOption
- func WithAllowExtraColumns() UnmarshalOption
- func WithAllowMissingColumns() UnmarshalOption
- func WithDefaultUnmarshalFunc(fn UnmarshalFunc) UnmarshalOption
- func WithDefaultUnmarshaler(unmarshaler Unmarshaler) UnmarshalOption
- func WithUnmarshalFunc(t reflect.Type, fn UnmarshalFunc) UnmarshalOption
- func WithUnmarshalFuncFor[T any](fn UnmarshalFunc) UnmarshalOption
- func WithUnmarshaler(t reflect.Type, unmarshaler Unmarshaler) UnmarshalOption
- func WithUnmarshalerFor[T any](unmarshaller Unmarshaler) UnmarshalOption
- type Unmarshaler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Column ¶
Column extracts a single column from the table by index and returns it as a string slice. Note: No bounds checking is performed. Panics if index is out of range.
func Columns ¶
Columns returns an iterator over all columns in the table as string slices. The iterator follows the iter.Seq pattern introduced in Go 1.23.
func Header ¶
Header extracts and returns the first row of the table as a string slice. If the table is empty, it returns nil.
func Rows ¶
Rows returns an iterator over all rows in the table as string slices. If skipHeader is true, the first row (header) is skipped. The iterator follows the iter.Seq pattern introduced in Go 1.23.
func Table ¶
Table creates a godog.Table from a 2D slice of strings. Each inner slice represents a row in the table, with elements representing cell values.
func UnmarshalTable ¶
func UnmarshalTable(table *godog.Table, dest any, options ...UnmarshalOption) error
UnmarshalTable unmarshals a godog.Table into a slice of structs. The dest parameter must be a pointer to a slice of structs. The table's header row is used to map columns to struct fields using the "table" tag, or the field name if no tag is present. Fields can be marked as optional with "table:\"name,optional\"".
By default, JSON unmarshaling is used for cell values. Custom unmarshalers can be configured using the With* option functions.
Types ¶
type UnmarshalFunc ¶
UnmarshalFunc is a function type that implements the Unmarshaler interface. This allows regular functions to be used as unmarshalers.
type UnmarshalOption ¶
type UnmarshalOption = func(*unmarshalConfig)
UnmarshalOption is a function that configures unmarshal behavior.
func WithAllowExtraColumns ¶
func WithAllowExtraColumns() UnmarshalOption
WithAllowExtraColumns returns an option that allows the table to have columns that don't map to any struct field. Without this option, extra columns cause an error.
func WithAllowMissingColumns ¶
func WithAllowMissingColumns() UnmarshalOption
WithAllowMissingColumns returns an option that allows struct fields to have no corresponding table column, unless the field is marked as optional. Without this option, missing columns cause an error.
func WithDefaultUnmarshalFunc ¶
func WithDefaultUnmarshalFunc(fn UnmarshalFunc) UnmarshalOption
WithDefaultUnmarshalFunc returns an option that sets the default unmarshaler to use for all types that don't have a specific unmarshaler configured. This is a convenience wrapper that accepts a function directly.
func WithDefaultUnmarshaler ¶
func WithDefaultUnmarshaler(unmarshaler Unmarshaler) UnmarshalOption
WithDefaultUnmarshaler returns an option that sets the default unmarshaler to use for all types that don't have a specific unmarshaler configured. The default unmarshaler is json.Unmarshal.
func WithUnmarshalFunc ¶
func WithUnmarshalFunc(t reflect.Type, fn UnmarshalFunc) UnmarshalOption
WithUnmarshalFunc returns an option that registers a custom unmarshaler for a specific type. This is a convenience wrapper that accepts a function directly.
func WithUnmarshalFuncFor ¶
func WithUnmarshalFuncFor[T any](fn UnmarshalFunc) UnmarshalOption
WithUnmarshalFuncFor returns an option that registers a custom unmarshaler for a specific type T. This is a convenience wrapper that accepts a function directly.
func WithUnmarshaler ¶
func WithUnmarshaler(t reflect.Type, unmarshaler Unmarshaler) UnmarshalOption
WithUnmarshaler returns an option that registers a custom unmarshaler for a specific type. The type-specific unmarshaler takes precedence over the default unmarshaler.
func WithUnmarshalerFor ¶
func WithUnmarshalerFor[T any](unmarshaller Unmarshaler) UnmarshalOption
WithUnmarshalerFor returns an option that registers a custom unmarshaler for a specific type T. This is a type-safe convenience wrapper around WithUnmarshaler.
type Unmarshaler ¶
Unmarshaler is an interface for types that can unmarshal data into a value. It follows the same pattern as json.Unmarshaler but with a different method signature.