environment

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package environment 提供环境配置管理功能,用于 enhance 框架。

该模块支持多环境配置加载、属性源管理、配置文件绑定、类型转换等环境配置相关功能。 参考 Spring Environment 的设计理念。

架构设计

  • Environment: 环境配置接口,定义配置访问操作
  • PropertySource: 属性源接口,表示配置数据来源
  • PropertyResolver: 属性解析器,解析和转换配置值
  • TypeConverter: 类型转换器,处理字符串到类型的转换
  • Profile: 环境配置,表示特定的运行环境

核心功能

  • 多环境配置: 支持 dev、test、prod 等多环境配置
  • 属性源管理: 支持多个属性源的优先级管理
  • 配置文件绑定: 支持从 JSON、YAML 等文件加载配置
  • 类型转换: 支持字符串到各种 Go 类型的转换
  • 属性占位符: 支持 ${key} 格式的占位符替换

使用方式

创建环境配置:

env := environment.NewEnvironment()
env.AddPropertySource(environment.NewFilePropertySource("application.json"))
env.AddPropertySource(environment.NewEnvPropertySource())

获取属性:

port := env.GetProperty("server.port", "8080")
timeout := env.GetPropertyAsInt("server.timeout", 30)

设置激活的环境:

env.SetActiveProfiles("dev", "local")

属性源优先级

属性源按添加顺序的逆序优先级(后添加的优先级更高):

  • 命令行参数(最高优先级)
  • 环境变量
  • 配置文件
  • 默认值(最低优先级)

配置文件格式

支持以下配置文件格式:

  • application.json: JSON 格式配置
  • application.yaml: YAML 格式配置
  • application-{profile}.json: 特定环境配置

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindConfig

func BindConfig[T any](env *Environment) (T, error)

BindConfig 泛型配置绑定

将环境配置绑定到指定类型的结构体并返回。 支持 config/mapstructure/value/env 标签。

示例:

type ServerConfig struct {
    Port int    `config:"server.port" default:"8080"`
    Host string `config:"server.host" default:"localhost"`
}

cfg, err := environment.BindConfig[ServerConfig](env)

func BindConfigPrefix

func BindConfigPrefix[T any](env *Environment, prefix string) (T, error)

BindConfigPrefix 泛型配置绑定(带前缀)

将指定前缀的配置绑定到结构体。

示例:

cfg, err := environment.BindConfigPrefix[ServerConfig](env, "server")

func BindConfigRequired

func BindConfigRequired[T any](env *Environment) (T, error)

BindConfigRequired 泛型配置绑定(必填)

绑定配置,如果任何带 required 标签的字段缺失则返回错误。

func FindApplicationConfigFile

func FindApplicationConfigFile() string

FindApplicationConfigFile 查找应用配置文件

在以下路径中查找 application.json:

  1. 当前目录
  2. ./config 目录
  3. 可执行文件所在目录
  4. 可执行文件所在目录的 ./config 子目录

func FindDefaultConfigFile

func FindDefaultConfigFile() string

FindDefaultConfigFile 查找默认配置文件(向后兼容)

@deprecated 使用 FindApplicationConfigFile 代替

func GetConfigFileExtension

func GetConfigFileExtension(configType ConfigType) string

GetConfigFileExtension 根据配置类型返回文件扩展名

func GetProfileActive

func GetProfileActive(args []string) string

GetProfileActive 从命令行参数或环境变量中获取激活的 Profile

优先级:命令行参数 --profile=xxx > 环境变量 GO_BOOT_PROFILE。 命令行参数格式:--profile=dev

func MustBindConfig

func MustBindConfig[T any](env *Environment) T

MustBindConfig 泛型配置绑定(失败时 panic)

适用于启动时配置,配置错误应该直接终止程序。

func MustBindConfigPrefix

func MustBindConfigPrefix[T any](env *Environment, prefix string) T

MustBindConfigPrefix 泛型配置绑定带前缀(失败时 panic)

func ParseProfiles

func ParseProfiles(raw string) []string

ParseProfiles 解析逗号分隔的 Profile 字符串为切片

例如 "dev,test" -> ["dev", "test"],会自动去除空格和空项。

Types

type ArgsPropertySource

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

ArgsPropertySource 命令行参数配置源。

func NewArgsPropertySource

func NewArgsPropertySource(name string, args []string) *ArgsPropertySource

NewArgsPropertySource 解析命令行参数并创建配置源

支持的格式:--key=value

func (*ArgsPropertySource) Contains

func (a *ArgsPropertySource) Contains(key string) bool

func (*ArgsPropertySource) GetProperty

func (a *ArgsPropertySource) GetProperty(key string) (any, bool)

func (*ArgsPropertySource) Name

func (a *ArgsPropertySource) Name() string

func (*ArgsPropertySource) Priority

func (a *ArgsPropertySource) Priority() Priority

type ConfigLoader

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

ConfigLoader 配置文件加载器

在预设搜索路径中查找配置文件,支持基础配置和 Profile 配置的分层加载。 Profile 配置会覆盖基础配置中的同名属性。

加载流程:

  1. 如果指定了 configLocation,直接加载该文件
  2. 否则搜索 configName.{configType} 作为基础配置
  3. 依次搜索 configName-{profile}.{configType} 作为 Profile 配置

func NewConfigLoader

func NewConfigLoader(configName string, configType ConfigType, configLocation string, profiles []string) *ConfigLoader

NewConfigLoader 创建配置文件加载器

参数:

  • configName: 配置文件名(不含扩展名),如 "application"
  • configType: 配置文件类型,如 ConfigTypeJSON
  • configLocation: 自定义配置文件路径,为空时自动搜索
  • profiles: 激活的 Profile 列表

func NewConfigLoaderWithPaths

func NewConfigLoaderWithPaths(configName string, configType ConfigType, configLocation string, profiles []string, searchPaths []string) *ConfigLoader

NewConfigLoaderWithPaths 创建配置文件加载器并指定搜索路径

参数:

  • configName: 配置文件名(不含扩展名),如 "application"
  • configType: 配置文件类型,如 ConfigTypeJSON
  • configLocation: 自定义配置文件路径,为空时自动搜索
  • profiles: 激活的 Profile 列表
  • searchPaths: 自定义搜索路径列表

func (*ConfigLoader) Load

func (l *ConfigLoader) Load() ([]PropertySource, error)

Load 加载配置文件并返回配置源列表

返回的配置源按加载顺序排列:基础配置在前,Profile 配置在后。 Profile 配置的优先级高于基础配置。

type ConfigType

type ConfigType string

ConfigType 配置文件类型枚举

const (
	// ConfigTypeJSON JSON 配置文件类型
	ConfigTypeJSON ConfigType = "json"
)

func ParseConfigType

func ParseConfigType(filePath string) ConfigType

ParseConfigType 根据文件路径解析配置类型(当前仅支持 JSON)

type EnvPropertySource

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

EnvPropertySource 环境变量配置源。

func NewEnvPropertySource

func NewEnvPropertySource(name, prefix string) *EnvPropertySource

NewEnvPropertySource 创建环境变量配置源

prefix 是环境变量前缀,例如 "GO_BOOT"。 环境变量 GO_BOOT_SERVER_PORT=9090 将映射为键 "server.port"。

func (*EnvPropertySource) Contains

func (e *EnvPropertySource) Contains(key string) bool

func (*EnvPropertySource) GetProperty

func (e *EnvPropertySource) GetProperty(key string) (any, bool)

GetProperty 从环境变量获取配置

键 "server.port" 将转换为环境变量 "GO_BOOT_SERVER_PORT"(如果 prefix="GO_BOOT")。

func (*EnvPropertySource) Name

func (e *EnvPropertySource) Name() string

func (*EnvPropertySource) Priority

func (e *EnvPropertySource) Priority() Priority

type Environment

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

Environment 环境配置管理器

参考 Spring 的 Environment 抽象,提供分层配置源管理和 Profile 机制。 配置源按优先级排序,高优先级覆盖低优先级。 支持命令行参数(最高优先级)> 环境变量 > 配置文件(最低优先级)。

func CreateEnvironment

func CreateEnvironment(opts ...EnvironmentOption) *Environment

CreateEnvironment 创建并配置环境

func NewEnvironment

func NewEnvironment() *Environment

NewEnvironment 创建环境配置管理器

默认配置源优先级(从高到低):

  1. 命令行参数(--key=value)
  2. 环境变量(GO_BOOT_ 前缀)
  3. 应用 JSON 配置文件(application.json)

func (*Environment) AcceptsProfile

func (e *Environment) AcceptsProfile(profile string) bool

AcceptsProfile 检查指定 Profile 是否被当前环境接受

支持否定前缀 "!",如 "!dev" 表示非 dev 环境时匹配。 检查流程:

  1. 检查是否有否定前缀
  2. 在激活的 Profile 列表中查找
  3. 含否定前缀时:Profile 不在激活列表中返回 true
  4. 无否定前缀时:Profile 在激活列表中返回 true

func (*Environment) AddActiveProfile

func (e *Environment) AddActiveProfile(profile string)

AddActiveProfile 激活指定 Profile.

如果该 Profile 已经激活,则忽略.

参数:

  • profile: Profile 名称

func (*Environment) AddConfigChangeListener

func (e *Environment) AddConfigChangeListener(listener func(refresh.ConfigChangeEvent))

AddConfigChangeListener 添加配置变更监听器

func (*Environment) AddPropertySource

func (e *Environment) AddPropertySource(source PropertySource)

AddPropertySource 添加配置源到环境.

新添加的配置源会按优先级排序,高优先级覆盖低优先级.

参数:

  • source: 配置源实例

func (*Environment) AddPropertySourceFirst

func (e *Environment) AddPropertySourceFirst(source PropertySource)

AddPropertySourceFirst 添加最高优先级的配置源.

新添加的配置源会插入到配置源列表头部,成为最高优先级的来源.

参数:

  • source: 配置源实例

func (*Environment) Bind

func (e *Environment) Bind(target any) error

Bind 将整个环境配置绑定到目标结构体

func (*Environment) BindKey

func (e *Environment) BindKey(key string, target any) error

BindKey 将指定键的值绑定到目标

func (*Environment) BindPrefix

func (e *Environment) BindPrefix(prefix string, target any) error

BindPrefix 将指定前缀的配置绑定到目标结构体

func (*Environment) BindProperties

func (e *Environment) BindProperties(target any) error

BindProperties 绑定配置属性到结构体(支持 value tag)

支持以下 tag:

  • value:"${key}" — 从配置中获取 key 的值
  • value:"${key:default}" — 从配置中获取 key 的值,不存在时使用默认值
  • mapstructure:"key" — 从配置中获取 key 的值(无占位符)
  • env:"KEY" — 从环境变量获取

示例:

type ServerConfig struct {
    Port    int           `value:"${server.port:8080}"`
    Host    string        `value:"${server.host:0.0.0.0}"`
    Timeout time.Duration `value:"${server.timeout:30s}"`
}

var config ServerConfig
env.BindProperties(&config)

func (*Environment) ContainsProperty

func (e *Environment) ContainsProperty(key string) bool

ContainsProperty 检查属性是否存在.

参数:

  • key: 属性键名

返回:

  • bool: 是否存在

func (*Environment) GetActiveProfiles

func (e *Environment) GetActiveProfiles() []string

GetActiveProfiles 获取当前激活的 Profile 列表.

返回:

  • []string: Profile 名称列表

func (*Environment) GetBool

func (e *Environment) GetBool(key string, defaultVal bool) bool

GetBool 获取布尔类型的属性值.

支持 bool 和字符串 "true"/"false" 形式.

参数:

  • key: 属性键名
  • defaultVal: 属性不存在时返回的默认值

返回:

  • bool: 属性值,不存在时返回 defaultVal

func (*Environment) GetFloat64

func (e *Environment) GetFloat64(key string, defaultVal float64) float64

GetFloat64 获取 float64 类型属性

func (*Environment) GetInt

func (e *Environment) GetInt(key string, defaultVal int) int

GetInt 获取整数类型的属性值.

支持 int、float64 和字符串形式的整数值.

参数:

  • key: 属性键名
  • defaultVal: 属性不存在时返回的默认值

返回:

  • int: 属性值,不存在时返回 defaultVal

func (*Environment) GetProperty

func (e *Environment) GetProperty(key string) (any, bool)

GetProperty 从所有配置源中获取属性值

遍历配置源(从高优先级到低优先级),返回第一个匹配的值。 如果值是字符串且包含 ${...} 占位符,自动递归解析。 高优先级的配置源会覆盖低优先级的同名属性。

func (*Environment) GetPropertySources

func (e *Environment) GetPropertySources() []PropertySource

GetPropertySources 获取所有配置源列表.

返回按优先级排序的配置源副本,高优先级在后.

返回:

  • []PropertySource: 配置源列表

func (*Environment) GetRequiredProperty

func (e *Environment) GetRequiredProperty(key string) (any, error)

GetRequiredProperty 获取必需属性,不存在时返回错误

func (*Environment) GetString

func (e *Environment) GetString(key, defaultVal string) string

GetString 获取字符串类型的属性值.

参数:

  • key: 属性键名
  • defaultVal: 属性不存在时返回的默认值

返回:

  • string: 属性值,不存在时返回 defaultVal

func (*Environment) IsPropertyEmpty

func (e *Environment) IsPropertyEmpty(key string) bool

IsPropertyEmpty 检查属性是否为空(不存在或空字符串)

func (*Environment) RemoveProfile

func (e *Environment) RemoveProfile(profile string)

RemoveProfile 移除激活的 Profile

func (*Environment) RemovePropertySource

func (e *Environment) RemovePropertySource(name string)

RemovePropertySource 按名称移除配置源

func (*Environment) ResolvePlaceholders

func (e *Environment) ResolvePlaceholders(val string) string

ResolvePlaceholders 解析 ${...} 占位符(公有 API)

支持语法:

  • ${key} — 引用配置参数 key
  • ${key:defaultValue} — 引用 key,不存在时使用 defaultValue

func (*Environment) Validate

func (e *Environment) Validate() []error

Validate 验证配置,返回所有错误

type EnvironmentBuilder

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

EnvironmentBuilder 环境配置构建器,支持链式配置

func NewEnvironmentBuilder

func NewEnvironmentBuilder() *EnvironmentBuilder

NewEnvironmentBuilder 创建环境配置构建器

func (*EnvironmentBuilder) Build

func (b *EnvironmentBuilder) Build() *Environment

Build 构建环境配置

func (*EnvironmentBuilder) MustBuild

func (b *EnvironmentBuilder) MustBuild() *Environment

MustBuild 构建环境配置,失败则panic

func (*EnvironmentBuilder) WithArgs

func (b *EnvironmentBuilder) WithArgs(args ...string) *EnvironmentBuilder

WithArgs 添加命令行参数配置源

func (*EnvironmentBuilder) WithEnvPrefix

func (b *EnvironmentBuilder) WithEnvPrefix(prefix string) *EnvironmentBuilder

WithEnvPrefix 添加带前缀的环境变量配置源

func (*EnvironmentBuilder) WithJSONConfig

func (b *EnvironmentBuilder) WithJSONConfig(filePath string) *EnvironmentBuilder

WithJSONConfig 添加JSON配置文件

func (*EnvironmentBuilder) WithProfile

func (b *EnvironmentBuilder) WithProfile(profile string) *EnvironmentBuilder

WithProfile 添加激活的 Profile

func (*EnvironmentBuilder) WithProfiles

func (b *EnvironmentBuilder) WithProfiles(profiles ...string) *EnvironmentBuilder

WithProfiles 批量添加激活的 Profiles

func (*EnvironmentBuilder) WithPropertySource

func (b *EnvironmentBuilder) WithPropertySource(source PropertySource) *EnvironmentBuilder

WithPropertySource 添加配置源

func (*EnvironmentBuilder) WithPropertySourceFirst

func (b *EnvironmentBuilder) WithPropertySourceFirst(source PropertySource) *EnvironmentBuilder

WithPropertySourceFirst 添加最高优先级的配置源

type EnvironmentConfig

type EnvironmentConfig struct {
	Profiles           []string         // 激活的 Profiles
	PropertySources    []PropertySource // 配置源列表
	DefaultProfile     string           // 默认 Profile
	AutoDetectProfiles bool             // 是否自动检测 Profiles
}

EnvironmentConfig 环境配置

func DefaultEnvironmentConfig

func DefaultEnvironmentConfig() *EnvironmentConfig

DefaultEnvironmentConfig 返回默认环境配置

func (*EnvironmentConfig) ApplyOptions

func (c *EnvironmentConfig) ApplyOptions(opts []EnvironmentOption)

ApplyOptions 应用配置选项

type EnvironmentHelper

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

EnvironmentHelper 环境辅助工具,简化环境配置操作

func NewEnvironmentHelper

func NewEnvironmentHelper(env *Environment) *EnvironmentHelper

NewEnvironmentHelper 创建环境辅助工具

func (*EnvironmentHelper) ContainsProperty

func (h *EnvironmentHelper) ContainsProperty(key string) bool

ContainsProperty 检查属性是否存在

func (*EnvironmentHelper) GetActiveProfile

func (h *EnvironmentHelper) GetActiveProfile() string

GetActiveProfile 获取当前激活的 Profile

func (*EnvironmentHelper) GetBool

func (h *EnvironmentHelper) GetBool(key string, defaultVal bool) bool

GetBool 获取布尔类型属性

func (*EnvironmentHelper) GetFloat64

func (h *EnvironmentHelper) GetFloat64(key string, defaultVal float64) float64

GetFloat64 获取浮点数类型属性

func (*EnvironmentHelper) GetInt

func (h *EnvironmentHelper) GetInt(key string, defaultVal int) int

GetInt 获取整数类型属性

func (*EnvironmentHelper) GetRequiredProperty

func (h *EnvironmentHelper) GetRequiredProperty(key string) (any, error)

GetRequiredProperty 获取必需属性

func (*EnvironmentHelper) GetString

func (h *EnvironmentHelper) GetString(key string, defaultVal string) string

GetString 获取字符串类型属性

func (*EnvironmentHelper) IsDev

func (h *EnvironmentHelper) IsDev() bool

IsDev 检查是否为开发环境

func (*EnvironmentHelper) IsProd

func (h *EnvironmentHelper) IsProd() bool

IsProd 检查是否为生产环境

func (*EnvironmentHelper) IsTest

func (h *EnvironmentHelper) IsTest() bool

IsTest 检查是否为测试环境

func (*EnvironmentHelper) WithPrefix

func (h *EnvironmentHelper) WithPrefix(prefix string) *EnvironmentHelper

WithPrefix 创建带前缀的新辅助工具

type EnvironmentOption

type EnvironmentOption func(*EnvironmentConfig)

EnvironmentOption 环境配置选项

func WithAutoDetectProfiles

func WithAutoDetectProfiles(enabled bool) EnvironmentOption

WithAutoDetectProfiles 设置是否自动检测 Profiles

func WithDefaultProfile

func WithDefaultProfile(profile string) EnvironmentOption

WithDefaultProfile 设置默认 Profile

func WithProfiles

func WithProfiles(profiles ...string) EnvironmentOption

WithProfiles 设置激活的 Profiles

func WithPropertySources

func WithPropertySources(sources ...PropertySource) EnvironmentOption

WithPropertySources 设置配置源列表

type EnvironmentTemplate

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

EnvironmentTemplate 环境模板,提供常用的环境配置模板

func NewEnvironmentTemplate

func NewEnvironmentTemplate(env *Environment) *EnvironmentTemplate

NewEnvironmentTemplate 创建环境模板

func (*EnvironmentTemplate) GetDatabaseHost

func (t *EnvironmentTemplate) GetDatabaseHost(defaultVal string) string

GetDatabaseHost 获取数据库主机

func (*EnvironmentTemplate) GetDatabaseName

func (t *EnvironmentTemplate) GetDatabaseName(defaultVal string) string

GetDatabaseName 获取数据库名称

func (*EnvironmentTemplate) GetDatabasePort

func (t *EnvironmentTemplate) GetDatabasePort(defaultVal int) int

GetDatabasePort 获取数据库端口

func (*EnvironmentTemplate) GetDatabaseURL

func (t *EnvironmentTemplate) GetDatabaseURL(defaultVal string) string

GetDatabaseURL 获取数据库连接URL

func (*EnvironmentTemplate) GetLogLevel

func (t *EnvironmentTemplate) GetLogLevel(defaultVal string) string

GetLogLevel 获取日志级别

func (*EnvironmentTemplate) GetRedisHost

func (t *EnvironmentTemplate) GetRedisHost(defaultVal string) string

GetRedisHost 获取Redis主机

func (*EnvironmentTemplate) GetRedisPassword

func (t *EnvironmentTemplate) GetRedisPassword(defaultVal string) string

GetRedisPassword 获取Redis密码

func (*EnvironmentTemplate) GetRedisPort

func (t *EnvironmentTemplate) GetRedisPort(defaultVal int) int

GetRedisPort 获取Redis端口

func (*EnvironmentTemplate) GetServerHost

func (t *EnvironmentTemplate) GetServerHost(defaultVal string) string

GetServerHost 获取服务器主机

func (*EnvironmentTemplate) GetServerPort

func (t *EnvironmentTemplate) GetServerPort(defaultVal int) int

GetServerPort 获取服务器端口

func (*EnvironmentTemplate) IsDebugMode

func (t *EnvironmentTemplate) IsDebugMode() bool

IsDebugMode 检查是否启用调试模式

func (*EnvironmentTemplate) IsVerbose

func (t *EnvironmentTemplate) IsVerbose() bool

IsVerbose 检查是否启用详细日志

type JSONPropertySource

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

JSONPropertySource 基于 JSON 文件的配置源

从 JSON 文件加载配置,支持嵌套结构。 使用 PriorityLowest 优先级,作为默认配置源。

func NewJSONPropertySource

func NewJSONPropertySource(name, filePath string) (*JSONPropertySource, error)

NewJSONPropertySource 从 JSON 文件创建配置源

filePath 是 JSON 文件的完整路径。 如果文件不存在或解析失败,返回空配置源。

func NewJSONPropertySourceOrDefault

func NewJSONPropertySourceOrDefault(name, filePath string) *JSONPropertySource

NewJSONPropertySourceOrDefault 从 JSON 文件创建配置源,如果失败则返回空配置源

filePath 是 JSON 文件的完整路径。 如果文件不存在或解析失败,返回空配置源但不报错。

func (*JSONPropertySource) Contains

func (j *JSONPropertySource) Contains(key string) bool

func (*JSONPropertySource) GetProperty

func (j *JSONPropertySource) GetProperty(key string) (any, bool)

GetProperty 从 JSON 数据中获取配置

支持点分隔的嵌套键名,如 "server.port"。

func (*JSONPropertySource) Keys

func (j *JSONPropertySource) Keys() []string

Keys 返回所有扁平化的键名

将嵌套结构转换为点分隔的键名,如 "server.port"。

func (*JSONPropertySource) Name

func (j *JSONPropertySource) Name() string

func (*JSONPropertySource) Priority

func (j *JSONPropertySource) Priority() Priority

type MapPropertySource

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

MapPropertySource 基于 map 的内存配置源。

func NewDefaultPropertySource

func NewDefaultPropertySource(name string, data map[string]any) *MapPropertySource

NewDefaultPropertySource 创建最低优先级的默认配置源

使用 PriorityLowest 优先级,被所有其他配置源覆盖。

func NewMapPropertySource

func NewMapPropertySource(name string, priority Priority, data map[string]any) *MapPropertySource

NewMapPropertySource 创建 MapPropertySource

func (*MapPropertySource) Contains

func (m *MapPropertySource) Contains(key string) bool

func (*MapPropertySource) GetProperty

func (m *MapPropertySource) GetProperty(key string) (any, bool)

func (*MapPropertySource) Keys

func (m *MapPropertySource) Keys() []string

Keys 返回所有键

func (*MapPropertySource) Name

func (m *MapPropertySource) Name() string

func (*MapPropertySource) Priority

func (m *MapPropertySource) Priority() Priority

type Priority

type Priority int

Priority 优先级类型,值越大优先级越高。

const (
	// PriorityLowest 最低优先级,应用配置使用
	PriorityLowest Priority = iota
	// PriorityLow 低优先级
	PriorityLow
	// PriorityNormal 正常优先级
	PriorityNormal
	// PriorityHigh 高优先级,环境变量使用
	PriorityHigh
	// PriorityHighest 最高优先级,命令行使用
	PriorityHighest
)

type PropertySource

type PropertySource interface {
	Name() string
	GetProperty(key string) (any, bool)
	Priority() Priority
	Contains(key string) bool
}

PropertySource 配置源接口。

代表一个配置数据源,具有名称和优先级。

type TypeConverter

type TypeConverter struct{}

TypeConverter 类型转换器

统一的类型转换逻辑,消除重复代码。

func NewTypeConverter

func NewTypeConverter() *TypeConverter

NewTypeConverter 创建类型转换器

func (*TypeConverter) ConvertTo

func (c *TypeConverter) ConvertTo(val any, targetType reflect.Type) (reflect.Value, error)

ConvertTo 将值转换为目标类型

Jump to

Keyboard shortcuts

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