cfg

package module
v1.0.0-rc3 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: MIT Imports: 12 Imported by: 0

README

cfg [WIP]

IMPORTANT: Work in Progress

Simplified settings for Go projects

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(key string) bool

func DateOnly

func DateOnly(key string, def ...time.Time) time.Time

func DateTime

func DateTime(key string, def ...time.Time) time.Time

func Duration

func Duration(key string, def ...time.Duration) time.Duration

func Float

func Float(key string, def ...float64) float64

func Get

func Get(key string) any

func Int

func Int(key string, def ...int) int

func JsonUnmarshal

func JsonUnmarshal(content []byte) (map[string]interface{}, error)

func Load

func Load() error

func LoadDotEnv

func LoadDotEnv() error

func LoadEnviron

func LoadEnviron(environ []string)

func LoadFiles

func LoadFiles() error

func LoadObject

func LoadObject(config O)

func LoadOsArgs

func LoadOsArgs(args []string)

func LoadOsEnv

func LoadOsEnv()

func LoadProfiles

func LoadProfiles() error

func Merge

func Merge(src *Env)

func SetFileExt

func SetFileExt(ext string, fn UnmarshalFn)

func SetFilePaths

func SetFilePaths(filePaths ...string)

func SetFileSystem

func SetFileSystem(fs FileSystem)

func SetLogger

func SetLogger(logger Logger)

func SetProfileKey

func SetProfileKey(profileKey string)

func SetString

func SetString(key string, value string)

func String

func String(key string, def ...string) string

func Strings

func Strings(key string, def ...[]string) []string

func Time

func Time(key string, def ...time.Time) time.Time

func TimeLayout

func TimeLayout(key string, layout string, def ...time.Time) time.Time

func TimeOnly

func TimeOnly(key string, def ...time.Time) time.Time

Types

type DefaultConfigFn

type DefaultConfigFn func(extra ...O) O

func CreateDefaultConfigFn

func CreateDefaultConfigFn(defaultValue O) DefaultConfigFn

CreateDefaultConfigFn simplifica a criação de configurações genéricas

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

Entry todas as propriedades são mapeadas para um tipo de dado abaixo, afim de respeitar corretamente a integração com Json

func ParseEntry

func ParseEntry(v any) *Entry

func (*Entry) Clone

func (e *Entry) Clone() *Entry

Clone faz um deep copy dessa entrada

func (*Entry) Kind

func (e *Entry) Kind() EntryKind

func (*Entry) Merge

func (e *Entry) Merge(other *Entry)

Merge faz a mesclagem de dois objetos

func (*Entry) Value

func (e *Entry) Value() any

func (*Entry) Walk

func (e *Entry) Walk(visitor func(*Entry))

type EntryKind

type EntryKind uint

EntryKind representa os tipos de dados suportados na configuração, afim de manter compatibilidade total com arquivos JSON

const (
	BoolKind   EntryKind = iota // bool, for JSON booleans
	NumberKind                  // float64, for JSON numbers
	StringKind                  // string, for JSON strings
	ArrayKind                   // []*Entry{}, for JSON arrays
	ObjectKind                  // map[string]*Entry{}, for JSON objects
)

type Env

type Env struct {
	// contains filtered or unexported fields
}

Env propriedades de configuração de um site ou globais.

func Clone

func Clone() *Env

func Global

func Global() *Env

func New

func New(defaults ...O) *Env

New 6) Default config

func (*Env) Bool

func (c *Env) Bool(key string) bool

Bool get a boolean value

func (*Env) Clone

func (c *Env) Clone() *Env

Clone make a copy of the config

func (*Env) DateOnly

func (c *Env) DateOnly(key string, def ...time.Time) time.Time

func (*Env) DateTime

func (c *Env) DateTime(key string, def ...time.Time) time.Time

func (*Env) Duration

func (c *Env) Duration(key string, def ...time.Duration) time.Duration

Duration obtém uma duração da config

func (*Env) Float

func (c *Env) Float(key string, def ...float64) float64

Float obtém um valor inteiro de uma config

func (*Env) Get

func (c *Env) Get(key string) any

Get a configuration property

func (*Env) Int

func (c *Env) Int(key string, def ...int) int

Int get an integer value from a config

func (*Env) Keys

func (c *Env) Keys(key string) []string

func (*Env) Load

func (c *Env) Load() error

Load initialize default settings. A variable is obtained respecting the order below.

1) command line arguments (starting with "--", e.g. --server.port=9000) 2) DotEnv file variables ".env" 3) Operating system variables 4) Profile specific configuration (config-{dev|prod|test}.json) 5) global config (config.json) 6) Default config (cfg.New(DefaultConfig))

func (*Env) LoadDotEnv

func (c *Env) LoadDotEnv() error

LoadDotEnv from https://github.com/joho/godotenv

func (*Env) LoadEnviron

func (c *Env) LoadEnviron(environ []string)

func (*Env) LoadFiles

func (c *Env) LoadFiles() error

LoadFiles processa arquivos de configuração

func (*Env) LoadObject

func (c *Env) LoadObject(config O)

LoadObject obtém as configurações a partir de um mapa em memória

func (*Env) LoadOsArgs

func (c *Env) LoadOsArgs(args []string)

LoadOsArgs will convert any command line option arguments (starting with ‘--’, e.g. --server.port=9000) to a property and add it to the Env.

Command line properties always take precedence over other property sources.

func (*Env) LoadOsEnv

func (c *Env) LoadOsEnv()

LoadOsEnv obtém todas as configurações do ambiente

func (*Env) LoadProfiles

func (c *Env) LoadProfiles() error

LoadProfiles processa arquivos de configuração (config.json)

func (*Env) Merge

func (c *Env) Merge(src *Env)

Merge faz o merge de src na config atual

func (*Env) Set

func (c *Env) Set(key string, value any)

Set a configuration property

func (*Env) SetFileExt

func (c *Env) SetFileExt(ext string, fn UnmarshalFn)

SetFileExt define o processador para essa extensão de arquivo. Usado para suportar .yaml, .toml, .xml

func (*Env) SetFilePaths

func (c *Env) SetFilePaths(filePaths ...string)

SetFilePaths define o caminho dos arquivos de configuração.

func (*Env) SetFileSystem

func (c *Env) SetFileSystem(fs FileSystem)

SetFileSystem define a instância do FileSystem que será usado para carregamento

func (*Env) SetLogger

func (c *Env) SetLogger(logger Logger)

func (*Env) SetProfileKey

func (c *Env) SetProfileKey(profileKey string)

SetProfileKey define a key que identifica os arquivos de perfil de configuração.

func (*Env) String

func (c *Env) String(key string, def ...string) string

String get a string value

func (*Env) Strings

func (c *Env) Strings(key string, def ...[]string) []string

func (*Env) Time

func (c *Env) Time(key string, def ...time.Time) time.Time

Time obtém uma duração da config

func (*Env) TimeLayout

func (c *Env) TimeLayout(key string, layout string, def ...time.Time) time.Time

TimeLayout obtém uma duração da config

func (*Env) TimeOnly

func (c *Env) TimeOnly(key string, def ...time.Time) time.Time

type FileSystem

type FileSystem interface {
	Exists(filepath string) (bool, error)
	Read(filepath string) ([]byte, error)
}

type Logger

type Logger interface {
	Error(string, ...interface{})
	Info(string, ...interface{})
	Warn(string, ...interface{})
}

Logger is implemented by any logging system that is used for standard logs.

type O

type O map[string]interface{}

O representa um objeto JSON, usado para criação de configurações padrão

func (O) Merge

func (o O) Merge(other O)

Merge faz a mesclagem do objeto atual com o outro sem manter referencia

type UnmarshalFn

type UnmarshalFn func(content []byte) (map[string]interface{}, error)

Jump to

Keyboard shortcuts

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