reflect

package
v0.0.51 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: MIT Imports: 4 Imported by: 0

README

Package testing/reflect

Setting up tests and comparing test results is most efficient, when you can directly set up and compare the actual test and result objects. However, this is sometimes prevented by objects not being open for construction and having private states.

To cope with this challenge the test-package supports helpers to access, i.e. read and write, private fields of objects using reflection.

  • test.NewBuilder[...]() allows constructing a new object from scratch.
  • test.NewGetter(...) allows reading private fields of an object by name.
  • test.NewSetter(...) allows writing private fields by name, and finally
  • test.NewAccessor(...) allows reading and writing of private fields by name.

Example usage

The following example shows how the private properties of a closed error can be set up using the test.NewBuilder[...]().

    err := test.NewBuilder[viper.ConfigFileNotFoundError]().
        Set("locations", fmt.Sprintf("%s", "...path...")).
        Set("name", "test").Build()

Similar we can set up input objects with private properties to minimize the dependencies in the test setup, however, using this features exposes the test to internal changes.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Find

func Find[P, T any](param P, deflt T, names ...string) T

Find returns the first value of a struct field from the given list of field names with a type matching the default value type. If the name list is empty or contains a star (`*`), the first matching field in order of the struct declaration is returned as fallback. If no matching field is found, the default value is returned.

The `param` object can be a struct, a pointer to a struct, or an arbitrary value matching the default value type. In the last case, the arbitrary value is returned as is.

func Name

func Name[P any](name string, param P) string

Name returns the normalized test case name for the given default name and parameter set. If the default name is empty, the test name is resolved from the parameter set using the `name` field. The resolved value is normalized before being returned. If no test name can be resolved an empty string is returned.

Types

type Builder

type Builder[T any] interface {
	// Getter is a generic interface that allows you to access unexported fields
	// of a (pointer) struct by field name.
	Getter[T]
	// Finder is a generic interface that allows you to access unexported fields
	// of a (pointer) struct by field name.
	Finder[T]
	// Setter is a generic fluent interface that allows you to modify unexported
	// fields of a (pointer) struct by field name.
	Setter[T]
}

Builder is a generic, partially fluent interface that allows you to access and modify unexported fields of a (pointer) struct by field name.

func NewAccessor

func NewAccessor[T any](target T) Builder[T]

NewAccessor creates a generic builder/accessor for a given target struct. The builder allows you to access and modify unexported fields of the struct by field name.

If the target is a pointer to a struct (template), the pointer is stored and the instance is modified directly. If the pointer is nil a new instance is created and stored for modification.

If the target is a struct, it cannot be modified directly and a new pointer struct is created to circumvent the access restrictions on private fields. The pointer struct is stored for modification.

func NewBuilder

func NewBuilder[T any]() Builder[T]

NewBuilder creates a generic builder for a target struct type. The builder allows you to access and modify unexported fields of the struct by field name.

type Finder

type Finder[T any] interface {
	// Find returns the first value of a field from the given list of field
	// names with a type matching the default value type. If the name list is
	// empty or contains a star (`*`), the first matching field in order of the
	// struct declaration is returned as fallback. If no matching field is
	// found, the default value is returned.
	Find(dflt any, names ...string) any
}

Finder is a generic interface that allows you to access unexported fields of a (pointer) struct by field name.

func NewFinder

func NewFinder[T any](target T) Finder[T]

NewFinder creates a generic finder for a target struct type. The finder allows you to access unexported fields of the struct by field name.

type Getter

type Getter[T any] interface {
	// Get returns the value of the field with the given name. If the name is
	// empty, the stored target instance is returned.
	Get(name string) any
}

Getter is a generic interface that allows you to access unexported fields of a (pointer) struct by field name.

func NewGetter

func NewGetter[T any](target T) Getter[T]

NewGetter creates a generic getter for a target struct type. The getter allows you to access unexported fields of the struct by field name.

type Setter

type Setter[T any] interface {
	// Set sets the value of the field with the given name. If the name is empty,
	// and of the same type the stored target instance is replaced by the given
	// value.
	Set(name string, value any) Setter[T]
	// Build returns the created or modified target instance of the builder.
	Build() T
}

Setter is a generic fluent interface that allows you to modify unexported fields of a (pointer) struct by field name.

func NewSetter

func NewSetter[T any](target T) Setter[T]

NewSetter creates a generic setter for a target struct type. The setter allows you to modify unexported fields of the struct by field name.

Jump to

Keyboard shortcuts

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