config

package
v0.0.0-...-b00be9e Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultConfigType = "json"
)

Variables

View Source
var (
	// ERROR
	ErrInvalidDirectory  = errors.New("invalid directory path")
	ErrMissingConfigName = errors.New("config name not specified")
)

Functions

func CompositeDecodeHook

func CompositeDecodeHook() mapstructure.DecodeHookFunc

CompositeDecodeHook 组合所有解码钩子

func DecodeStringToMap

func DecodeStringToMap() mapstructure.DecodeHookFunc

DecodeStringToMap returns a DecodeHookFunc that converts a string to a map[string]string. The string is expected to be a comma-separated list of key-value pairs, where the key and value are separated by an equal sign.

func GetStructKeys

func GetStructKeys(typ reflect.Type, tag, squashValue string) []string

GetStructKeys returns all keys in a nested struct type, taking the name from the tag name or the field name. It handles an additional suffix squashValue like mapstructure does: if present on an embedded struct, name components for that embedded struct should not be included. It does not handle maps, does chase pointers, but does not check for loops in nesting.

func PrepareDir

func PrepareDir(path string) error

PrepareDir ensures that the specified directory path exists. If the directory does not exist, it attempts to create it.

func SetDefaults

func SetDefaults(v *viper.Viper, c any, defaults map[string]any)

func StringToSliceWithBracketHookFunc

func StringToSliceWithBracketHookFunc() mapstructure.DecodeHookFunc

StringToSliceWithBracketHookFunc returns a DecodeHookFunc that converts a string to a slice of strings. Useful when configuration values are provided as JSON arrays in string form, but need to be parsed into slices. The string is expected to be a JSON array. If the string is empty, an empty slice is returned. If the string cannot be parsed as a JSON array, the original data is returned unchanged.

func StringToStructHookFunc

func StringToStructHookFunc() mapstructure.DecodeHookFunc

StringToStructHookFunc returns a DecodeHookFunc that converts a string to a struct. Useful for parsing configuration values that are provided as JSON strings but need to be converted to sturcts. The string is expected to be a JSON object that can be unmarshaled into the target struct. If the string is empty, a new instance of the target struct is returned. If the string cannot be parsed as a JSON object, the original data is returned unchanged.

func ValidateMissingRequiredKeys

func ValidateMissingRequiredKeys(value interface{}, tag, squashValue string) []string

ValidateMissingRequiredKeys returns all keys of value in GetStructKeys format that have an additional required tag set but are unset.

Types

type Manager

type Manager struct {
	App         string
	EnvPrefix   string
	Path        string
	Name        string
	WriteConfig bool

	Viper *viper.Viper
}

func New

func New(app, path, name, envPrefix string, writeConfig bool) (*Manager, error)

New initializes the configuration settings. It sets up the name, type, and path for the configuration file.

func (*Manager) GetConfig

func (c *Manager) GetConfig() map[string]interface{}

GetConfig retrieves all configuration settings as a map.

func (*Manager) Load

func (c *Manager) Load(conf interface{}) error

Load loads the configuration from the previously initialized file. It unmarshals the configuration into the provided conf interface.

func (*Manager) LoadFile

func (c *Manager) LoadFile(file string, conf interface{}) error

LoadFile loads the configuration from a specified file. It unmarshals the configuration into the provided conf interface.

func (*Manager) SetConfig

func (c *Manager) SetConfig(key string, value interface{}) error

SetConfig sets a configuration key to a specified value. It also writes the updated configuration back to the file.

Jump to

Keyboard shortcuts

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