confKit

package
v2.1.58 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package confKit

支持的配置文件格式(详见viper.go): "yaml", "yml", "json", "toml", "hcl", "tfvars", "dotenv", "env", "properties", "props", "prop", "ini"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(path string, ptr any, options ...conf.Option) error

func MustLoad

func MustLoad(path string, ptr any, options ...conf.Option)

MustLoad 读取配置文件并反序列化为 指定结构体指针ptr

PS: (1) default对应的是 该行被注释掉(而非值为空) 的情况; (2) default和 '|' 一起使用; (3) range和 ':' 一起使用.

!!!: (0) 所有字段首字母大写 && 有json tag; (1) 结构体可以参考 go-zero的 rest.RestConf,可以通过 tag 控制配置的值(默认值default、范围range、可选optional...;可以组合) (2) 值为数组时要注意(要么有值(size > 0),要么全注释掉),以免返回error. (3) tag range(定义当前字段数值范围)支持:

[:5] (:5] [:5) (:5)
[1:] [1:) (1:] (1:)
[1:5] [1:5) (1:5] (1:5)

e.g.
type config struct {
	Number1 int           `json:"number1,range=[1:]"`
}

可能返回的error:
(a) wrong number range setting: 不满足tag range

@param path 配置文件的路径(推荐使用.yaml) @param ptr [不能为nil] 结构体实例的指针 @param opts e.g. conf.UseEnv()

e.g. 结构体属性的tag(https://www.w3cschool.cn/gozero/gozero-eo623nm5.html

json:"name,optional"
json:"gender,options=male|female"
json:"gender,default=male"
json:"age,range=[0:120]"

e.g.1 组合多个tag

Port     int    `json:"port,optional,range=[-1:65535]"`
Port     int    `json:"port,default=-1,range=[-1:65535]"`

func ReadAs

func ReadAs(data []byte, configType string, defaultMap map[string]interface{}, ptr interface{}) error

ReadAs 读取配置文本内容,并反序列化.

@param configType 配置文件的类型(不区分大小写,详见viper.go):"yaml", "yml", "json", "toml", "hcl", "tfvars", "dotenv", "env", "properties", "props", "prop", "ini" @param defaultMap 默认值,可以为nil @param ptr 指针,且不能为nil

func ReadFileAs

func ReadFileAs(filePath string, defaultMap map[string]interface{}, ptr interface{}) error

ReadFileAs 读取配置文件,并反序列化.

PS: (1) 配置文件 和 defaultMap 中,key首字母的大小写无所谓,都支持; (2) 支持配置文件的格式:JSON, TOML, HCL, .env, .yaml, .properties.

@param filePath 配置文件的路径(绝对路径 和 相对路径 都支持),内部会判断文件是否存在 @param defaultMap (可以为nil) 默认值;key如果有多层的话,用"."分隔,e.g. "WoService.LowerLimit" @param ptr (不能为nil) 指针

Types

This section is empty.

Jump to

Keyboard shortcuts

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