gcfg

package
v0.0.0-...-ac9d817 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package gcfg provides reading, caching and managing for configuration.

Index

Examples

Constants

View Source
const (
	DefaultInstanceName   = "config" // DefaultName is the default instance name for instance usage.
	DefaultConfigFileName = "config" // DefaultConfigFile is the default configuration file name.
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter interface {
	// Available checks and returns the backend configuration service is available.
	// The optional parameter `resource` specifies certain configuration resource.
	//
	// Note that this function does not return error as it just does simply check for
	// backend configuration service.
	Available(ctx context.Context, resource ...string) (ok bool)

	// Get retrieves and returns value by specified `pattern` in current resource.
	// Pattern like:
	// "x.y.z" for map item.
	// "x.0.y" for slice item.
	Get(ctx context.Context, pattern string) (value interface{}, err error)

	// Data retrieves and returns all configuration data in current resource as map.
	// Note that this function may lead lots of memory usage if configuration data is too large,
	// you can implement this function if necessary.
	Data(ctx context.Context) (data map[string]interface{}, err error)
}

Adapter is the interface for configuration retrieving.

type AdapterContent

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

AdapterContent implements interface Adapter using content. The configuration content supports the coding types as package `gjson`.

func NewAdapterContent

func NewAdapterContent(content ...string) (*AdapterContent, error)

NewAdapterContent returns a new configuration management object using custom content. The parameter `content` specifies the default configuration content for reading.

func (*AdapterContent) Available

func (a *AdapterContent) Available(ctx context.Context, resource ...string) (ok bool)

Available checks and returns the backend configuration service is available. The optional parameter `resource` specifies certain configuration resource.

Note that this function does not return error as it just does simply check for backend configuration service.

func (*AdapterContent) Data

func (a *AdapterContent) Data(ctx context.Context) (data map[string]interface{}, err error)

Data retrieves and returns all configuration data in current resource as map. Note that this function may lead lots of memory usage if configuration data is too large, you can implement this function if necessary.

func (*AdapterContent) Get

func (a *AdapterContent) Get(ctx context.Context, pattern string) (value interface{}, err error)

Get retrieves and returns value by specified `pattern` in current resource. Pattern like: "x.y.z" for map item. "x.0.y" for slice item.

func (*AdapterContent) SetContent

func (a *AdapterContent) SetContent(content string) error

SetContent sets customized configuration content for specified `file`. The `file` is unnecessary param, default is DefaultConfigFile.

type AdapterFile

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

AdapterFile implements interface Adapter using file.

func NewAdapterFile

func NewAdapterFile(file ...string) (*AdapterFile, error)

NewAdapterFile returns a new configuration management object. The parameter `file` specifies the default configuration file name for reading.

func (*AdapterFile) AddPath

func (a *AdapterFile) AddPath(directoryPaths ...string) (err error)

AddPath adds an absolute or relative `directory` path to the search paths.

Note that this parameter is paths to a directories not files.

func (*AdapterFile) Available

func (a *AdapterFile) Available(ctx context.Context, fileName ...string) bool

Available checks and returns whether configuration of given `file` is available.

func (*AdapterFile) Clear

func (a *AdapterFile) Clear()

Clear removes all parsed configuration files content cache, which will force reload configuration content from file.

func (*AdapterFile) ClearContent

func (a *AdapterFile) ClearContent()

ClearContent removes all global configuration contents.

func (*AdapterFile) Data

func (a *AdapterFile) Data(ctx context.Context) (data map[string]interface{}, err error)

Data retrieves and returns all configuration data as map type.

func (*AdapterFile) Dump

func (a *AdapterFile) Dump()

Dump prints current Json object with more manually readable.

func (*AdapterFile) Get

func (a *AdapterFile) Get(ctx context.Context, pattern string) (value interface{}, err error)

Get retrieves and returns value by specified `pattern`. It returns all values of current Json object if `pattern` is given empty or string ".". It returns nil if no value found by `pattern`.

We can also access slice item by its index number in `pattern` like: "list.10", "array.0.name", "array.0.1.id".

It returns a default value specified by `def` if value for `pattern` is not found.

func (*AdapterFile) GetContent

func (a *AdapterFile) GetContent(file ...string) string

GetContent returns customized configuration content for specified `file`. The `file` is unnecessary param, default is DefaultConfigFile.

func (*AdapterFile) GetFileName

func (a *AdapterFile) GetFileName() string

GetFileName returns the default configuration file name.

func (*AdapterFile) GetFilePath

func (a *AdapterFile) GetFilePath(fileName ...string) (filePath string, err error)

GetFilePath returns the absolute configuration file path for the given filename by `file`. If `file` is not passed, it returns the configuration file path of the default name. It returns an empty `path` string and an error if the given `file` does not exist.

func (*AdapterFile) GetPaths

func (a *AdapterFile) GetPaths() []string

GetPaths returns the searching directory path array of current configuration manager.

func (*AdapterFile) MustGet

func (a *AdapterFile) MustGet(ctx context.Context, pattern string) *gvar.Var

MustGet acts as function Get, but it panics if error occurs.

func (*AdapterFile) RemoveContent

func (a *AdapterFile) RemoveContent(file ...string)

RemoveContent removes the global configuration with specified `file`. If `name` is not passed, it removes configuration of the default group name.

func (*AdapterFile) Set

func (a *AdapterFile) Set(pattern string, value interface{}) error

Set sets value with specified `pattern`. It supports hierarchical data access by char separator, which is '.' in default. It is commonly used for updates certain configuration value in runtime. Note that, it is not recommended using `Set` configuration at runtime as the configuration would be automatically refreshed if underlying configuration file changed.

func (*AdapterFile) SetContent

func (a *AdapterFile) SetContent(content string, file ...string)

SetContent sets customized configuration content for specified `file`. The `file` is unnecessary param, default is DefaultConfigFile.

func (*AdapterFile) SetFileName

func (a *AdapterFile) SetFileName(name string)

SetFileName sets the default configuration file name.

func (*AdapterFile) SetPath

func (a *AdapterFile) SetPath(directoryPath string) (err error)

SetPath sets the configuration `directory` path for file search. The parameter `path` can be absolute or relative `directory` path, but absolute `directory` path is strongly recommended.

Note that this parameter is a path to a directory not a file.

func (*AdapterFile) SetViolenceCheck

func (a *AdapterFile) SetViolenceCheck(check bool)

SetViolenceCheck sets whether to perform hierarchical conflict checking. This feature needs to be enabled when there is a level symbol in the key name. It is off in default.

Note that, turning on this feature is quite expensive, and it is not recommended allowing separators in the key names. It is best to avoid this on the application side.

type Config

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

Config is the configuration management object.

func Instance

func Instance(name ...string) *Config

Instance returns an instance of Config with default settings. The parameter `name` is the name for the instance. But very note that, if the file "name.toml" exists in the configuration directory, it then sets it as the default configuration file. The toml file type is the default configuration file type.

func New

func New() (*Config, error)

New creates and returns a Config object with default adapter of AdapterFile.

func NewWithAdapter

func NewWithAdapter(adapter Adapter) *Config

NewWithAdapter creates and returns a Config object with given adapter.

func (*Config) Available

func (c *Config) Available(ctx context.Context, resource ...string) (ok bool)

Available checks and returns the configuration service is available. The optional parameter `pattern` specifies certain configuration resource.

It returns true if configuration file is present in default AdapterFile, or else false. Note that this function does not return error as it just does simply check for backend configuration service.

func (*Config) Data

func (c *Config) Data(ctx context.Context) (data map[string]interface{}, err error)

Data retrieves and returns all configuration data as map type.

func (*Config) Get

func (c *Config) Get(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)

Get retrieves and returns value by specified `pattern`. It returns all values of current Json object if `pattern` is given empty or string ".". It returns nil if no value found by `pattern`.

It returns a default value specified by `def` if value for `pattern` is not found.

func (*Config) GetAdapter

func (c *Config) GetAdapter() Adapter

GetAdapter returns the adapter of current Config object.

func (*Config) GetWithCmd

func (c *Config) GetWithCmd(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)

GetWithCmd returns the configuration value specified by pattern `pattern`. If the configuration value does not exist, then it retrieves and returns the command line option specified by `key`. It returns the default value `def` if none of them exists.

Fetching Rules: Command line arguments are in lowercase format, eg: gf.package.variable.

Example
package main

import (
	"fmt"
	"os"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcmd"
	"github.com/gogf/gf/v2/os/gctx"
)

func main() {
	var (
		key = `cmd.test`
		ctx = gctx.New()
	)
	v, err := g.Cfg().GetWithCmd(ctx, key)
	if err != nil {
		panic(err)
	}
	fmt.Printf("cmd:%s\n", v)
	// Re-Initialize custom command arguments.
	os.Args = append(os.Args, fmt.Sprintf(`--%s=yes`, key))
	gcmd.Init(os.Args...)
	// Retrieve the configuration and command option again.
	v, err = g.Cfg().GetWithCmd(ctx, key)
	if err != nil {
		panic(err)
	}
	fmt.Printf("cmd:%s", v)

}
Output:

cmd:
cmd:yes

func (*Config) GetWithEnv

func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)

GetWithEnv returns the configuration value specified by pattern `pattern`. If the configuration value does not exist, then it retrieves and returns the environment value specified by `key`. It returns the default value `def` if none of them exists.

Fetching Rules: Environment arguments are in uppercase format, eg: GF_PACKAGE_VARIABLE.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/os/genv"
)

func main() {
	var (
		key = `ENV_TEST`
		ctx = gctx.New()
	)
	v, err := g.Cfg().GetWithEnv(ctx, key)
	if err != nil {
		panic(err)
	}
	fmt.Printf("env:%s\n", v)
	if err = genv.Set(key, "gf"); err != nil {
		panic(err)
	}
	v, err = g.Cfg().GetWithEnv(ctx, key)
	if err != nil {
		panic(err)
	}
	fmt.Printf("env:%s", v)

}
Output:

env:
env:gf

func (*Config) MustData

func (c *Config) MustData(ctx context.Context) map[string]interface{}

MustData acts as function Data, but it panics if error occurs.

func (*Config) MustGet

func (c *Config) MustGet(ctx context.Context, pattern string, def ...interface{}) *gvar.Var

MustGet acts as function Get, but it panics if error occurs.

func (*Config) MustGetWithCmd

func (c *Config) MustGetWithCmd(ctx context.Context, pattern string, def ...interface{}) *gvar.Var

MustGetWithCmd acts as function GetWithCmd, but it panics if error occurs.

func (*Config) MustGetWithEnv

func (c *Config) MustGetWithEnv(ctx context.Context, pattern string, def ...interface{}) *gvar.Var

MustGetWithEnv acts as function GetWithEnv, but it panics if error occurs.

func (*Config) SetAdapter

func (c *Config) SetAdapter(adapter Adapter)

SetAdapter sets the adapter of current Config object.

Jump to

Keyboard shortcuts

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