mcfg

package
v0.0.0-...-961d35a Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultInstanceName 默认实例名称
	DefaultInstanceName = "config"
	// DefaultConfigFileName 默认配置文件名
	DefaultConfigFileName = "config"
)

Variables

This section is empty.

Functions

func IsExist

func IsExist(path string) bool

IsExist 检查文件是否存在

Types

type Adapter

type Adapter interface {
	// Get 获取指定键的配置值
	Get(ctx context.Context, pattern string) (any, error)

	// Data 获取所有配置数据
	Data(ctx context.Context) (map[string]any, error)

	// Available 检查和返回配置服务是否可用。
	// 可选参数 `resource` 指定某些配置资源。
	Available(ctx context.Context, resource ...string) bool
}

Adapter 定义配置适配器接口

type AdapterFile

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

func NewAdapterFile

func NewAdapterFile() (*AdapterFile, error)

NewAdapterFile 创建一个新的文件适配器

func (*AdapterFile) Available

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

Available 检查和后端配置服务是否可用。 可选参数 `resource` 指定某些配置资源。

func (*AdapterFile) Data

func (c *AdapterFile) Data(ctx context.Context) (map[string]any, error)

Data 获取所有配置数据

func (*AdapterFile) Get

func (c *AdapterFile) Get(ctx context.Context, pattern string) (any, error)

Get 获取配置值

func (*AdapterFile) SetFileName

func (c *AdapterFile) SetFileName(name string)

SetFileName 设置配置文件名

type Config

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

Config 是配置管理对象

func Instance

func Instance(name ...string) *Config

Instance 返回一个具有默认设置的 Config 实例 参数 `name` 是实例的名称。但需要注意的是,如果配置目录中存在文件 "name.yaml",则将其设置为默认配置文件

注意:如果配置目录中存在文件 "name.yaml",则将其设置为默认配置文件 如果配置目录中不存在文件 "name.yaml",则使用默认配置文件名 "config"

func New

func New() (*Config, error)

New 创建一个新的配置管理对象,并使用文件适配器

func NewWithAdapter

func NewWithAdapter(adapter Adapter) *Config

NewWithAdapter 创建一个新的配置管理对象

func (*Config) Available

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

Available 检查适配器是否可用 可选参数 `resource` 是资源名称,如果资源名称不为空,则检查资源是否可用

func (*Config) Data

func (c *Config) Data(ctx context.Context) (map[string]any, error)

Data 获取所有配置数据

func (*Config) Get

func (c *Config) Get(ctx context.Context, pattern string, def ...any) (*mvar.Var, error)

Get 获取指定键的配置值 可选参数 `def` 是默认值,如果配置值为空,则返回默认值 如果配置值为空,并且没有提供默认值,则返回 nil

func (*Config) GetAdapter

func (c *Config) GetAdapter() Adapter

GetAdapter 获取配置适配器

func (*Config) SetAdapter

func (c *Config) SetAdapter(adapter Adapter)

SetAdapter 设置配置适配器

Jump to

Keyboard shortcuts

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