本文整理匯總了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
}