setting_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package setting
  2. import (
  3. "log"
  4. "testing"
  5. "github.com/spf13/viper"
  6. )
  7. type ConfigTest struct {
  8. HttpAddr string ` toml:"http_addr"`
  9. GrpcAddr string `json:"grpc_addr" toml:"grpcaddr" yaml:"grpc_addr"`
  10. JaegerUrl string `json:"jaeger_url" toml:"jaeger_url" yaml:"jaeger_url" mapstructure:"jaeger_url"`
  11. Tracing bool `toml:"tracing" json:"tracing" yaml:"tracing" ` // opentelemetry tracing
  12. }
  13. type ConfigTest2 struct {
  14. Host string ` toml:"Host"`
  15. Port string `json:"Port" toml:"Port" yaml:"Port"`
  16. Name string `json:"Name" toml:"Name" yaml:"Name" mapstructure:"Name"`
  17. Username string `toml:"Username" json:"Username" yaml:"Username" ` // opentelemetry tracing
  18. }
  19. type C1 struct {
  20. Abc ConfigTest2 `mapstructure:"database"`
  21. }
  22. // jaeger 加载配置文件
  23. func TestSourceFile_Unmarshal(t *testing.T) {
  24. filePath := "config.toml"
  25. viper.SetConfigFile(filePath)
  26. if err := viper.ReadInConfig(); err != nil {
  27. t.Error(err)
  28. }
  29. c := &C1{}
  30. if err := viper.Unmarshal(&c); err != nil {
  31. t.Error(err)
  32. }
  33. log.Println("Unmarshal file sucess", "v", c)
  34. }