confKit

package
v2.2.44 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 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

View Source
var LoadFromJsonBytes func(content []byte, v any) error = conf.LoadFromJsonBytes

LoadFromJsonBytes 加载 .json 格式的配置文件内容.

View Source
var LoadFromTomlBytes func(content []byte, v any) error = conf.LoadFromTomlBytes

LoadFromTomlBytes 加载 .toml 格式的配置文件内容.

View Source
var LoadFromYamlBytes func(content []byte, v any) error = conf.LoadFromYamlBytes

LoadFromYamlBytes 加载 .yaml 格式的配置文件内容.

Functions

func Load

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

func LoadFromJsonText added in v2.2.24

func LoadFromJsonText(text string, v any) error

func LoadFromTomlText added in v2.2.24

func LoadFromTomlText(text string, v any) error

func LoadFromYamlText added in v2.2.24

func LoadFromYamlText(text string, v any) error

func MustLoad

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

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

使用 go-zero/core/conf 的缺陷: 不能自动将string类型转换为int类型,会直接返回error.

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 MustLoadFromJsonBytes added in v2.2.1

func MustLoadFromJsonBytes(content []byte, v any)

func MustLoadFromJsonText added in v2.2.24

func MustLoadFromJsonText(text string, v any)

func MustLoadFromTomlBytes added in v2.2.24

func MustLoadFromTomlBytes(content []byte, v any)

func MustLoadFromTomlText added in v2.2.24

func MustLoadFromTomlText(text string, v any)

func MustLoadFromYamlBytes added in v2.2.1

func MustLoadFromYamlBytes(content []byte, v any)

func MustLoadFromYamlText added in v2.2.24

func MustLoadFromYamlText(text string, v any)

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