Documentation
¶
Index ¶
- Constants
- Variables
- func CompositeDecodeHook() mapstructure.DecodeHookFunc
- func DecodeStringToMap() mapstructure.DecodeHookFunc
- func GetStructKeys(typ reflect.Type, tag, squashValue string) []string
- func PrepareDir(path string) error
- func SetDefaults(v *viper.Viper, c any, defaults map[string]any)
- func StringToSliceWithBracketHookFunc() mapstructure.DecodeHookFunc
- func StringToStructHookFunc() mapstructure.DecodeHookFunc
- func ValidateMissingRequiredKeys(value interface{}, tag, squashValue string) []string
- type Manager
Constants ¶
const (
DefaultConfigType = "json"
)
Variables ¶
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 ¶
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 ¶
PrepareDir ensures that the specified directory path exists. If the directory does not exist, it attempts to create it.
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 ¶
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 ¶
New initializes the configuration settings. It sets up the name, type, and path for the configuration file.
func (*Manager) Load ¶
Load loads the configuration from the previously initialized file. It unmarshals the configuration into the provided conf interface.