本文整理汇总了Golang中github.com/kitcambridge/envconf.Environment.DecodeStrict方法的典型用法代码示例。如果您正苦于以下问题:Golang Environment.DecodeStrict方法的具体用法?Golang Environment.DecodeStrict怎么用?Golang Environment.DecodeStrict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/kitcambridge/envconf.Environment
的用法示例。
在下文中一共展示了Environment.DecodeStrict方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: LoadConfigStruct
func LoadConfigStruct(sectionName string, env envconf.Environment,
config toml.Primitive, configable HasConfigStruct) (
configStruct interface{}, err error) {
if configStruct = configable.ConfigStruct(); configStruct == nil {
return configStruct, nil
}
// Global section options
// SimplePush defines some common parameters
// that are defined in the ExtensibleGlobals struct.
// Use reflection to extract the tagged ExtensibleGlobals fields.
var pg ExtensibleGlobals
rt := reflect.TypeOf(pg)
ignoreConfig := make(map[string]interface{}, rt.NumField())
ignoreEnv := make(map[string]interface{}, rt.NumField())
for i := 0; i < rt.NumField(); i++ {
sft := rt.Field(i)
kname := sft.Tag.Get("toml")
if len(kname) == 0 {
kname = sft.Name
}
ignoreConfig[kname] = true
if kname = sft.Tag.Get("env"); len(kname) == 0 {
kname = sft.Name
}
ignoreEnv[toEnvName(sectionName, kname)] = true
}
if err = toml.PrimitiveDecodeStrict(config, configStruct, ignoreConfig); err != nil {
matches := unknownOptionRegex.FindStringSubmatch(err.Error())
if len(matches) == 2 {
// We've got an unrecognized config option.
err = fmt.Errorf("Unknown config setting for section '%s': %s",
sectionName, matches[1])
}
return nil, err
}
if err = env.DecodeStrict(toEnvName(sectionName), EnvSep, configStruct, ignoreEnv); err != nil {
return nil, fmt.Errorf("Invalid environment variable for section '%s': %s",
sectionName, err)
}
return configStruct, nil
}