Open daixiang0 opened 5 years ago
Hi, @sagikazarmark @spf13 mind have a look?
I use 1.4.0 version.
A more simple example:
package main
import (
"os"
"path"
"fmt"
"strings"
"github.com/spf13/viper"
)
type Config struct {
ClientConfig clientConfig `yaml:"client,omitempty"`
ClientConfigs []clientConfig `yaml:"clients,omitempty"`
}
type clientConfig struct {
URL string `"yaml: url"`
}
func main() {
var (
configFile = "cmd/promtail/promtail-local-config.yaml"
config Config
)
configFile = "path/to/config.yml"
viper.SetConfigType("yaml")
viper.SetConfigName(strings.TrimSuffix(path.Base(configFile), path.Ext(configFile)))
viper.AddConfigPath(path.Dir(configFile))
if err := viper.ReadInConfig(); err != nil {
fmt.Println("msg", "error reading config", "filename", configFile, "err", err)
os.Exit(1)
}
if err := viper.Unmarshal(&config); err != nil {
fmt.Println("msg", "error decoding config", "filename", configFile, "err", err)
os.Exit(1)
}
viper.Debug()
for i := range config.ClientConfigs {
fmt.Println("config: ", i)
}
fmt.Println("url: ",config.ClientConfig.URL)
}
All fmt print is empty.
Seems is mapstructure issue
@sagikazarmark could help me?
Looks like you received an answer on the mapstructure issue tracker. Is your issue resolved?
still not resolved :(
Reproduce: main.go:
configfile:
The bug is that
config.ClientConfigs
is empty.