internalhooks

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FlagDecodeHookAnnotation = "___leodido_structcli_flagdecodehooks"
)

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(),
	},
	"zapcore.Level": {
		"StringToZapcoreLevelHookFunc",
		StringToZapcoreLevelHookFunc(),
	},
	"slog.Level": {
		"StringToSlogLevelHookFunc",
		StringToSlogLevelHookFunc(),
	},
	"[]string": {
		"StringToSliceHookFunc",
		mapstructure.StringToSliceHookFunc(","),
	},
	"[]int": {
		"StringToIntSliceHookFunc",
		StringToIntSliceHookFunc(","),
	},
}
View Source
var DefineHookRegistry = map[string]DefineHookFunc{
	"zapcore.Level": DefineZapcoreLevelHookFunc(),
	"time.Duration": DefineTimeDurationHookFunc(),
	"slog.Level":    DefineSlogLevelHookFunc(),
}

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 StoreDecodeHookFunc

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

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 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 StringToZapcoreLevelHookFunc

func StringToZapcoreLevelHookFunc() mapstructure.DecodeHookFunc

StringToZapcoreLevelHookFunc creates a decode hook that converts string values to zapcore.Level types during configuration unmarshaling.

Types

type DecodeHookFunc

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

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 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 DefineTimeDurationHookFunc

func DefineTimeDurationHookFunc() DefineHookFunc

func DefineZapcoreLevelHookFunc

func DefineZapcoreLevelHookFunc() DefineHookFunc

DefineZapcoreLevelHookFunc creates a flag definition function for zapcore.Level.

It returns an enum flag that implements pflag.Value.

Jump to

Keyboard shortcuts

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