binder API

binder

package

API reference for the binder package.

T
type

HandlerFunc

HandlerFunc handles binding a set of string arguments to a field.

internal/binder/binder.go:14-14
type HandlerFunc func(args []string) error
S
struct

Binder

Binder handles mapping and execution of field bindings.

internal/binder/binder.go:17-21
type Binder struct

Methods

autoDiscover
Method

Returns

error
func (*Binder) autoDiscover() error
{
	v := reflect.ValueOf(b.dst)
	if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
		return fmt.Errorf("binder: destination must be a pointer to a struct")
	}

	elem := v.Elem()
	typ := elem.Type()

	for i := 0; i < elem.NumField(); i++ {
		field := elem.Field(i)
		fieldType := typ.Field(i)

		tag := fieldType.Tag.Get("flag")
		if tag == "" {
			tag = fieldType.Tag.Get("cli")
		}
		if tag == "" {
			continue
		}

		name := fieldType.Name
		if _, ok := b.handlers[name]; !ok {
			b.registerDefaultHandler(field, name)
		}
	}

	return nil
}

Parameters

name string
func (*Binder) registerDefaultHandler(field reflect.Value, name string)
{
	b.handlers[name] = func(args []string) error {
		var val string
		if len(args) > 0 {
			val = args[0]
		}

		// Handle boolean flag without value (implicitly true)
		if field.Kind() == reflect.Bool && len(args) == 0 {
			val = "true"
		}

		// Skip binding if empty value for non-string types (unless bool handled above)
		if val == "" && field.Kind() != reflect.String && field.Kind() != reflect.Bool {
			return nil
		}

		return freflect.Bind(field, val)
	}
}
Handlers
Method

Handlers returns the registered handlers.

Returns

map[string]HandlerFunc
func (*Binder) Handlers() map[string]HandlerFunc
{
	return b.handlers
}
AddBool
Method

AddBool registers a custom handler for a boolean field.

Parameters

key string
fn func(bool) error
func (*Binder) AddBool(key string, fn func(bool) error)
{
	b.handlers[key] = func(args []string) error {
		var val bool
		if len(args) > 0 {
			fmt.Sscanf(args[0], "%t", &val)
		} else {
			val = true
		}
		return fn(val)
	}
}
AddInt
Method

AddInt registers a custom handler for an integer field.

Parameters

key string
fn func(int64) error
func (*Binder) AddInt(key string, fn func(int64) error)
{
	b.handlers[key] = func(args []string) error {
		if len(args) == 0 {
			return fmt.Errorf("missing value for %s", key)
		}
		var val int64
		if _, err := fmt.Sscanf(args[0], "%d", &val); err != nil {
			return err
		}
		return fn(val)
	}
}
AddStrings
Method

AddStrings registers a custom handler for a string slice field.

Parameters

key string
fn func([]string) error
func (*Binder) AddStrings(key string, fn func([]string) error)
{
	b.handlers[key] = fn
}
AddDuration
Method

AddDuration registers a custom handler for a duration field.

Parameters

key string
fn func(time.Duration) error
func (*Binder) AddDuration(key string, fn func(time.Duration) error)
{
	b.handlers[key] = func(args []string) error {
		if len(args) == 0 {
			return fmt.Errorf("missing value for %s", key)
		}
		val, err := time.ParseDuration(args[0])
		if err != nil {
			return err
		}
		return fn(val)
	}
}
AddEnum
Method

AddEnum registers a handler that validates against a set of allowed values.

Parameters

key string
choices []string
fn func(string) error
func (*Binder) AddEnum(key string, choices []string, fn func(string) error)
{
	b.handlers[key] = func(args []string) error {
		if len(args) == 0 {
			return fmt.Errorf("missing value for %s", key)
		}
		val := args[0]
		if !slices.Contains(choices, val) {
			return fmt.Errorf("invalid value %s for %s, allowed: %v", val, key, choices)
		}
		return fn(val)
	}
}
Run
Method

Run executes the handler for the given key with the provided arguments.

Parameters

key string
args []string

Returns

error
func (*Binder) Run(key string, args []string) error
{
	h, ok := b.handlers[key]
	if !ok {
		return fmt.Errorf("no handler for key: %s", key)
	}
	return h(args)
}

Fields

Name Type Description
dst any
handlers map[string]HandlerFunc
runner *hooks.Runner
F
function

NewBinder

NewBinder creates a new binder for the destination object.

Parameters

dst
any

Returns

error
internal/binder/binder.go:24-36
func NewBinder(dst any) (*Binder, error)

{
	b := &Binder{
		dst:      dst,
		handlers: make(map[string]HandlerFunc),
		runner:   hooks.NewRunner(),
	}

	if err := b.autoDiscover(); err != nil {
		return nil, err
	}

	return b, nil
}