bind

package module
v0.0.0-...-3729cab Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 9 Imported by: 1

README

bind

Package bind provides functions to work with flattened lists of structure fields.

See the documentation at:


Made in Berlin, DE

Documentation

Overview

Package bind provides functions to work with flattened lists of structure fields.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcceptsFields

func AcceptsFields[T any]() bool

AcceptsFields checks if a type can be used with BindFieldsCreate or the values of the type with BindFields.

func AcceptsList

func AcceptsList[T any]() bool

AcceptsList checks if a type can be used to bind multiple values.

func AcceptsScalar

func AcceptsScalar[T any]() bool

AcceptsScalar checks if a type can be used with CreateScalarAndBind or the values of the type with BindScalar.

func BindScalar

func BindScalar(receiver any, value ...any) bool

BindScalar sets the receiver to the provided value or values. It traverses the receiver through wrappers like pointers, slices and interfaces. If the receiver contains a slice which is greater or equal length as the number of the provided values, N, it sets the first N slice items to the values. It returns true, if the values were set. It returns false, if the values were not set, e.g. due to incompatible types.

func Bindable

func Bindable[T any]() bool

Bindable is the same as AcceptsScalar[T]() || AcceptsFields[T]().

func BindableType

func BindableType(t reflect.Type) bool

BindableType is like Bindable, but uses type t as input.

func CreateAndBindScalar

func CreateAndBindScalar[T any](value ...any) (T, bool)

CreateAndBindScalar is like BindScalar, but it allocates the receiver from the type T.

func CreateAndBindScalarFieldType

func CreateAndBindScalarFieldType(t FieldType, value ...any) (reflect.Value, bool)

CreateAndBindScalarFieldType is like BindScalar, but it allocates the receiver from the field type t.

func CreateAndBindScalarFor

func CreateAndBindScalarFor(t reflect.Type, value ...any) (reflect.Value, bool)

CreateAndBindScalarFor is like BindScalar, but it allocates the receiver from the type t.

func TypeAcceptsFields

func TypeAcceptsFields(t reflect.Type) bool

TypeAcceptsFields is like AcceptsFields, but uses type t as input.

func TypeAcceptsList

func TypeAcceptsList(t reflect.Type) bool

TypeAcceptsList is like AcceptsList, but uses type t as input.

func TypeAcceptsScalar

func TypeAcceptsScalar(t reflect.Type) bool

TypeAcceptsScalar is like AcceptsScalar, but uses type t as input.

Types

type Field

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

Field is a field of structure or a compatible map. It is used as the output of the inspection functions, and as the input of the binding functions.

func Bind

func Bind(structure any, value ...Field) []Field

Bind sets structure fields recursively. It traverses through pointers, slices and interfaces. It returns the values for which it is not possible to find a compatible matching field. It supports maps that have string keys and scalar values.

func CreateAndBind

func CreateAndBind[T any](value ...Field) (T, []Field)

CreateAndBind is like Bind, but it allocates the receiver from type T.

func CreateAndBindFor

func CreateAndBindFor(t reflect.Type, value ...Field) (reflect.Value, []Field)

CreateAndBindFor is like Bind, but it allocates the receiver from type t.

func FieldValues

func FieldValues(structure any) []Field

FieldValues returns the fields of a structure value recursively. It traverses through pointers, slices and interfaces.

func Fields

func Fields[T any]() []Field

Fields returns the fields of a structure type recursively. It traverses through pointers and slices.

func FieldsOf

func FieldsOf(t reflect.Type) []Field

Fields of is like Fields but uses type t as the input.

func NamedValue

func NamedValue(name string, value any) Field

NamedValue defines a field for inptu to BindFields or BindFieldsCreate. The name is the kebab case representation of the field path. The struct boundaries in the field path are not represented in the name, which leads to potentially ambigous field references, depending on the receiver type structure and naming. In case of ambigous fields, the field that can match multiple structure fields, will be bound to each matched structure field.

func ValueByPath

func ValueByPath(path []string, value any) Field

ValueByPath defines a field for input to BindFields or BindFieldsCreate. It defines the field by its exact field path in the receiver structure.

func (Field) Free

func (f Field) Free() bool

Free indicates that the field was found in a map.

func (Field) List

func (f Field) List() bool

List indicates whether a field is wrapped in a slice and accpets multiple values.

func (Field) Name

func (f Field) Name() string

Name returns the structure path of a field concatenated and in kebab casing, unless defined otherwise by NamedValue.

func (Field) Path

func (f Field) Path() []string

Path returns the structure path of a field.

func (Field) Type

func (f Field) Type() FieldType

Type returns the scalar type of the field.

func (Field) Value

func (f Field) Value() any

Value returns the value of a field.

type FieldType

type FieldType = reflect.Kind

Field type represents the possible types of a field.

Jump to

Keyboard shortcuts

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