viper

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 12 Imported by: 0

README ΒΆ

Compogo Viper πŸ¦™

Compogo Viper β€” это готовая интСграция spf13/viper с Ρ„Ρ€Π΅ΠΉΠΌΠ²ΠΎΡ€ΠΊΠΎΠΌ Compogo. ДобавляСтся ΠΎΠ΄Π½ΠΎΠΉ строкой ΠΈ автоматичСски настраиваСт ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ†ΠΈΡŽ Ρ‡Π΅Ρ€Π΅Π· Ρ„Π°ΠΉΠ»Ρ‹ ΠΈ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ окруТСния.

πŸš€ Установка

go get github.com/Compogo/viper

πŸ“¦ ИспользованиС

package main

import (
    "github.com/Compogo/compogo"
    "github.com/Compogo/dig"
    "github.com/Compogo/viper"
    "github.com/Compogo/logrus"
    "github.com/Compogo/myapp/service"
)

func main() {
    app := compogo.NewApp("myapp",
        compogo.WithOsSignalCloser(),
        dig.WithDig(),
        viper.WithViper(),                     // ← ΠΎΠ΄Π½Π° строка
        logrus.WithLogrus(),
        compogo.WithComponents(
            service.Component,
        ),
    )

    if err := app.Serve(); err != nil {
        panic(err)
    }
}

✨ ВозмоТности

🎯 Π’Ρ€ΠΈ способа ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ‚ΠΎΡ€

Π’ любом ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Π΅ ΠΌΠΎΠΆΠ½ΠΎ Π·Π°ΠΏΡ€ΠΎΡΠΈΡ‚ΡŒ:

// 1. Как интСрфСйс configurator.Configurator
type Service struct {
    cfg configurator.Configurator
}

// 2. Как *viper.Decorator (с Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹ΠΌΠΈ ΠΌΠ΅Ρ‚ΠΎΠ΄Π°ΠΌΠΈ)
type Service struct {
    dec *viper.Decorator
}

// 3. Как чистый *viper.Viper
type Service struct {
    v *viper.Viper
}
🎚️ ПолноС ΠΏΠΎΠΊΡ€Ρ‹Ρ‚ΠΈΠ΅ Ρ‚ΠΈΠΏΠΎΠ²
GetString(key string) string
GetBool(key string) bool
GetInt(key string) int
GetInt8(key string) int8
GetInt16(key string) int16
GetInt32(key string) int32
GetInt64(key string) int64
GetUint(key string) uint
GetUint8(key string) uint8
GetUint16(key string) uint16
GetUint32(key string) uint32
GetUint64(key string) uint64
GetFloat32(key string) float32
GetFloat64(key string) float64
GetTime(key string) time.Time
GetDuration(key string) time.Duration
GetIntSlice(key string) []int
GetStringSlice(key string) []string
GetStringMap(key string) map[string]interface{}
GetStringMapString(key string) map[string]string
GetStringMapStringSlice(key string) map[string][]string
GetSizeInBytes(key string) uint

πŸ“„ ΠŸΡ€ΠΈΠΌΠ΅Ρ€ ΠΊΠΎΠ½Ρ„ΠΈΠ³Π° (config.yaml)

app:
  name: myapp
  port: 8080
  
database:
  host: localhost
  port: 5432
  user: postgres

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

View Source
const (
	PathFieldName              = "config.path"
	NameFieldName              = "config.name"
	TypeFieldName              = "config.type"
	EnvKeyReplaceFromFieldName = "config.env.replace.from"
	EnvKeyReplaceToFieldName   = "config.env.replace.to"

	PathDefault              = "."
	NameDefault              = "config"
	TypeDefault              = "yaml"
	EnvKeyReplaceFromDefault = "."
	EnvKeyReplaceToDefault   = "_"
)

Variables ΒΆ

This section is empty.

Functions ΒΆ

func WithViper ΒΆ

func WithViper() compogo.Option

Types ΒΆ

type Config ΒΆ

type Config struct {
	Path              string
	Name              string
	Type              string
	EnvKeyReplaceFrom string
	EnvKeyReplaceTo   string
}

func Configuration ΒΆ

func Configuration(config *Config) *Config

func NewConfig ΒΆ

func NewConfig() *Config

type Decorator ΒΆ

type Decorator struct {
	*viper.Viper
	// contains filtered or unexported fields
}

func NewDecorator ΒΆ

func NewDecorator() *Decorator

func (*Decorator) GetBool ΒΆ added in v0.0.9

func (decorator *Decorator) GetBool(s string) bool

func (*Decorator) GetDuration ΒΆ added in v0.0.9

func (decorator *Decorator) GetDuration(s string) time.Duration

func (*Decorator) GetFloat32 ΒΆ

func (decorator *Decorator) GetFloat32(s string) float32

func (*Decorator) GetFloat64 ΒΆ added in v0.0.9

func (decorator *Decorator) GetFloat64(s string) float64

func (*Decorator) GetInt ΒΆ added in v0.0.9

func (decorator *Decorator) GetInt(s string) int

func (*Decorator) GetInt8 ΒΆ

func (decorator *Decorator) GetInt8(s string) int8

func (*Decorator) GetInt16 ΒΆ

func (decorator *Decorator) GetInt16(s string) int16

func (*Decorator) GetInt32 ΒΆ added in v0.0.9

func (decorator *Decorator) GetInt32(s string) int32

func (*Decorator) GetInt64 ΒΆ added in v0.0.9

func (decorator *Decorator) GetInt64(s string) int64

func (*Decorator) GetIntSlice ΒΆ added in v0.0.9

func (decorator *Decorator) GetIntSlice(s string) []int

func (*Decorator) GetSizeInBytes ΒΆ added in v0.0.9

func (decorator *Decorator) GetSizeInBytes(s string) uint

func (*Decorator) GetString ΒΆ added in v0.0.9

func (decorator *Decorator) GetString(s string) string

func (*Decorator) GetStringMap ΒΆ added in v0.0.9

func (decorator *Decorator) GetStringMap(s string) map[string]any

func (*Decorator) GetStringMapString ΒΆ added in v0.0.9

func (decorator *Decorator) GetStringMapString(s string) map[string]string

func (*Decorator) GetStringMapStringSlice ΒΆ added in v0.0.9

func (decorator *Decorator) GetStringMapStringSlice(s string) map[string][]string

func (*Decorator) GetStringSlice ΒΆ added in v0.0.9

func (decorator *Decorator) GetStringSlice(s string) []string

func (*Decorator) GetTime ΒΆ added in v0.0.9

func (decorator *Decorator) GetTime(s string) time.Time

func (*Decorator) GetUint ΒΆ added in v0.0.9

func (decorator *Decorator) GetUint(s string) uint

func (*Decorator) GetUint8 ΒΆ added in v0.0.9

func (decorator *Decorator) GetUint8(s string) uint8

func (*Decorator) GetUint16 ΒΆ added in v0.0.9

func (decorator *Decorator) GetUint16(s string) uint16

func (*Decorator) GetUint32 ΒΆ added in v0.0.9

func (decorator *Decorator) GetUint32(s string) uint32

func (*Decorator) GetUint64 ΒΆ added in v0.0.9

func (decorator *Decorator) GetUint64(s string) uint64

func (*Decorator) ReadConfig ΒΆ

func (decorator *Decorator) ReadConfig() (err error)

func (*Decorator) SetDefault ΒΆ added in v0.0.9

func (decorator *Decorator) SetDefault(s string, a any)

func (*Decorator) With ΒΆ added in v0.0.9

func (decorator *Decorator) With(s string) configurator.Configurator

Jump to

Keyboard shortcuts

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