Documentation
¶
Index ¶
- func Exists(confName string) bool
- func MustParse(confName string, obj any)
- func MustParseByAbsPath(confAbsPath string, obj any)
- func MustParseBytes(fileExt string, content []byte, obj any)
- func MustRegisterHook(h Hook)
- func MustRegisterParser(fileExt string, fn DecoderFunc)
- func Parse(confName string, obj any) (err error)
- func ParseByAbsPath(confAbsPath string, obj any) (err error)
- func ParseBytes(fileExt string, content []byte, obj any) error
- func RegisterHook(h Hook) error
- func RegisterParser(fileExt string, fn DecoderFunc) error
- type AutoChecker
- type Configure
- func (c *Configure) Clone() *Configure
- func (c *Configure) Exists(confName string) bool
- func (c *Configure) MustRegisterHook(h Hook)
- func (c *Configure) Parse(confName string, obj any) (err error)
- func (c *Configure) ParseByAbsPath(confAbsPath string, obj any) (err error)
- func (c *Configure) ParseBytes(fileExt string, content []byte, obj any) error
- func (c *Configure) RegisterHook(h Hook) error
- func (c *Configure) RegisterParser(fileExt string, fn DecoderFunc) error
- func (c *Configure) WithContext(ctx context.Context) *Configure
- func (c *Configure) WithHook(hs ...Hook) *Configure
- type DecoderFunc
- type Hook
- type HookParam
- type HookTPL
- type Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Exists ¶
Exists (全局)判断是否存在
confName 的文件后缀是可选的,当查找文件不存在时,会添加上支持的后缀依次去判断。 如 Exists("app.toml") 会去 {ConfDir}/app.toml 判断
func MustParseByAbsPath ¶ added in v0.4.0
MustParseByAbsPath 调用 ParseByAbsPath,若返回 err!=ni 则 panic
func MustParseBytes ¶ added in v0.4.0
MustParseBytes 调用 ParseBytes,若返回 err!=ni 则 panic
func MustRegisterHook ¶ added in v0.3.0
func MustRegisterHook(h Hook)
MustRegisterHook (全局)注册一个辅助类,若失败会 panic
func MustRegisterParser ¶ added in v0.4.0
func MustRegisterParser(fileExt string, fn DecoderFunc)
MustRegisterParser 调用 RegisterParser,若返回的 err!=nil 则 panic
func ParseByAbsPath ¶
ParseByAbsPath 解析绝对路径的配置
func ParseBytes ¶ added in v0.2.0
ParseBytes (全局)解析 bytes fileExt 是文件后缀,如.json、.toml
Example ¶
package main import ( "fmt" "log" "github.com/fsgo/fsconf" ) func main() { type User struct { Name string Age int } content := []byte(`{"Name":"Hello","age":18}`) var user *User if err := fsconf.ParseBytes(".json", content, &user); err != nil { log.Fatalln("ParseBytes with error:", err) } fmt.Println("Name=", user.Name) fmt.Println("Age=", user.Age) }
Output: Name= Hello Age= 18
func RegisterParser ¶
func RegisterParser(fileExt string, fn DecoderFunc) error
RegisterParser (全局)注册一个解析器 fileExt 是文件后缀,如 .json
Types ¶
type AutoChecker ¶ added in v0.2.5
type AutoChecker interface {
AutoCheck() error
}
AutoChecker 当配置解析完成后,用于自动校验, 这个方法是在 validator 校验完成之后才执行的
type Configure ¶ added in v0.2.0
type Configure struct {
// contains filtered or unexported fields
}
func SetDefault ¶ added in v0.3.0
func WithContext ¶ added in v0.2.0
WithContext (全局)返回新的对象,并设置新的 ctx
func (*Configure) MustRegisterHook ¶ added in v0.3.0
MustRegisterHook 注册新的 Hook, 若失败会 panic
func (*Configure) ParseByAbsPath ¶ added in v0.2.0
func (*Configure) ParseBytes ¶ added in v0.2.0
func (*Configure) RegisterHook ¶ added in v0.2.2
RegisterHook 注册新的 Hook,若出现重名会注册失败
func (*Configure) RegisterParser ¶ added in v0.2.0
func (c *Configure) RegisterParser(fileExt string, fn DecoderFunc) error
func (*Configure) WithContext ¶ added in v0.2.0
type DecoderFunc ¶ added in v0.4.0
DecoderFunc 针对特定文件后缀的配置解析方法 当前已经内置了 .toml 和 .json的解析方法
type Hook ¶ added in v0.2.2
type Hook interface { // Name 名称,不可为空 // 每个 Hook 应返回唯一的名称,若重名会注册失败 Name() string // Execute 对读取的配置内容加工的逻辑 Execute(ctx context.Context, p *HookParam) (output []byte, err error) }
Hook 辅助类,在执行解析前,会先会配置的内容进行解析处理
type HookParam ¶ added in v0.2.2
type HookParam struct { FileExt string // 文件类型后缀,如 .toml,.json Configure *Configure // 当前 Configure 对象 ConfPath string // 文件路径。当直接解析内容时,为空字符串 Content []byte // 文件内容 }
HookParam param for Hook
type HookTPL ¶ added in v0.3.0
type HookTPL struct { // HookName 名称,必填 HookName string // KeyPrefix 查找的表达式前缀,必填 // 最终表达是为 {$RegexpKey.([A-Za-z0-9_]+)} KeyPrefix string // Values 用于查找的值,必填 Values map[string]string // contains filtered or unexported fields }
HookTPL 一个通用的可用于替换内容的 Hook 模版
type Validator ¶ added in v0.2.5
Validator 自动规则校验器
默认使用的 github.com/go-playground/validator/v10 如下设置所有字段都是必填的:
type Address struct { Street string `validate:"required"` City string `validate:"required"` Planet string `validate:"required"` Phone string `validate:"required"` }
var DefaultValidator Validator