Documentation
¶
Index ¶
- Constants
- Variables
- func WithViper() compogo.Option
- type Config
- type Decorator
- func (decorator *Decorator) GetBool(s string) bool
- func (decorator *Decorator) GetDuration(s string) time.Duration
- func (decorator *Decorator) GetFloat32(s string) float32
- func (decorator *Decorator) GetFloat64(s string) float64
- func (decorator *Decorator) GetInt(s string) int
- func (decorator *Decorator) GetInt8(s string) int8
- func (decorator *Decorator) GetInt16(s string) int16
- func (decorator *Decorator) GetInt32(s string) int32
- func (decorator *Decorator) GetInt64(s string) int64
- func (decorator *Decorator) GetIntSlice(s string) []int
- func (decorator *Decorator) GetSizeInBytes(s string) uint
- func (decorator *Decorator) GetString(s string) string
- func (decorator *Decorator) GetStringMap(s string) map[string]any
- func (decorator *Decorator) GetStringMapString(s string) map[string]string
- func (decorator *Decorator) GetStringMapStringSlice(s string) map[string][]string
- func (decorator *Decorator) GetStringSlice(s string) []string
- func (decorator *Decorator) GetTime(s string) time.Time
- func (decorator *Decorator) GetUint(s string) uint
- func (decorator *Decorator) GetUint8(s string) uint8
- func (decorator *Decorator) GetUint16(s string) uint16
- func (decorator *Decorator) GetUint32(s string) uint32
- func (decorator *Decorator) GetUint64(s string) uint64
- func (decorator *Decorator) ReadConfig() (err error)
- func (decorator *Decorator) SetDefault(s string, a any)
- func (decorator *Decorator) With(s string) compogo.Configurator
Constants ¶
const ( // PathFieldName — имя поля для пути к конфигурационным файлам. PathFieldName = "config.path" // NameFieldName — имя поля для имени конфигурационного файла (без расширения). NameFieldName = "config.name" // TypeFieldName — имя поля для типа конфигурационного файла (yaml, json, toml, и т.д.). TypeFieldName = "config.type" // EnvKeyReplaceFromFieldName — имя поля для замены в ключах переменных окружения (из). EnvKeyReplaceFromFieldName = "config.env.replace.from" // EnvKeyReplaceToFieldName — имя поля для замены в ключах переменных окружения (в). EnvKeyReplaceToFieldName = "config.env.replace.to" )
Variables ¶
var ( // PathDefault — путь по умолчанию для конфигурационных файлов (текущая директория). PathDefault = "." // NameDefault — имя файла по умолчанию (config). NameDefault = "config" // TypeDefault — тип файла по умолчанию (yaml). TypeDefault = "yaml" // EnvKeyReplaceFromDefault — символ для замены в ключах переменных окружения по умолчанию (точка). EnvKeyReplaceFromDefault = "." // EnvKeyReplaceToDefault — символ, на который заменяется в ключах переменных окружения по умолчанию (подчёркивание). EnvKeyReplaceToDefault = "_" )
Functions ¶
Types ¶
type Config ¶
type Config struct {
Path string
Name string
Type string
EnvKeyReplaceFrom string
EnvKeyReplaceTo string
}
Config содержит настройки конфигуратора Viper.
func Configuration ¶
Configuration заполняет конфигурацию значениями по умолчанию, если они не были установлены через флаги или другие источники.
type Decorator ¶
Decorator реализует интерфейс compogo.Configurator на основе Viper. Добавляет поддержку вложенных конфигураций через With().
Decorator автоматически:
- Загружает конфигурацию из файлов (YAML, JSON, TOML)
- Читает переменные окружения
- Позволяет переопределять значения через флаги
func (*Decorator) GetDuration ¶ added in v0.0.9
GetDuration возвращает time.Duration значение по ключу.
func (*Decorator) GetFloat32 ¶
GetFloat32 возвращает float32 значение по ключу.
func (*Decorator) GetFloat64 ¶ added in v0.0.9
GetFloat64 возвращает float64 значение по ключу.
func (*Decorator) GetIntSlice ¶ added in v0.0.9
GetIntSlice возвращает []int значение по ключу.
func (*Decorator) GetSizeInBytes ¶ added in v0.0.9
GetSizeInBytes возвращает размер в байтах по ключу (поддерживает 10MB, 1GB и т.д.).
func (*Decorator) GetStringMap ¶ added in v0.0.9
GetStringMap возвращает map[string]any значение по ключу.
func (*Decorator) GetStringMapString ¶ added in v0.0.9
GetStringMapString возвращает map[string]string значение по ключу.
func (*Decorator) GetStringMapStringSlice ¶ added in v0.0.9
GetStringMapStringSlice возвращает map[string][]string значение по ключу.
func (*Decorator) GetStringSlice ¶ added in v0.0.9
GetStringSlice возвращает []string значение по ключу.
func (*Decorator) ReadConfig ¶
ReadConfig читает конфигурацию из файла. Если файл не найден и используется имя по умолчанию, ошибка игнорируется.
func (*Decorator) SetDefault ¶ added in v0.0.9
SetDefault устанавливает значение по умолчанию для ключа.
func (*Decorator) With ¶ added in v0.0.9
func (decorator *Decorator) With(s string) compogo.Configurator
With возвращает новый Configurator с указанным префиксом ключа. Позволяет работать с вложенными конфигурациями.
Пример:
dbCfg := configurator.With("database")
dsn := dbCfg.GetString("dsn") // читает ключ "database.dsn"