internalhooks

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FlagDecodeHookAnnotation = "leodido/structcli/flag-decode-hooks"
)

Variables

View Source
var AnnotationToDecodeHookRegistry map[string]mapstructure.DecodeHookFunc

AnnotationToDecodeHookRegistry maps annotation names to decode hook functions

View Source
var DecodeHookRegistry = map[string]decodingAnnotation{
	"time.Duration": {
		"StringToTimeDurationHookFunc",
		mapstructure.StringToTimeDurationHookFunc(),
	},
	"[]time.Duration": {
		"StringToDurationSliceHookFunc",
		StringToDurationSliceHookFunc(),
	},
	"[]bool": {
		"StringToBoolSliceHookFunc",
		StringToBoolSliceHookFunc(),
	},
	"[]uint": {
		"StringToUintSliceHookFunc",
		StringToUintSliceHookFunc(),
	},
	"map[string]string": {
		"StringToStringMapHookFunc",
		StringToStringMapHookFunc(),
	},
	"map[string]int": {
		"StringToIntMapHookFunc",
		StringToIntMapHookFunc(),
	},
	"map[string]int64": {
		"StringToInt64MapHookFunc",
		StringToInt64MapHookFunc(),
	},
	"net.IP": {
		"StringToIPHookFunc",
		mapstructure.StringToIPHookFunc(),
	},
	"net.IPMask": {
		"StringToIPMaskHookFunc",
		StringToIPMaskHookFunc(),
	},
	"net.IPNet": {
		"StringToIPNetHookFunc",
		mapstructure.StringToIPNetHookFunc(),
	},
	"[]net.IP": {
		"StringToIPSliceHookFunc",
		StringToIPSliceHookFunc(),
	},
	"slog.Level": {
		"StringToSlogLevelHookFunc",
		StringToSlogLevelHookFunc(),
	},
	"[]string": {
		"StringToCSVStringSliceHookFunc",
		StringToCSVStringSliceHookFunc(),
	},
	"[]int": {
		"StringToIntSliceHookFunc",
		StringToIntSliceHookFunc(","),
	},
	"[]uint8": {
		"StringToRawBytesHookFunc",
		StringToRawBytesHookFunc(),
	},
	"structcli.Hex": {
		"StringToHexHookFunc",
		StringToNamedBytesHookFunc("structcli.Hex", decodeHexBytes),
	},
	"structcli.Base64": {
		"StringToBase64HookFunc",
		StringToNamedBytesHookFunc("structcli.Base64", decodeBase64Bytes),
	},
}
View Source
var DefineHookRegistry = map[string]DefineHookFunc{
	"time.Duration":     DefineTimeDurationHookFunc(),
	"[]time.Duration":   DefineDurationSliceHookFunc(),
	"[]bool":            DefineBoolSliceHookFunc(),
	"[]uint":            DefineUintSliceHookFunc(),
	"map[string]string": DefineStringMapHookFunc(),
	"map[string]int":    DefineIntMapHookFunc(),
	"map[string]int64":  DefineInt64MapHookFunc(),
	"net.IP":            DefineIPHookFunc(),
	"net.IPMask":        DefineIPMaskHookFunc(),
	"net.IPNet":         DefineIPNetHookFunc(),
	"[]net.IP":          DefineIPSliceHookFunc(),
	"slog.Level":        DefineSlogLevelHookFunc(),
	"[]uint8":           DefineRawBytesHookFunc(),
	"structcli.Hex":     DefineHexBytesHookFunc(),
	"structcli.Base64":  DefineBase64BytesHookFunc(),
}

DefineHookRegistry keeps track of the built-in flag definition functions

Functions

func InferDecodeHooks

func InferDecodeHooks(c *cobra.Command, name, typename string) bool

func InferDefineHooks

func InferDefineHooks(c *cobra.Command, name, short, descr string, structField reflect.StructField, fieldValue reflect.Value) bool

InferDefineHooks checks if there's a predefined flag definition function for the given type

func RegisterDecodeHook added in v0.16.0

func RegisterDecodeHook(typeName, annotationName string, hook mapstructure.DecodeHookFunc)

RegisterDecodeHook registers a decode hook for a custom type. It updates both DecodeHookRegistry and AnnotationToDecodeHookRegistry. Panics on duplicate annotation name (consistent with init() behavior).

func RestoreDecodeRegistries added in v0.16.0

func RestoreDecodeRegistries(snap DecodeRegistrySnapshot)

RestoreDecodeRegistries replaces both decode registries from a snapshot.

func StoreCompletionHookFunc added in v0.11.0

func StoreCompletionHookFunc(c *cobra.Command, flagName string, completeM reflect.Value)

StoreCompletionHookFunc registers a validated completion hook method for a flag. Panics if the flag does not exist (structurally impossible when called after flag registration) or if completeM is invalid.

func StoreDecodeHookFunc

func StoreDecodeHookFunc(c *cobra.Command, flagname string, decodeM reflect.Value, target reflect.Type)

func StringToBoolSliceHookFunc added in v0.12.0

func StringToBoolSliceHookFunc() mapstructure.DecodeHookFunc

func StringToCSVStringSliceHookFunc added in v0.12.0

func StringToCSVStringSliceHookFunc() mapstructure.DecodeHookFunc

StringToCSVStringSliceHookFunc converts textual input into []string using CSV semantics.

func StringToDurationSliceHookFunc added in v0.12.0

func StringToDurationSliceHookFunc() mapstructure.DecodeHookFunc

func StringToEnumHookFunc added in v0.16.0

func StringToEnumHookFunc[E ~string](values map[E][]string) mapstructure.DecodeHookFunc

StringToEnumHookFunc creates a decode hook that converts string values to a ~string enum type during configuration unmarshaling. It supports case-insensitive matching and aliases.

func StringToIPMaskHookFunc added in v0.12.0

func StringToIPMaskHookFunc() mapstructure.DecodeHookFunc

StringToIPMaskHookFunc converts textual input into net.IPMask.

func StringToIPSliceHookFunc added in v0.12.0

func StringToIPSliceHookFunc() mapstructure.DecodeHookFunc

StringToIPSliceHookFunc converts textual and list input into []net.IP.

func StringToInt64MapHookFunc added in v0.12.0

func StringToInt64MapHookFunc() mapstructure.DecodeHookFunc

func StringToIntEnumHookFunc added in v0.16.0

func StringToIntEnumHookFunc[E ~int | ~int8 | ~int16 | ~int32 | ~int64](values map[E][]string) mapstructure.DecodeHookFunc

StringToIntEnumHookFunc creates a decode hook that converts string values to an integer-based enum type during configuration unmarshaling. It supports case-insensitive matching and aliases.

func StringToIntMapHookFunc added in v0.12.0

func StringToIntMapHookFunc() mapstructure.DecodeHookFunc

func StringToIntSliceHookFunc

func StringToIntSliceHookFunc(sep string) mapstructure.DecodeHookFunc

StringToIntSliceHookFunc creates a decode hook that converts comma-separated string values to []int slices during configuration unmarshaling.

func StringToNamedBytesHookFunc added in v0.12.0

func StringToNamedBytesHookFunc(typeName string, decode func(string) ([]byte, error)) mapstructure.DecodeHookFunc

StringToNamedBytesHookFunc converts encoded textual input into a named []byte type.

func StringToRawBytesHookFunc added in v0.12.0

func StringToRawBytesHookFunc() mapstructure.DecodeHookFunc

StringToRawBytesHookFunc converts plain textual input into raw []byte.

func StringToSlogLevelHookFunc added in v0.9.1

func StringToSlogLevelHookFunc() mapstructure.DecodeHookFunc

StringToSlogLevelHookFunc creates a decode hook that converts string values to slog.Level types during configuration unmarshaling.

func StringToStringMapHookFunc added in v0.12.0

func StringToStringMapHookFunc() mapstructure.DecodeHookFunc

func StringToUintSliceHookFunc added in v0.12.0

func StringToUintSliceHookFunc() mapstructure.DecodeHookFunc

func WrapWithEnumValues added in v0.13.0

func WrapWithEnumValues(v pflag.Value, values []string) pflag.Value

WrapWithEnumValues wraps a pflag.Value to also satisfy structcli.EnumValuer.

Types

type CompleteHookFunc added in v0.11.0

type CompleteHookFunc func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)

CompleteHookFunc defines the optional completion hook for a struct field flag.

Methods matching the signature and naming convention `Complete<FieldName>` are discovered during Define() and automatically registered on the command.

type DecodeHookFunc

type DecodeHookFunc func(input any) (any, error)

type DecodeRegistrySnapshot added in v0.16.0

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

DecodeRegistrySnapshot holds opaque copies of both decode registries for test isolation.

func SnapshotDecodeRegistries added in v0.16.0

func SnapshotDecodeRegistries() DecodeRegistrySnapshot

SnapshotDecodeRegistries returns a deep copy of both decode registries.

type DefineHookFunc

type DefineHookFunc func(name, short, descr string, structField reflect.StructField, fieldValue reflect.Value) (pflag.Value, string)

DefineHookFunc defines how to create a flag for a custom type.

It receives flag metadata and struct field information and must return a pflag.Value that knows how to set the underlying field's value, along with an optional enhanced description for the flag's usage message.

func DefineBase64BytesHookFunc added in v0.12.0

func DefineBase64BytesHookFunc() DefineHookFunc

func DefineBoolSliceHookFunc added in v0.12.0

func DefineBoolSliceHookFunc() DefineHookFunc

func DefineDurationSliceHookFunc added in v0.12.0

func DefineDurationSliceHookFunc() DefineHookFunc

func DefineHexBytesHookFunc added in v0.12.0

func DefineHexBytesHookFunc() DefineHookFunc

func DefineIPHookFunc added in v0.12.0

func DefineIPHookFunc() DefineHookFunc

func DefineIPMaskHookFunc added in v0.12.0

func DefineIPMaskHookFunc() DefineHookFunc

func DefineIPNetHookFunc added in v0.12.0

func DefineIPNetHookFunc() DefineHookFunc

func DefineIPSliceHookFunc added in v0.12.0

func DefineIPSliceHookFunc() DefineHookFunc

func DefineInt64MapHookFunc added in v0.12.0

func DefineInt64MapHookFunc() DefineHookFunc

func DefineIntEnumHookFunc added in v0.16.0

func DefineIntEnumHookFunc[E ~int | ~int8 | ~int16 | ~int32 | ~int64](values map[E][]string) DefineHookFunc

DefineIntEnumHookFunc creates a DefineHookFunc for a registered integer-based enum. It wraps enumflag/v2 and attaches EnumValuer metadata via WrapWithEnumValues.

func DefineIntMapHookFunc added in v0.12.0

func DefineIntMapHookFunc() DefineHookFunc

func DefineRawBytesHookFunc added in v0.12.0

func DefineRawBytesHookFunc() DefineHookFunc

func DefineSlogLevelHookFunc added in v0.9.1

func DefineSlogLevelHookFunc() DefineHookFunc

DefineSlogLevelHookFunc creates a flag definition function for slog.Level.

It returns an enum flag that implements pflag.Value.

func DefineStringEnumHookFunc added in v0.16.0

func DefineStringEnumHookFunc[E ~string](values map[E][]string) DefineHookFunc

DefineStringEnumHookFunc creates a DefineHookFunc for a registered ~string enum. The returned hook creates an enumStringValue that validates on Set() and appends "{val1,val2,...}" to the flag description.

func DefineStringMapHookFunc added in v0.12.0

func DefineStringMapHookFunc() DefineHookFunc

func DefineTimeDurationHookFunc

func DefineTimeDurationHookFunc() DefineHookFunc

func DefineUintSliceHookFunc added in v0.12.0

func DefineUintSliceHookFunc() DefineHookFunc

Jump to

Keyboard shortcuts

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