viperKit

package
v2.8.115 Latest Latest
Warning

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

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

Documentation

Overview

Package viperKit

支持的配置文件格式(详见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 GetContentType added in v2.8.112

func GetContentType(path string) string

func IsContentTypeSupported added in v2.8.112

func IsContentTypeSupported(contentType string) bool

IsContentTypeSupported

@param extName "json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"

func MarshalToFile deprecated added in v2.8.109

func MarshalToFile(obj interface{}, target string) error

MarshalToFile

Deprecated: 不建议用此方法, (1) 转换为 .yaml 还是用 yamlKit 吧;

(2) 转换为 .properties 有问题.

@param obj 如果为nil,将返回error

func PolyfillContentType added in v2.8.112

func PolyfillContentType(contentType string) string

PolyfillContentType

e.g.

extname := ".JSON"
fmt.Println(viperKit.PolyfillContentType(extname)) // "json"

@param extName "json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"

func Read added in v2.8.109

func Read(data []byte, configType string, defaultMap map[string]interface{}) (*viper.Viper, error)

func ReadFile added in v2.8.109

func ReadFile(filePath string, defaultMap map[string]interface{}) (*viper.Viper, error)

func Unmarshal added in v2.8.109

func Unmarshal(content []byte, configType string, defaultMap map[string]interface{}, ptr interface{}) (*viper.Viper, error)

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

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

func UnmarshalFromData added in v2.8.111

func UnmarshalFromData(data *Data, defaultMap map[string]interface{}, ptr interface{}) (*viper.Viper, error)

func UnmarshalFromFile added in v2.8.109

func UnmarshalFromFile(filePath string, defaultMap map[string]interface{}, ptr interface{}) (*viper.Viper, error)

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

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

type Data added in v2.8.111

type Data struct {
	// Content 配置文件的内容
	Content []byte `json:"data" yaml:"data"`

	// Type 配置文件的类型,"yaml", "yml", "json", "toml", "hcl", "tfvars", "dotenv", "env", "properties", "props", "prop", "ini"
	Type string `json:"type" yaml:"type"`
}

Data 配置文件的数据.

func NewDataFromFile added in v2.8.113

func NewDataFromFile(path string) (*Data, error)

Jump to

Keyboard shortcuts

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